@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.
- package/dist/index.js +20 -20
- 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
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
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
|
|
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
|
-
|
|
372
|
-
|
|
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) &&
|
|
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) &&
|
|
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 (
|
|
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);
|