@jhqn/utils-core 0.4.2 → 0.4.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/index.js +17 -23
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -349,38 +349,32 @@ const isError = (val) => getVariableType(val) === "error";
349
349
  * @param {any} val 检测的目标
350
350
  */
351
351
  const isDef = (val) => !isNull(val) && !isUndefined(val);
352
+ const INVALID_JSON = Symbol("invalid_json");
353
+ const parseJSONLikeStr = (str) => {
354
+ if (!isDef(str)) return INVALID_JSON;
355
+ const normalized = str.trim();
356
+ if (normalized === "") return INVALID_JSON;
357
+ const firstChar = normalized[0];
358
+ if (!"{[\"tfn-0123456789".includes(firstChar)) return INVALID_JSON;
359
+ try {
360
+ return JSON.parse(normalized);
361
+ } catch (e) {
362
+ jWarn("Failed to parse invalid string", e);
363
+ return INVALID_JSON;
364
+ }
365
+ };
352
366
  /**
353
367
  * 判断是否合法JSON字符串
354
368
  * @param str
355
369
  */
356
- const isJSONStr = (str) => {
357
- if (isDef(str)) {
358
- const normalized = str.trim();
359
- if (normalized === "") return false;
360
- const firstChar = normalized[0];
361
- if (!"{[\"tfn-0123456789".includes(firstChar)) return false;
362
- try {
363
- JSON.parse(normalized);
364
- return true;
365
- } catch (e) {
366
- jWarn("Failed to parse invalid string", e);
367
- return false;
368
- }
369
- }
370
- return false;
371
- };
370
+ const isJSONStr = (str) => parseJSONLikeStr(str) !== INVALID_JSON;
372
371
  /**
373
372
  * 判断是否合法JSON数组字符串
374
373
  * @param str
375
374
  */
376
375
  const isArrStr = (str) => {
377
- if (isDef(str)) try {
378
- return isArray(JSON.parse(str));
379
- } catch (e) {
380
- jWarn("Failed to parse invalid string", e);
381
- return false;
382
- }
383
- return false;
376
+ const parsed = parseJSONLikeStr(str);
377
+ return parsed !== INVALID_JSON && isArray(parsed);
384
378
  };
385
379
  /**
386
380
  * 判断是否有效的key
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jhqn/utils-core",
3
- "version": "0.4.2",
3
+ "version": "0.4.3",
4
4
  "keywords": [
5
5
  "jade",
6
6
  "utils"