@jhqn/utils-core 0.4.1 → 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 +20 -20
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -349,32 +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
- /**
353
- * 判断是否合法JSON字符串
354
- * @param str
355
- */
356
- const isJSONStr = (str) => {
357
- if (isDef(str)) try {
358
- JSON.parse(str);
359
- return true;
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);
360
361
  } catch (e) {
361
362
  jWarn("Failed to parse invalid string", e);
362
- return false;
363
+ return INVALID_JSON;
363
364
  }
364
- return false;
365
365
  };
366
366
  /**
367
+ * 判断是否合法JSON字符串
368
+ * @param str
369
+ */
370
+ const isJSONStr = (str) => parseJSONLikeStr(str) !== INVALID_JSON;
371
+ /**
367
372
  * 判断是否合法JSON数组字符串
368
373
  * @param str
369
374
  */
370
375
  const isArrStr = (str) => {
371
- if (isDef(str)) try {
372
- return Array.isArray(JSON.parse(str));
373
- } catch (e) {
374
- jWarn("Failed to parse invalid string", e);
375
- return false;
376
- }
377
- return false;
376
+ const parsed = parseJSONLikeStr(str);
377
+ return parsed !== INVALID_JSON && isArray(parsed);
378
378
  };
379
379
  /**
380
380
  * 判断是否有效的key
@@ -517,13 +517,13 @@ const isValidRes = (res, code) => isValidResCode(res, code) && isDef(res.data);
517
517
  * @param res
518
518
  * @param code
519
519
  */
520
- const isValidArrRes = (res, code) => isValidResCode(res, code) && Array.isArray(res.data);
520
+ const isValidArrRes = (res, code) => isValidResCode(res, code) && isArray(res.data);
521
521
  /**
522
522
  * 校验接口分页返回内容
523
523
  * @param res
524
524
  * @param code
525
525
  */
526
- const isValidPageRes = (res, code) => isValidResCode(res, code) && isDef(res.data) && Array.isArray(res.data.list || res.data.records);
526
+ const isValidPageRes = (res, code) => isValidResCode(res, code) && isDef(res.data) && isArray(res.data.list || res.data.records);
527
527
 
528
528
  //#endregion
529
529
  //#region src/desensitize.ts
@@ -914,7 +914,7 @@ const attachFmt = (data) => {
914
914
  let attachList = [];
915
915
  const prototype = Object.prototype.toString.call(data);
916
916
  if (!data) attachList = [];
917
- else if (Array.isArray(data)) attachList = data.filter(Boolean);
917
+ else if (isArray(data)) attachList = data.filter(Boolean);
918
918
  else if (typeof data === "string") if (isArrStr(data)) attachList = (parseJSON(data) || []).filter(Boolean);
919
919
  else {
920
920
  const jsonData = parseJSON(data);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jhqn/utils-core",
3
- "version": "0.4.1",
3
+ "version": "0.4.3",
4
4
  "keywords": [
5
5
  "jade",
6
6
  "utils"