### stringify.configure(options) Source: https://github.com/bridgear/safe-stable-stringify/blob/main/readme.md Configures and returns a new stringify function with custom options for handling BigInt, circular references, deterministic key order, and more. ```APIDOC ## stringify.configure(options) ### Description Creates and returns a new stringify function with specified configuration options. This allows for customization of how BigInt, circular references, key order, and other aspects are handled during serialization. ### Parameters #### options - `options` (object) - An object containing configuration options: - `bigint` (boolean|string): Controls how BigInt values are serialized. `true` converts to number, `'string'` serializes as a string, otherwise ignored. Default: `true`. - `circularValue` (string|null|undefined|ErrorConstructor): Defines the replacement for circular references. `undefined` omits them, `Error` throws an error. Default: `'[Circular]'`. - `deterministic` (boolean|function): If `true` or a comparator function, guarantees deterministic key order. Default: `true`. - `maximumBreadth` (number): Maximum number of entries to serialize per object. Default: `Infinity`. - `maximumDepth` (number): Maximum nesting level for serialization. Default: `Infinity`. - `strict` (boolean): If `true`, throws an error for values not representable as JSON (e.g., functions, NaN). Default: `false`. - `safe` (boolean): If `true`, errors from `.toJSON()`, getters, or replacers return the error message instead of throwing. Default: `false`. ### Returns - {function} A new stringify function configured with the provided options. ``` -------------------------------- ### Configured Stringify with Custom Options Source: https://github.com/bridgear/safe-stable-stringify/blob/main/readme.md Shows how to configure safe-stable-stringify with custom options for bigint serialization, circular value representation, deterministic key order, maximum depth, and maximum breadth. ```javascript import { configure } from 'safe-stable-stringify' const stringify = configure({ bigint: true, circularValue: 'Magic circle!', deterministic: false, maximumDepth: 1, maximumBreadth: 4 }) const circular = { bigint: 999_999_999_999_999_999n, typed: new Uint8Array(3), deterministic: "I don't think so", } circular.circular = circular circular.ignored = true circular.alsoIgnored = 'Yes!' const stringified = stringify(circular, null, 4) console.log(stringified) // { // "bigint": 999999999999999999, // "typed": "[Object]", // "deterministic": "I don't think so", // "circular": "Magic circle!", // "...": "2 items not stringified" // } ``` -------------------------------- ### safe-stable-stringify Benchmark Results Source: https://github.com/bridgear/safe-stable-stringify/blob/main/readme.md Performance metrics for safe-stable-stringify across different object types (simple, circular, deep) and stringification methods (default, replacer, array, indentation). ```markdown simple: simple object x 3,463,894 ops/sec ±0.44% (98 runs sampled) simple: circular x 1,236,007 ops/sec ±0.46% (99 runs sampled) simple: deep x 18,942 ops/sec ±0.41% (93 runs sampled) simple: deep circular x 18,690 ops/sec ±0.72% (96 runs sampled) replacer: simple object x 2,664,940 ops/sec ±0.31% (98 runs sampled) replacer: circular x 1,015,981 ops/sec ±0.09% (99 runs sampled) replacer: deep x 17,328 ops/sec ±0.38% (97 runs sampled) replacer: deep circular x 17,071 ops/sec ±0.21% (98 runs sampled) array: simple object x 3,869,608 ops/sec ±0.22% (98 runs sampled) array: circular x 3,853,943 ops/sec ±0.45% (96 runs sampled) array: deep x 3,563,227 ops/sec ±0.20% (100 runs sampled) array: deep circular x 3,286,475 ops/sec ±0.07% (100 runs sampled) indentation: simple object x 2,183,162 ops/sec ±0.66% (97 runs sampled) indentation: circular x 872,538 ops/sec ±0.57% (98 runs sampled) indentation: deep x 16,795 ops/sec ±0.48% (93 runs sampled) indentation: deep circular x 16,443 ops/sec ±0.40% (97 runs sampled) ``` -------------------------------- ### Basic Stringify with BigInt and Circular Structures Source: https://github.com/bridgear/safe-stable-stringify/blob/main/readme.md Demonstrates how safe-stable-stringify handles BigInt and circular references, which cause errors in JSON.stringify. ```javascript const stringify = require('safe-stable-stringify') const bigint = { a: 0, c: 2n, b: 1 } stringify(bigint) // '{"a":0,"b":1,"c":2}' JSON.stringify(bigint) // TypeError: Do not know how to serialize a BigInt const circular = { b: 1, a: 0 } circular.circular = circular stringify(circular) // '{"a":0,"b":1,"circular":"[Circular]"}' JSON.stringify(circular) // TypeError: Converting circular structure to JSON stringify(circular, ['a', 'b'], 2) // { // "a": 0, // "b": 1 // } ``` -------------------------------- ### Comparison with Alternative Stringify Libraries Source: https://github.com/bridgear/safe-stable-stringify/blob/main/readme.md Performance comparison of safe-stable-stringify against several other JSON stringification libraries, including fast-json-stable-stringify, json-stable-stringify, and others. ```markdown fast-json-stable-stringify x 18,765 ops/sec ±0.71% (94 runs sampled) json-stable-stringify x 13,870 ops/sec ±0.72% (94 runs sampled) fast-stable-stringify x 21,343 ops/sec ±0.33% (95 runs sampled) faster-stable-stringify x 17,707 ops/sec ±0.44% (97 runs sampled) json-stringify-deterministic x 11,208 ops/sec ±0.57% (98 runs sampled) fast-safe-stringify x 21,460 ops/sec ±0.75% (99 runs sampled) this x 30,367 ops/sec ±0.39% (96 runs sampled) ``` -------------------------------- ### Configured Stringify to Throw on Circular References Source: https://github.com/bridgear/safe-stable-stringify/blob/main/readme.md Demonstrates configuring safe-stable-stringify to throw a TypeError when encountering circular references, using the Error constructor for the circularValue option. ```javascript import { configure } from 'safe-stable-stringify' const throwOnCircular = configure({ circularValue: Error }) const circular = { bigint: 999_999_999_999_999_999n, typed: new Uint8Array(3), deterministic: "I don't think so", } circular.circular = circular circular.ignored = true circular.alsoIgnored = 'Yes!' throwOnCircular(circular); // TypeError: Converting circular structure to JSON ``` -------------------------------- ### stringify(value[, replacer[, space]]) Source: https://github.com/bridgear/safe-stable-stringify/blob/main/readme.md Serializes a JavaScript value to a JSON string, similar to JSON.stringify but with enhanced handling for circular structures and BigInt values. ```APIDOC ## stringify(value[, replacer[, space]]) ### Description Serializes a JavaScript value to a JSON string. It gracefully handles circular structures and BigInt values, unlike the native JSON.stringify. ### Parameters #### value - `value` (any) - The value to convert to a JSON string. #### replacer - `replacer` (string[]|function|null) - A function that alters the behavior of the stringification process, or an array of strings and numbers that acts as a whitelist for selecting properties. #### space - `space` (number|string) - Adds indentation, white space, and line break characters to the return-value JSON text to make it more readable. ### Returns - {string} The JSON string representation of the value. ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.