### Installing destr Dependency Source: https://github.com/unjs/destr/blob/main/README.md This snippet demonstrates how to install the `destr` package using common Node.js package managers: npm, yarn, and pnpm. This is the first step to integrate `destr` into a Node.js project. ```bash # npm npm i destr # yarn yarn add destr # pnpm pnpm i destr ``` -------------------------------- ### Using destr in Deno Source: https://github.com/unjs/destr/blob/main/README.md This snippet illustrates how to import and use `destr` in a Deno environment. It shows importing directly from a URL and then parsing a simple JSON string, demonstrating basic functionality in Deno. ```javascript import { destr, safeDestr } from "https://deno.land/x/destr/src/index.ts"; console.log(destr('{ "deno": "yay" }')); ``` -------------------------------- ### Running Benchmarks for destr Source: https://github.com/unjs/destr/blob/main/README.md This snippet provides commands to run performance benchmarks for the `destr` library. Users can execute these commands to compare `destr`'s performance against `JSON.parse` in Node.js or Bun environments. ```bash pnpm run bench:node pnpm run bench:bun ``` -------------------------------- ### Importing destr in Node.js Source: https://github.com/unjs/destr/blob/main/README.md This snippet shows how to import the `destr` and `safeDestr` functions into a Node.js project using both ESM (ECMAScript Modules) and CommonJS syntax. This allows the functions to be used within your JavaScript files. ```javascript // ESM import { destr, safeDestr } from "destr"; // CommonJS const { destr, safeDestr } = require("destr"); ``` -------------------------------- ### Preventing Prototype Pollution with destr Source: https://github.com/unjs/destr/blob/main/README.md This snippet demonstrates `destr`'s built-in protection against prototype pollution. When parsing an input designed to exploit `__proto__`, `JSON.parse` allows the pollution, while `destr` sanitizes the output, returning an empty object for the polluted property. ```javascript const input = '{ "user": { "__proto__": { "isAdmin": true } } }'; // { user: { __proto__: { isAdmin: true } } } JSON.parse(input); // { user: {} } destr(input); ``` -------------------------------- ### Handling Non-String Input with destr Source: https://github.com/unjs/destr/blob/main/README.md This snippet demonstrates `destr`'s graceful handling of non-string inputs compared to `JSON.parse`. `JSON.parse()` throws an error for `undefined` input, whereas `destr()` returns `undefined`, preventing uncaught exceptions. ```javascript // Uncaught SyntaxError: Unexpected token u in JSON at position 0 JSON.parse(); // undefined destr(); ``` -------------------------------- ### Parsing Known String Values with destr Source: https://github.com/unjs/destr/blob/main/README.md This snippet illustrates `destr`'s ability to parse known string values like 'TRUE' directly into their corresponding JavaScript primitives. `JSON.parse` would throw a `SyntaxError` for such inputs, as they are not valid JSON strings. ```javascript // Uncaught SyntaxError: Unexpected token T in JSON at position 0 JSON.parse("TRUE"); // true destr("TRUE"); ``` -------------------------------- ### Type Safety with destr in TypeScript Source: https://github.com/unjs/destr/blob/main/README.md This snippet highlights the type safety benefits of `destr` compared to `JSON.parse` in TypeScript. While `JSON.parse` returns `any`, `destr` returns `unknown` by default, allowing for explicit type assertion with generics (``) for better type checking. ```typescript const obj = JSON.parse("{}"); // obj type is any const obj = destr("{}"); // obj type is unknown by default const obj = destr("{}"); // obj is well-typed ``` -------------------------------- ### Fallback to Original Value on Parse Failure Source: https://github.com/unjs/destr/blob/main/README.md This snippet shows how `destr` returns the original input string if parsing fails (e.g., for non-JSON plain strings like 'salam'). In contrast, `JSON.parse` would throw a `SyntaxError` for such invalid JSON inputs. ```javascript // Uncaught SyntaxError: Unexpected token s in JSON at position 0 JSON.parse("salam"); // "salam" destr("salam"); ``` -------------------------------- ### Strict Mode Parsing with safeDestr Source: https://github.com/unjs/destr/blob/main/README.md This snippet highlights the behavior of `safeDestr` in strict mode. Unlike `destr`, which might return the original invalid string, `safeDestr` throws an error when the input is not a valid JSON string or parsing fails, providing stricter validation. ```javascript // Returns "[foo" destr("[foo"); // Throws an error safeDestr("[foo"); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.