Convert JS values into deterministic JSON. It can sort keys in ascending order or according to scheme. Normjson is useful for hash algorithms to generate the same hashsum in different js environments.
Install via npm
npm i normjson
Require from unpkg.com:
Normjson will create the same JSON string from objects with different props order and
JSON.stringify does not:
const object1 = a: 1 b: 2;const object2 = b: 2 a: 1;JSON === JSON; // => false=== ; // => true
If no scheme specified then properties sorting by name in ascending order:
Scheme could be presented in several ways.
Array scheme contains list of properties which should be converted in specified order.
It allow to specify rest of props with true and sorts them by key names:
Rest of props could be set as the first, the last or in the middle.
Object scheme allow nesting.
Function as a scheme should return other type of scheme (array, object, no). The first argument is a document.