index.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. 'use strict';
  2. /*! (c) 2020 Andrea Giammarchi */
  3. const {parse: $parse, stringify: $stringify} = JSON;
  4. const {keys} = Object;
  5. const Primitive = String; // it could be Number
  6. const primitive = 'string'; // it could be 'number'
  7. const ignore = {};
  8. const object = 'object';
  9. const noop = (_, value) => value;
  10. const primitives = value => (
  11. value instanceof Primitive ? Primitive(value) : value
  12. );
  13. const Primitives = (_, value) => (
  14. typeof value === primitive ? new Primitive(value) : value
  15. );
  16. const revive = (input, parsed, output, $) => {
  17. const lazy = [];
  18. for (let ke = keys(output), {length} = ke, y = 0; y < length; y++) {
  19. const k = ke[y];
  20. const value = output[k];
  21. if (value instanceof Primitive) {
  22. const tmp = input[value];
  23. if (typeof tmp === object && !parsed.has(tmp)) {
  24. parsed.add(tmp);
  25. output[k] = ignore;
  26. lazy.push({k, a: [input, parsed, tmp, $]});
  27. }
  28. else
  29. output[k] = $.call(output, k, tmp);
  30. }
  31. else if (output[k] !== ignore)
  32. output[k] = $.call(output, k, value);
  33. }
  34. for (let {length} = lazy, i = 0; i < length; i++) {
  35. const {k, a} = lazy[i];
  36. output[k] = $.call(output, k, revive.apply(null, a));
  37. }
  38. return output;
  39. };
  40. const set = (known, input, value) => {
  41. const index = Primitive(input.push(value) - 1);
  42. known.set(value, index);
  43. return index;
  44. };
  45. const parse = (text, reviver) => {
  46. const input = $parse(text, Primitives).map(primitives);
  47. const value = input[0];
  48. const $ = reviver || noop;
  49. const tmp = typeof value === object && value ?
  50. revive(input, new Set, value, $) :
  51. value;
  52. return $.call({'': tmp}, '', tmp);
  53. };
  54. exports.parse = parse;
  55. const stringify = (value, replacer, space) => {
  56. const $ = replacer && typeof replacer === object ?
  57. (k, v) => (k === '' || -1 < replacer.indexOf(k) ? v : void 0) :
  58. (replacer || noop);
  59. const known = new Map;
  60. const input = [];
  61. const output = [];
  62. let i = +set(known, input, $.call({'': value}, '', value));
  63. let firstRun = !i;
  64. while (i < input.length) {
  65. firstRun = true;
  66. output[i] = $stringify(input[i++], replace, space);
  67. }
  68. return '[' + output.join(',') + ']';
  69. function replace(key, value) {
  70. if (firstRun) {
  71. firstRun = !firstRun;
  72. return value;
  73. }
  74. const after = $.call(this, key, value);
  75. switch (typeof after) {
  76. case object:
  77. if (after === null) return after;
  78. case primitive:
  79. return known.get(after) || set(known, input, after);
  80. }
  81. return after;
  82. }
  83. };
  84. exports.stringify = stringify;