@jhqn/utils-core 0.4.3 → 0.4.4
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 +11 -0
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -350,12 +350,23 @@ const isError = (val) => getVariableType(val) === "error";
|
|
|
350
350
|
*/
|
|
351
351
|
const isDef = (val) => !isNull(val) && !isUndefined(val);
|
|
352
352
|
const INVALID_JSON = Symbol("invalid_json");
|
|
353
|
+
const JSON_LITERAL_PREFIXES = {
|
|
354
|
+
t: "true",
|
|
355
|
+
f: "false",
|
|
356
|
+
n: "null"
|
|
357
|
+
};
|
|
353
358
|
const parseJSONLikeStr = (str) => {
|
|
354
359
|
if (!isDef(str)) return INVALID_JSON;
|
|
355
360
|
const normalized = str.trim();
|
|
356
361
|
if (normalized === "") return INVALID_JSON;
|
|
357
362
|
const firstChar = normalized[0];
|
|
358
363
|
if (!"{[\"tfn-0123456789".includes(firstChar)) return INVALID_JSON;
|
|
364
|
+
if (firstChar in JSON_LITERAL_PREFIXES) {
|
|
365
|
+
const literal = JSON_LITERAL_PREFIXES[firstChar];
|
|
366
|
+
if (!normalized.startsWith(literal)) return INVALID_JSON;
|
|
367
|
+
const nextChar = normalized[literal.length];
|
|
368
|
+
if (isDef(nextChar) && !",]} \r\n".includes(nextChar)) return INVALID_JSON;
|
|
369
|
+
}
|
|
359
370
|
try {
|
|
360
371
|
return JSON.parse(normalized);
|
|
361
372
|
} catch (e) {
|