3.17.105 • Published 10 months ago

@diotoborg/fugiat-distinctio v3.17.105

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

@diotoborg/fugiat-distinctio

github actions coverage License Downloads

npm badge

deterministic version of JSON.stringify() so you can get a consistent hash from stringified results

You can also pass in a custom comparison function.

example

const stringify = require('json-stringify');

const obj = { c: 8, b: [{ z: 6, y: 5, x: 4 }, 7], a: 3 };

console.log(stringify(obj));

output:

{"a":3,"b":[{"x":4,"y":5,"z":6},7],"c":8}

methods

const stringify = require('json-stringify')

const str = stringify(obj, opts)

Return a deterministic stringified string str from the object obj.

options

cmp

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:

opts.cmp({ key: akey, value: avalue }, { key: bkey, value: bvalue }, { get(key): value })

For example, to sort on the object key names in reverse order you could write:

const stringify = require('json-stringify');

const obj = { c: 8, b: [{ z: 6, y: 5, x: 4 },7], a: 3 };

const s = stringify(obj, function (a, b) {
	return b.key.localeCompare(a.key);
});

console.log(s);

which results in the output string:

{"c":8,"b":[{"z":6,"y":5,"x":4},7],"a":3}

Or if you wanted to sort on the object values in reverse order, you could write:

const stringify = require('json-stringify');

const obj = { d: 6, c: 5, b: [{ z: 3, y: 2, x: 1 }, 9], a: 10 };

const s = stringify(obj, function (a, b) {
	return a.value < b.value ? 1 : -1;
});

console.log(s);

which outputs:

{"d":6,"c":5,"b":[{"z":3,"y":2,"x":1},9],"a":10}

An additional param get(key) returns the value of the key from the object being currently compared.

space

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:

const obj = { b: 1, a: { foo: 'bar', and: [1, 2, 3] } };

const s = stringify(obj, { space: '  ' });

console.log(s);

which outputs:

{
  "a": {
    "and": [
      1,
      2,
      3
    ],
    "foo": "bar"
  },
  "b": 1
}

replacer

The replacer parameter is a function opts.replacer(key, value) that behaves the same as the replacer from the core JSON object.

install

With npm do:

npm install json-stringify

license

3.17.105

10 months ago

3.17.104

10 months ago

3.17.103

10 months ago

3.17.102

10 months ago

3.16.102

10 months ago

3.16.101

10 months ago

3.16.100

10 months ago

3.16.99

10 months ago

3.16.98

10 months ago

2.16.98

10 months ago

2.15.98

10 months ago

2.15.97

10 months ago

2.15.96

10 months ago

2.15.95

10 months ago

2.14.95

10 months ago

2.14.94

10 months ago

2.13.94

10 months ago

2.13.93

10 months ago

2.13.92

10 months ago

2.13.91

10 months ago

2.12.91

10 months ago

2.12.90

11 months ago

2.12.89

11 months ago

2.12.88

11 months ago

1.12.88

11 months ago

1.12.87

11 months ago

1.12.86

11 months ago

1.12.85

11 months ago

1.12.84

11 months ago

1.11.84

11 months ago

1.11.83

11 months ago

1.11.82

11 months ago

1.11.81

11 months ago

1.11.80

11 months ago

1.11.79

11 months ago

1.11.78

11 months ago

1.11.77

11 months ago

1.10.77

11 months ago

1.10.76

11 months ago

1.10.75

11 months ago

1.10.74

11 months ago

1.10.73

11 months ago

1.10.72

11 months ago

1.10.71

11 months ago

1.10.70

11 months ago

1.10.69

11 months ago

1.10.68

11 months ago

1.10.67

11 months ago

1.10.66

11 months ago

1.10.65

11 months ago

1.10.64

11 months ago

1.9.64

12 months ago

1.9.63

12 months ago

1.9.62

12 months ago

1.9.61

12 months ago

1.9.60

12 months ago

1.9.59

12 months ago

1.9.58

12 months ago

1.9.57

12 months ago

1.9.56

12 months ago

1.9.55

12 months ago

1.9.54

12 months ago

1.9.53

12 months ago

1.9.52

12 months ago

1.9.51

12 months ago

1.9.50

12 months ago

1.9.49

12 months ago

1.8.49

12 months ago

1.7.49

12 months ago

1.7.48

12 months ago

1.7.47

12 months ago

1.7.46

1 year ago

1.7.45

1 year ago

1.7.44

1 year ago

1.7.43

1 year ago

1.7.42

1 year ago

1.6.42

1 year ago

1.5.42

1 year ago

1.5.41

1 year ago

1.5.40

1 year ago

1.5.39

1 year ago

1.5.38

1 year ago

1.5.37

1 year ago

1.5.36

1 year ago

1.5.35

1 year ago

1.5.34

1 year ago

1.5.33

1 year ago

1.5.32

1 year ago

1.5.31

1 year ago

1.4.31

1 year ago

1.4.30

1 year ago

1.4.29

1 year ago

1.4.28

1 year ago

1.4.27

1 year ago

1.4.26

1 year ago

1.3.26

1 year ago

1.3.25

1 year ago

1.3.24

1 year ago

1.3.23

1 year ago

1.2.23

1 year ago

1.2.22

1 year ago

1.2.21

1 year ago

1.2.20

1 year ago

1.1.20

1 year ago

1.1.19

1 year ago

1.1.18

1 year ago

1.1.17

1 year ago

1.1.16

1 year ago

1.1.15

1 year ago

1.1.14

1 year ago

1.1.13

1 year ago

1.1.12

1 year ago

1.1.11

1 year ago

1.1.10

1 year ago

1.1.9

1 year ago

1.0.9

1 year ago

1.0.8

1 year ago

1.0.7

1 year ago

1.0.6

1 year ago

1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago