Skip to content

Commit 69276c4

Browse files
committed
feat: add function of isJsonString and parseQueryParams
1 parent 4c3146f commit 69276c4

File tree

5 files changed

+74
-29
lines changed

5 files changed

+74
-29
lines changed

src/dom.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { arrayEach } from './array';
22
import { easingFunctional, EasingName } from './easing';
33
import { objectEach, objectMerge } from './object';
44
import { stringKebabCase } from './string';
5-
import { isObject } from './type';
5+
import { isObject, isString } from './type';
66

77
export interface Style {
88
[propName: string]: string | number;
@@ -179,3 +179,35 @@ export function getComputedCssVal(el: HTMLElement, property: string, reNumber: b
179179
const originVal = getComputedStyle(el).getPropertyValue(property) ?? '';
180180
return reNumber ? Number(originVal.replace(/([0-9]*)(.*)/g, '$1')) : originVal;
181181
}
182+
183+
/**
184+
* 字符串的像素宽度
185+
* @param {string} str 目标字符串
186+
* @param {number} fontSize 字符串字体大小
187+
* @param {boolean} isRemoveDom 计算后是否移除中间dom元素
188+
* @returns {*}
189+
*/
190+
export function getStrWidthPx(str: string, fontSize: number = 14, isRemoveDom: boolean = false): number {
191+
let strWidth = 0;
192+
console.assert(isString(str), `${str} 不是有效的字符串`);
193+
if (isString(str) && str.length > 0) {
194+
let getEle: HTMLSpanElement | null = document.querySelector('#getStrWidth1494304949567');
195+
if (!getEle) {
196+
const _ele = document.createElement('span');
197+
_ele.id = 'getStrWidth1494304949567';
198+
_ele.style.fontSize = fontSize + 'px';
199+
_ele.style.whiteSpace = 'nowrap';
200+
_ele.style.visibility = 'hidden';
201+
_ele.textContent = str;
202+
document.body.appendChild(_ele);
203+
getEle = _ele;
204+
}
205+
206+
getEle!.textContent = str;
207+
strWidth = getEle!.offsetWidth;
208+
if (isRemoveDom) {
209+
document.body.appendChild(getEle);
210+
}
211+
}
212+
return strWidth;
213+
}

src/string.ts

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -133,33 +133,20 @@ export const stringEscapeHtml = (html: string): string => {
133133
export const stringFill = (length: number, value = ' '): string => new Array(length).fill(value).join('');
134134

135135
/**
136-
* 字符串的像素宽度
137-
* @param {string} str 目标字符串
138-
* @param {number} fontSize 字符串字体大小
139-
* @param {boolean} isRemoveDom 计算后是否移除中间dom元素
140-
* @returns {*}
136+
* 解析URL查询参数
137+
* @param {string} searchStr
138+
* @return {Record<string, string | string[]>}
141139
*/
142-
export function getStrWidthPx(str: string, fontSize: number = 14, isRemoveDom: boolean = false): number {
143-
let strWidth = 0;
144-
console.assert(isString(str), `${str} 不是有效的字符串`);
145-
if (isString(str) && str.length > 0) {
146-
let getEle: HTMLSpanElement | null = document.querySelector('#getStrWidth1494304949567');
147-
if (!getEle) {
148-
const _ele = document.createElement('span');
149-
_ele.id = 'getStrWidth1494304949567';
150-
_ele.style.fontSize = fontSize + 'px';
151-
_ele.style.whiteSpace = 'nowrap';
152-
_ele.style.visibility = 'hidden';
153-
_ele.textContent = str;
154-
document.body.appendChild(_ele);
155-
getEle = _ele;
140+
export function parseQueryParams(searchStr: string = location.search): Record<string, string | string[]> {
141+
const queryObj = {};
142+
Array.from(searchStr.matchAll(/[&?]?([^=&]+)=?([^=&]*)/g)).forEach((item, i) => {
143+
if (!queryObj[item[1]]) {
144+
queryObj[item[1]] = item[2];
145+
} else if (typeof queryObj[item[1]] === 'string') {
146+
queryObj[item[1]] = [queryObj[item[1]], item[2]];
147+
} else {
148+
queryObj[item[1]].push(item[2]);
156149
}
157-
158-
getEle!.textContent = str;
159-
strWidth = getEle!.offsetWidth;
160-
if (isRemoveDom) {
161-
document.body.appendChild(getEle);
162-
}
163-
}
164-
return strWidth;
150+
});
151+
return queryObj;
165152
}

src/type.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,18 @@ export const isNaN = (any: unknown): any is number => Number.isNaN(any as number
5353
export const isDate = (any: unknown): any is Date => typeIs(any) === 'Date';
5454
export const isError = (any: unknown): any is Error => typeIs(any) === 'Error';
5555
export const isRegExp = (any: unknown): any is RegExp => typeIs(any) === 'RegExp';
56+
/**
57+
* 判断一个字符串是否为有效的 JSON
58+
* @param {string} str
59+
* @return {boolean}
60+
*/
61+
export function isJsonString(str: string): boolean {
62+
try {
63+
const parsed = JSON.parse(str);
64+
return typeof parsed === 'object' && parsed !== null;
65+
} catch (e) {
66+
return false;
67+
}
68+
}
5669

5770
export default typeIs;

test/string.test.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import {
77
STRING_LOWERCASE_ALPHA,
88
STRING_UPPERCASE_ALPHA,
99
stringEscapeHtml,
10-
stringFill
10+
stringFill,
11+
parseQueryParams
1112
} from '../src/string';
1213

1314
test('stringCamelCase', () => {
@@ -69,3 +70,7 @@ test('stringFill', () => {
6970
expect(stringFill(5)).toEqual(' ');
7071
expect(stringFill(5, '1')).toEqual('11111');
7172
});
73+
74+
test('parseQueryParams', () => {
75+
expect(parseQueryParams('?a=1&a=3&b=true&c&a=111')).toEqual({ a: ['1', '3', '111'], b: 'true', c: '' });
76+
});

test/type.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
isDate,
66
isError,
77
isFunction,
8+
isJsonString,
89
isNaN,
910
isNull,
1011
isNumber,
@@ -90,3 +91,10 @@ test('isRegExp', () => {
9091
expect(isRegExp(/s/)).toBe(true);
9192
expect(isRegExp('/s/')).toBe(false);
9293
});
94+
95+
test('isJsonString', () => {
96+
const jsonString = '{"name": "John", "age": 30}';
97+
const invalidJsonString = '{"name": "John", "age": 30';
98+
expect(isJsonString(jsonString)).toBe(true);
99+
expect(isJsonString(invalidJsonString)).toBe(false);
100+
});

0 commit comments

Comments
 (0)