Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | 1x 1x 9x 1x 8x 6x 6x 4x 4x 4x 4x 5x 5x 5x 5x 4x 2x 6x 1x | /**
* 解析 location.search 为一个JSON对象
* - 或者获取其中某个参数
* @method getQuery
* @param {String} href URL字符串
* @param {String} name 参数名称
* @return {Object|String} query对象 | 参数值
* @example
* import { getQuery } from '@spore-ui/tskit';
* const url = 'http://localhost/profile?beijing=huanyingni';
* console.info( getQuery(url) );
* // {beijing : 'huanyingni'}
* console.info( getQuery(url, 'beijing') );
* // 'huanyingni'
*/
import { TypePlainObject } from '../types';
export type TypeQueryCache = Map<string, TypePlainObject>;
export const queryCache: TypeQueryCache = new Map();
export function getQuery(): TypePlainObject;
export function getQuery(href: string): TypePlainObject;
export function getQuery(href: string, name: string): string;
export function getQuery(href?: string, name?: string): string | TypePlainObject {
const url = href || this?.location?.href || '';
if (!url) {
throw new Error('Require url as parameter.');
}
let query = queryCache.get(url);
if (!query) {
query = {};
const searchIndex = url.indexOf('?');
if (searchIndex >= 0) {
let search = url.slice(searchIndex + 1, url.length);
search = search.replace(/#.*/, '');
const params: string[] = search.split('&');
params.forEach((group: string) => {
const equalIndex = group.indexOf('=');
if (equalIndex > 0) {
const key = group.slice(0, equalIndex);
const value = group.slice(equalIndex + 1, group.length);
query[key] = value;
}
});
queryCache.set(url, query);
}
}
if (name) {
return query[name] as string || '';
}
return query;
}
export default getQuery;
|