index.js 2.6 KB

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