Package Exports
- destr
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (destr) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
destr
A faster, secure and convenient alternative for JSON.parse
:
Fast fallback to input if is not string:
// Uncaught SyntaxError: Unexpected token u in JSON at position 0
JSON.parse()
// undefined
destr()
// JSON.parse x 5,363,773 ops/sec ±0.31% (96 runs sampled
JSON.parse(3.14159265359)
// destr x 660,537,795 ops/sec ±0.06% (86 runs sampled)
destr(3.14159265359)
Fast lookup for known string values:
// Uncaught SyntaxError: Unexpected token T in JSON at position 0
JSON.parse('TRUE')
// true
destr('TRUE')
// JSON.parse x 10,432,994 ops/sec ±0.23% (94 runs sampled)
JSON.parse('true')
// destr x 652,107,152 ops/sec ±0.11% (94 runs sampled
destr('true')
Fallback to original value if parse fails (empty or any plain string):
// Uncaught SyntaxError: Unexpected token s in JSON at position 0
// JSON.parse (try-catch) x 248,749 ops/sec ±1.66% (93 runs sampled)
JSON.parse('salam')
// destr x 32,415,523 ops/sec ±0.57% (94 runs sampled)
destr('salam')
Avoid Prototype Pollution:
const input = '{ "user": { "__proto__": { "isAdmin": true } } }'
// { user: { __proto__: { isAdmin: true } } }
JSON.parse(input)
// { user: {} }
destr(input)
Better types:
interface JSON {
parse(text: string, reviver?: (this: any, key: string, value: any) => any): any
}
export declare type JSONObject = {
[key: string]: JSONValue;
};
export declare type JSONValue = true | false | null | string | JSONObject | JSONValue[];
export declare type DestrValue = JSONValue | JSONValue[] | undefined;
export default function destr(val: any): DestrValue;
License
MIT