Skip to content

Commit 7ceb846

Browse files
committed
fix: 新增isJson
1 parent dcedf06 commit 7ceb846

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

src/lib/validate/index.ts

+17-3
Original file line numberDiff line numberDiff line change
@@ -238,9 +238,9 @@ export function isEmpty(v: any): boolean {
238238
*/
239239
export function isHexCode(c: number): boolean {
240240
return (
241-
(0x30 /* 0 */ <= c && c <= 0x39 /* 9 */) ||
242-
(0x41 /* A */ <= c && c <= 0x46 /* F */) ||
243-
(0x61 /* a */ <= c && c <= 0x66 /* f */)
241+
(0x30 /* 0 */ <= c && c <= 0x39) /* 9 */ ||
242+
(0x41 /* A */ <= c && c <= 0x46) /* F */ ||
243+
(0x61 /* a */ <= c && c <= 0x66) /* f */
244244
);
245245
}
246246

@@ -359,6 +359,19 @@ export function inArray(needle: any, haystack: any): boolean {
359359
return res;
360360
}
361361

362+
// isJson 检查变量是否JSON字符串.
363+
export function isJson(text: any): boolean {
364+
if (typeof text !== 'string') {
365+
return false;
366+
}
367+
try {
368+
JSON.parse(text);
369+
return true;
370+
} catch (error) {
371+
return false;
372+
}
373+
}
374+
362375
export default {
363376
isString,
364377
isArray,
@@ -391,4 +404,5 @@ export default {
391404
objectOf,
392405
isNaturalNum,
393406
inArray,
407+
isJson,
394408
};

0 commit comments

Comments
 (0)