Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

3 anos atrás
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. # json-stable-stringify
  2. deterministic version of `JSON.stringify()` so you can get a consistent hash
  3. from stringified results
  4. You can also pass in a custom comparison function.
  5. [![browser support](https://ci.testling.com/substack/json-stable-stringify.png)](https://ci.testling.com/substack/json-stable-stringify)
  6. [![build status](https://secure.travis-ci.org/substack/json-stable-stringify.png)](http://travis-ci.org/substack/json-stable-stringify)
  7. # example
  8. ``` js
  9. var stringify = require('json-stable-stringify');
  10. var obj = { c: 8, b: [{z:6,y:5,x:4},7], a: 3 };
  11. console.log(stringify(obj));
  12. ```
  13. output:
  14. ```
  15. {"a":3,"b":[{"x":4,"y":5,"z":6},7],"c":8}
  16. ```
  17. # methods
  18. ``` js
  19. var stringify = require('json-stable-stringify')
  20. ```
  21. ## var str = stringify(obj, opts)
  22. Return a deterministic stringified string `str` from the object `obj`.
  23. ## options
  24. ### cmp
  25. If `opts` is given, you can supply an `opts.cmp` to have a custom comparison
  26. function for object keys. Your function `opts.cmp` is called with these
  27. parameters:
  28. ``` js
  29. opts.cmp({ key: akey, value: avalue }, { key: bkey, value: bvalue })
  30. ```
  31. For example, to sort on the object key names in reverse order you could write:
  32. ``` js
  33. var stringify = require('json-stable-stringify');
  34. var obj = { c: 8, b: [{z:6,y:5,x:4},7], a: 3 };
  35. var s = stringify(obj, function (a, b) {
  36. return a.key < b.key ? 1 : -1;
  37. });
  38. console.log(s);
  39. ```
  40. which results in the output string:
  41. ```
  42. {"c":8,"b":[{"z":6,"y":5,"x":4},7],"a":3}
  43. ```
  44. Or if you wanted to sort on the object values in reverse order, you could write:
  45. ```
  46. var stringify = require('json-stable-stringify');
  47. var obj = { d: 6, c: 5, b: [{z:3,y:2,x:1},9], a: 10 };
  48. var s = stringify(obj, function (a, b) {
  49. return a.value < b.value ? 1 : -1;
  50. });
  51. console.log(s);
  52. ```
  53. which outputs:
  54. ```
  55. {"d":6,"c":5,"b":[{"z":3,"y":2,"x":1},9],"a":10}
  56. ```
  57. ### space
  58. If you specify `opts.space`, it will indent the output for pretty-printing.
  59. Valid values are strings (e.g. `{space: \t}`) or a number of spaces
  60. (`{space: 3}`).
  61. For example:
  62. ```js
  63. var obj = { b: 1, a: { foo: 'bar', and: [1, 2, 3] } };
  64. var s = stringify(obj, { space: ' ' });
  65. console.log(s);
  66. ```
  67. which outputs:
  68. ```
  69. {
  70. "a": {
  71. "and": [
  72. 1,
  73. 2,
  74. 3
  75. ],
  76. "foo": "bar"
  77. },
  78. "b": 1
  79. }
  80. ```
  81. ### replacer
  82. The replacer parameter is a function `opts.replacer(key, value)` that behaves
  83. the same as the replacer
  84. [from the core JSON object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_native_JSON#The_replacer_parameter).
  85. # install
  86. With [npm](https://npmjs.org) do:
  87. ```
  88. npm install json-stable-stringify
  89. ```
  90. # license
  91. MIT