index.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. 'use strict';
  2. /// <reference types="../types/index.d.ts" />
  3. // (c) 2020-present Andrea Giammarchi
  4. const {parse: $parse, stringify: $stringify} = JSON;
  5. const {keys} = Object;
  6. const Primitive = String; // it could be Number
  7. const primitive = 'string'; // it could be 'number'
  8. const ignore = {};
  9. const object = 'object';
  10. const noop = (_, value) => value;
  11. const primitives = value => (
  12. value instanceof Primitive ? Primitive(value) : value
  13. );
  14. const Primitives = (_, value) => (
  15. typeof value === primitive ? new Primitive(value) : value
  16. );
  17. const revive = (input, parsed, output, $) => {
  18. const lazy = [];
  19. for (let ke = keys(output), {length} = ke, y = 0; y < length; y++) {
  20. const k = ke[y];
  21. const value = output[k];
  22. if (value instanceof Primitive) {
  23. const tmp = input[value];
  24. if (typeof tmp === object && !parsed.has(tmp)) {
  25. parsed.add(tmp);
  26. output[k] = ignore;
  27. lazy.push({k, a: [input, parsed, tmp, $]});
  28. }
  29. else
  30. output[k] = $.call(output, k, tmp);
  31. }
  32. else if (output[k] !== ignore)
  33. output[k] = $.call(output, k, value);
  34. }
  35. for (let {length} = lazy, i = 0; i < length; i++) {
  36. const {k, a} = lazy[i];
  37. output[k] = $.call(output, k, revive.apply(null, a));
  38. }
  39. return output;
  40. };
  41. const set = (known, input, value) => {
  42. const index = Primitive(input.push(value) - 1);
  43. known.set(value, index);
  44. return index;
  45. };
  46. /**
  47. * Converts a specialized flatted string into a JS value.
  48. * @param {string} text
  49. * @param {((this: any, key: string, value: any) => any) | undefined): any} [reviver]
  50. * @returns {any}
  51. */
  52. const parse = (text, reviver) => {
  53. const input = $parse(text, Primitives).map(primitives);
  54. const value = input[0];
  55. const $ = reviver || noop;
  56. const tmp = typeof value === object && value ?
  57. revive(input, new Set, value, $) :
  58. value;
  59. return $.call({'': tmp}, '', tmp);
  60. };
  61. exports.parse = parse;
  62. /**
  63. * Converts a JS value into a specialized flatted string.
  64. * @param {any} value
  65. * @param {((this: any, key: string, value: any) => any) | (string | number)[] | null | undefined} [replacer]
  66. * @param {string | number | undefined} [space]
  67. * @returns {string}
  68. */
  69. const stringify = (value, replacer, space) => {
  70. const $ = replacer && typeof replacer === object ?
  71. (k, v) => (k === '' || -1 < replacer.indexOf(k) ? v : void 0) :
  72. (replacer || noop);
  73. const known = new Map;
  74. const input = [];
  75. const output = [];
  76. let i = +set(known, input, $.call({'': value}, '', value));
  77. let firstRun = !i;
  78. while (i < input.length) {
  79. firstRun = true;
  80. output[i] = $stringify(input[i++], replace, space);
  81. }
  82. return '[' + output.join(',') + ']';
  83. function replace(key, value) {
  84. if (firstRun) {
  85. firstRun = !firstRun;
  86. return value;
  87. }
  88. const after = $.call(this, key, value);
  89. switch (typeof after) {
  90. case object:
  91. if (after === null) return after;
  92. case primitive:
  93. return known.get(after) || set(known, input, after);
  94. }
  95. return after;
  96. }
  97. };
  98. exports.stringify = stringify;
  99. /**
  100. * Converts a generic value into a JSON serializable object without losing recursion.
  101. * @param {any} value
  102. * @returns {any}
  103. */
  104. const toJSON = value => $parse(stringify(value));
  105. exports.toJSON = toJSON;
  106. /**
  107. * Converts a previously serialized object with recursion into a recursive one.
  108. * @param {any} value
  109. * @returns {any}
  110. */
  111. const fromJSON = value => parse($stringify(value));
  112. exports.fromJSON = fromJSON;