What is @types/json-stable-stringify?
@types/json-stable-stringify is a TypeScript type definition package for the json-stable-stringify library. This library provides a way to stringify JavaScript objects in a consistent and stable order, which is useful for tasks like comparing JSON objects or generating consistent hash values.
What are @types/json-stable-stringify's main functionalities?
Stable Stringification
This feature allows you to stringify a JavaScript object in a stable order. The keys in the resulting JSON string are sorted, ensuring consistent output for the same input object.
{"typescript":"import stringify from 'json-stable-stringify';\n\nconst obj = { b: 1, a: 2 };\nconst stableString = stringify(obj);\nconsole.log(stableString); // Output: '{\"a\":2,\"b\":1}'"}
Custom Comparator
This feature allows you to provide a custom comparator function to control the order of keys in the resulting JSON string. In this example, the keys are sorted in reverse order.
{"typescript":"import stringify from 'json-stable-stringify';\n\nconst obj = { b: 1, a: 2 };\nconst stableString = stringify(obj, { cmp: (a, b) => a.key < b.key ? 1 : -1 });\nconsole.log(stableString); // Output: '{\"b\":1,\"a\":2}'"}
Custom Replacer
This feature allows you to provide a custom replacer function to filter or modify values during the stringification process. In this example, the key 'b' is excluded from the resulting JSON string.
{"typescript":"import stringify from 'json-stable-stringify';\n\nconst obj = { b: 1, a: 2, c: 3 };\nconst stableString = stringify(obj, { replacer: (key, value) => key === 'b' ? undefined : value });\nconsole.log(stableString); // Output: '{\"a\":2,\"c\":3}'"}
Other packages similar to @types/json-stable-stringify
fast-json-stable-stringify
fast-json-stable-stringify is another library that provides stable JSON stringification. It is known for its performance and simplicity. Compared to json-stable-stringify, it is often faster but offers fewer customization options.
json-stringify-deterministic
json-stringify-deterministic is a library that ensures deterministic JSON stringification. It is similar to json-stable-stringify but focuses on providing a deterministic output without additional features like custom comparators or replacers.
canonical-json
canonical-json is a library that provides canonical JSON stringification, ensuring a consistent and stable order of keys. It is similar to json-stable-stringify but follows the canonical JSON specification more strictly.
Installation
npm install --save @types/json-stable-stringify
Summary
This package contains type definitions for json-stable-stringify (https://github.com/substack/json-stable-stringify).
Details
Files were exported from https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/json-stable-stringify.
declare function stringify(obj: any, opts?: stringify.Comparator | stringify.Options): string;
declare namespace stringify {
interface Element {
key: string;
value: any;
}
type Comparator = (a: Element, b: Element, opts?: { get(k: string): any }) => number;
type Replacer = (key: string, value: any) => any;
interface Options {
cmp?: Comparator;
space?: string | number;
replacer?: Replacer;
cycles?: boolean;
}
}
export = stringify;
Additional Details
- Last updated: Thu, 24 Oct 2024 15:34:35 GMT
- Dependencies: none
Credits
These definitions were written by Matt Frantz.