json-stable-stringify-without-jsonify
Last updated
Last updated
This is the same as https://github.com/substack/json-stable-stringify but it doesn't depend on libraries without licenses (jsonify).
deterministic version of JSON.stringify()
so you can get a consistent hash from stringified results
You can also pass in a custom comparison function.
output:
Return a deterministic stringified string str
from the object obj
.
If opts
is given, you can supply an opts.cmp
to have a custom comparison function for object keys. Your function opts.cmp
is called with these parameters:
For example, to sort on the object key names in reverse order you could write:
which results in the output string:
Or if you wanted to sort on the object values in reverse order, you could write:
which outputs:
If you specify opts.space
, it will indent the output for pretty-printing. Valid values are strings (e.g. {space: \t}
) or a number of spaces ({space: 3}
).
For example:
which outputs:
The replacer parameter is a function opts.replacer(key, value)
that behaves the same as the replacer from the core JSON object.
With npm do:
MIT