mirror of
https://github.com/Tencent/tmagic-editor.git
synced 2025-09-19 20:45:49 +08:00
feat(utils): 添加设置删除获取url参数的方法
This commit is contained in:
parent
0987901a46
commit
105de9d2c6
@ -163,6 +163,99 @@ export const getUrlParam = (param: string, url?: string) => {
|
||||
return '';
|
||||
};
|
||||
|
||||
/**
|
||||
* 设置url中指定的参数
|
||||
*
|
||||
* @param {string}
|
||||
* name [参数名]
|
||||
* @param {string}
|
||||
* value [参数值]
|
||||
* @param {string}
|
||||
* url [发生替换的url地址|默认为location.href]
|
||||
* @return {string} [返回处理后的url]
|
||||
*/
|
||||
export const setUrlParam = (name: string, value: string, url = globalThis.location.href) => {
|
||||
const reg = new RegExp(`[?&#]${name}=([^&#]*)`, 'gi');
|
||||
|
||||
const matches = url.match(reg);
|
||||
|
||||
const key = `{key${new Date().getTime()}}`;
|
||||
let strArr;
|
||||
|
||||
if (matches && matches.length > 0) {
|
||||
strArr = matches[matches.length - 1];
|
||||
} else {
|
||||
strArr = '';
|
||||
}
|
||||
|
||||
const extra = `${name}=${value}`;
|
||||
|
||||
// 当原url中含有要替换的属性:value不为空时,仅对值做替换,为空时,直接把参数删除掉
|
||||
if (strArr) {
|
||||
const first = strArr.charAt(0);
|
||||
url = url.replace(strArr, key);
|
||||
url = url.replace(key, value ? first + extra : '');
|
||||
} else if (value) {
|
||||
// 当原url中不含有要替换的属性且value值不为空时,直接在url后面添加参数字符串
|
||||
if (url.indexOf('?') > -1) {
|
||||
url += `&${extra}`;
|
||||
} else {
|
||||
url += `?${extra}`;
|
||||
}
|
||||
}
|
||||
// 其它情况直接返回原url
|
||||
return url;
|
||||
};
|
||||
|
||||
export const getSearchObj = (
|
||||
search = globalThis.location.search ? globalThis.location.search.substring(1) : '',
|
||||
): Record<string, string> => {
|
||||
return search.split('&').reduce((obj, item) => {
|
||||
const [a, b = ''] = item.split('=');
|
||||
return { ...obj, [a]: b };
|
||||
}, {});
|
||||
};
|
||||
|
||||
export const delQueStr = (url: string, ref: string[] | string) => {
|
||||
let str = '';
|
||||
if (url.indexOf('?') !== -1) {
|
||||
str = url.substring(url.indexOf('?') + 1);
|
||||
} else {
|
||||
return url;
|
||||
}
|
||||
let arr = [];
|
||||
let returnurl = '';
|
||||
|
||||
const isHit = Array.isArray(ref)
|
||||
? function (v: string) {
|
||||
return ~ref.indexOf(v);
|
||||
}
|
||||
: function (v: string) {
|
||||
return v === ref;
|
||||
};
|
||||
|
||||
if (str.indexOf('&') !== -1) {
|
||||
arr = str.split('&');
|
||||
for (let i = 0, len = arr.length; i < len; i++) {
|
||||
if (!isHit(arr[i].split('=')[0])) {
|
||||
returnurl = `${returnurl + arr[i].split('=')[0]}=${arr[i].split('=')[1]}&`;
|
||||
}
|
||||
}
|
||||
|
||||
return returnurl
|
||||
? `${url.substr(0, url.indexOf('?'))}?${returnurl.substr(0, returnurl.length - 1)}`
|
||||
: url.substr(0, url.indexOf('?'));
|
||||
}
|
||||
|
||||
arr = str.split('=');
|
||||
|
||||
if (isHit(arr[0])) {
|
||||
return url.substr(0, url.indexOf('?'));
|
||||
}
|
||||
|
||||
return url;
|
||||
};
|
||||
|
||||
export const isObject = (obj: any) => Object.prototype.toString.call(obj) === '[object Object]';
|
||||
|
||||
export const isPop = (node: MComponent | null): boolean => Boolean(node?.type?.toLowerCase().endsWith('pop'));
|
||||
|
@ -23,7 +23,7 @@ import type { DataSchema } from '@tmagic/schema';
|
||||
import * as util from '../../src';
|
||||
|
||||
describe('asyncLoadJs', () => {
|
||||
const url = 'https://m.film.qq.com/magic-ui/production/1/1625056093304/magic/magic-ui.umd.min.js';
|
||||
const url = 'https://m.www.tmagic.com/magic-ui/production/1/1625056093304/magic/magic-ui.umd.min.js';
|
||||
|
||||
test('第一次加载asyncLoadJs带url与crossorigin参数', () => {
|
||||
const crossOrigin = 'anonymous';
|
||||
@ -57,7 +57,7 @@ describe('asyncLoadJs', () => {
|
||||
});
|
||||
|
||||
describe('asyncLoadCss', () => {
|
||||
const url = 'https://beta.m.film.qq.com/magic-act/css/BuyGift.75d837d2b3fd.css?max_age=864000';
|
||||
const url = 'https://beta.m.www.tmagic.com/magic-act/css/BuyGift.75d837d2b3fd.css?max_age=864000';
|
||||
|
||||
test('第一次加载asyncLoadCss', () => {
|
||||
const load = util.asyncLoadCss(url);
|
||||
@ -180,24 +180,47 @@ describe('filterXSS', () => {
|
||||
|
||||
describe('getUrlParam', () => {
|
||||
test('正常', () => {
|
||||
const url = 'http://film.qq.com?a=b';
|
||||
const url = 'http://www.tmagic.com?a=b';
|
||||
const value = util.getUrlParam('a', url);
|
||||
expect(value).toBe('b');
|
||||
});
|
||||
|
||||
test('null', () => {
|
||||
const url = 'http://film.qq.com';
|
||||
const url = 'http://www.tmagic.com';
|
||||
const value = util.getUrlParam('a', url);
|
||||
expect(value).toBe('');
|
||||
});
|
||||
|
||||
test('emprty', () => {
|
||||
const url = 'http://film.qq.com?a=';
|
||||
const url = 'http://www.tmagic.com?a=';
|
||||
const value = util.getUrlParam('a', url);
|
||||
expect(value).toBe('');
|
||||
});
|
||||
});
|
||||
|
||||
describe('setUrlParam', () => {
|
||||
test('正常', () => {
|
||||
expect(util.setUrlParam('a', '1', 'https://www.tmagic.com')).toBe('https://www.tmagic.com?a=1');
|
||||
|
||||
expect(util.setUrlParam('a', '1', 'https://www.tmagic.com?c&d')).toBe('https://www.tmagic.com?c&d&a=1');
|
||||
expect(util.setUrlParam('a', '1', 'https://www.tmagic.com?b=1')).toBe('https://www.tmagic.com?b=1&a=1');
|
||||
});
|
||||
});
|
||||
|
||||
describe('getSearchObj', () => {
|
||||
test('正常', () => {
|
||||
expect(util.getSearchObj('a=1&b=2')).toEqual({ a: '1', b: '2' });
|
||||
});
|
||||
});
|
||||
|
||||
describe('delQueStr', () => {
|
||||
test('正常', () => {
|
||||
expect(util.delQueStr('https://www.tmagic.com?a=1', 'a')).toBe('https://www.tmagic.com');
|
||||
expect(util.delQueStr('https://www.tmagic.com?a=1&b=2', ['a', 'b'])).toBe('https://www.tmagic.com');
|
||||
expect(util.delQueStr('https://www.tmagic.com?a=1&b=2', ['a'])).toBe('https://www.tmagic.com?b=2');
|
||||
});
|
||||
});
|
||||
|
||||
describe('isPop', () => {
|
||||
// type 为 pop 结尾 isPop 才为 true
|
||||
test('true', () => {
|
||||
@ -250,24 +273,24 @@ describe('isPage', () => {
|
||||
|
||||
describe('getHost', () => {
|
||||
test('正常', () => {
|
||||
const host = util.getHost('https://film.qq.com/index.html');
|
||||
expect(host).toBe('film.qq.com');
|
||||
const host = util.getHost('https://www.tmagic.com/index.html');
|
||||
expect(host).toBe('www.tmagic.com');
|
||||
});
|
||||
});
|
||||
|
||||
describe('isSameDomain', () => {
|
||||
test('正常', () => {
|
||||
const flag = util.isSameDomain('https://film.qq.com/index.html', 'film.qq.com');
|
||||
const flag = util.isSameDomain('https://www.tmagic.com/index.html', 'www.tmagic.com');
|
||||
expect(flag).toBeTruthy();
|
||||
});
|
||||
|
||||
test('不正常', () => {
|
||||
const flag = util.isSameDomain('https://film.qq.com/index.html', 'test.film.qq.com');
|
||||
const flag = util.isSameDomain('https://www.tmagic.com/index.html', 'test.www.tmagic.com');
|
||||
expect(flag).toBeFalsy();
|
||||
});
|
||||
|
||||
test('不是http', () => {
|
||||
const flag = util.isSameDomain('ftp://film.qq.com/index.html', 'test.film.qq.com');
|
||||
const flag = util.isSameDomain('ftp://www.tmagic.com/index.html', 'test.www.tmagic.com');
|
||||
expect(flag).toBeTruthy();
|
||||
});
|
||||
});
|
||||
|
Loading…
x
Reference in New Issue
Block a user