index.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. self.Flatted = (function (exports) {
  2. 'use strict';
  3. function _typeof(obj) {
  4. "@babel/helpers - typeof";
  5. return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) {
  6. return typeof obj;
  7. } : function (obj) {
  8. return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
  9. }, _typeof(obj);
  10. }
  11. /*! (c) 2020 Andrea Giammarchi */
  12. var $parse = JSON.parse,
  13. $stringify = JSON.stringify;
  14. var keys = Object.keys;
  15. var Primitive = String; // it could be Number
  16. var primitive = 'string'; // it could be 'number'
  17. var ignore = {};
  18. var object = 'object';
  19. var noop = function noop(_, value) {
  20. return value;
  21. };
  22. var primitives = function primitives(value) {
  23. return value instanceof Primitive ? Primitive(value) : value;
  24. };
  25. var Primitives = function Primitives(_, value) {
  26. return _typeof(value) === primitive ? new Primitive(value) : value;
  27. };
  28. var revive = function revive(input, parsed, output, $) {
  29. var lazy = [];
  30. for (var ke = keys(output), length = ke.length, y = 0; y < length; y++) {
  31. var k = ke[y];
  32. var value = output[k];
  33. if (value instanceof Primitive) {
  34. var tmp = input[value];
  35. if (_typeof(tmp) === object && !parsed.has(tmp)) {
  36. parsed.add(tmp);
  37. output[k] = ignore;
  38. lazy.push({
  39. k: k,
  40. a: [input, parsed, tmp, $]
  41. });
  42. } else output[k] = $.call(output, k, tmp);
  43. } else if (output[k] !== ignore) output[k] = $.call(output, k, value);
  44. }
  45. for (var _length = lazy.length, i = 0; i < _length; i++) {
  46. var _lazy$i = lazy[i],
  47. _k = _lazy$i.k,
  48. a = _lazy$i.a;
  49. output[_k] = $.call(output, _k, revive.apply(null, a));
  50. }
  51. return output;
  52. };
  53. var set = function set(known, input, value) {
  54. var index = Primitive(input.push(value) - 1);
  55. known.set(value, index);
  56. return index;
  57. };
  58. var parse = function parse(text, reviver) {
  59. var input = $parse(text, Primitives).map(primitives);
  60. var value = input[0];
  61. var $ = reviver || noop;
  62. var tmp = _typeof(value) === object && value ? revive(input, new Set(), value, $) : value;
  63. return $.call({
  64. '': tmp
  65. }, '', tmp);
  66. };
  67. var stringify = function stringify(value, replacer, space) {
  68. var $ = replacer && _typeof(replacer) === object ? function (k, v) {
  69. return k === '' || -1 < replacer.indexOf(k) ? v : void 0;
  70. } : replacer || noop;
  71. var known = new Map();
  72. var input = [];
  73. var output = [];
  74. var i = +set(known, input, $.call({
  75. '': value
  76. }, '', value));
  77. var 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. var 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. var toJSON = function toJSON(any) {
  99. return $parse(stringify(any));
  100. };
  101. var fromJSON = function fromJSON(any) {
  102. return parse($stringify(any));
  103. };
  104. exports.fromJSON = fromJSON;
  105. exports.parse = parse;
  106. exports.stringify = stringify;
  107. exports.toJSON = toJSON;
  108. return exports;
  109. })({});