Urara-Blog/node_modules/.pnpm-store/v3/files/e5/f70c322adb95309e093ae0a83d8367c499675aaa17fc9a6d6fcbb7cd121e5598d060f87f32b5f5aecb5b2211aaaa440054eb45b2f5a36796c5736d74582ca2
2022-08-14 01:14:53 +08:00

46 lines
1.2 KiB
Text

const suspectProtoRx = /"(?:_|\\u005[Ff])(?:_|\\u005[Ff])(?:p|\\u0070)(?:r|\\u0072)(?:o|\\u006[Ff])(?:t|\\u0074)(?:o|\\u006[Ff])(?:_|\\u005[Ff])(?:_|\\u005[Ff])"\s*:/;
const suspectConstructorRx = /"(?:c|\\u0063)(?:o|\\u006[Ff])(?:n|\\u006[Ee])(?:s|\\u0073)(?:t|\\u0074)(?:r|\\u0072)(?:u|\\u0075)(?:c|\\u0063)(?:t|\\u0074)(?:o|\\u006[Ff])(?:r|\\u0072)"\s*:/;
const JsonSigRx = /^["{[]|^-?[0-9][0-9.]{0,14}$/;
function jsonParseTransform(key, value) {
if (key === "__proto__" || key === "constructor") {
return;
}
return value;
}
function destr(val) {
if (typeof val !== "string") {
return val;
}
const _lval = val.toLowerCase();
if (_lval === "true") {
return true;
}
if (_lval === "false") {
return false;
}
if (_lval === "null") {
return null;
}
if (_lval === "nan") {
return NaN;
}
if (_lval === "infinity") {
return Infinity;
}
if (_lval === "undefined") {
return void 0;
}
if (!JsonSigRx.test(val)) {
return val;
}
try {
if (suspectProtoRx.test(val) || suspectConstructorRx.test(val)) {
return JSON.parse(val, jsonParseTransform);
}
return JSON.parse(val);
} catch (_e) {
return val;
}
}
export { destr as default };