source: imaps-frontend/node_modules/flatted/index.js@ d565449

main
Last change on this file since d565449 was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 4.3 KB
Line 
1self.Flatted = (function (exports) {
2 'use strict';
3
4 function _typeof(o) {
5 "@babel/helpers - typeof";
6
7 return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) {
8 return typeof o;
9 } : function (o) {
10 return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o;
11 }, _typeof(o);
12 }
13
14 /// <reference types="../types/index.d.ts" />
15
16 // (c) 2020-present Andrea Giammarchi
17
18 var $parse = JSON.parse,
19 $stringify = JSON.stringify;
20 var keys = Object.keys;
21 var Primitive = String; // it could be Number
22 var primitive = 'string'; // it could be 'number'
23
24 var ignore = {};
25 var object = 'object';
26 var noop = function noop(_, value) {
27 return value;
28 };
29 var primitives = function primitives(value) {
30 return value instanceof Primitive ? Primitive(value) : value;
31 };
32 var Primitives = function Primitives(_, value) {
33 return _typeof(value) === primitive ? new Primitive(value) : value;
34 };
35 var revive = function revive(input, parsed, output, $) {
36 var lazy = [];
37 for (var ke = keys(output), length = ke.length, y = 0; y < length; y++) {
38 var k = ke[y];
39 var value = output[k];
40 if (value instanceof Primitive) {
41 var tmp = input[value];
42 if (_typeof(tmp) === object && !parsed.has(tmp)) {
43 parsed.add(tmp);
44 output[k] = ignore;
45 lazy.push({
46 k: k,
47 a: [input, parsed, tmp, $]
48 });
49 } else output[k] = $.call(output, k, tmp);
50 } else if (output[k] !== ignore) output[k] = $.call(output, k, value);
51 }
52 for (var _length = lazy.length, i = 0; i < _length; i++) {
53 var _lazy$i = lazy[i],
54 _k = _lazy$i.k,
55 a = _lazy$i.a;
56 output[_k] = $.call(output, _k, revive.apply(null, a));
57 }
58 return output;
59 };
60 var set = function set(known, input, value) {
61 var index = Primitive(input.push(value) - 1);
62 known.set(value, index);
63 return index;
64 };
65
66 /**
67 * Converts a specialized flatted string into a JS value.
68 * @param {string} text
69 * @param {((this: any, key: string, value: any) => any) | undefined): any} [reviver]
70 * @returns {any}
71 */
72 var parse = function parse(text, reviver) {
73 var input = $parse(text, Primitives).map(primitives);
74 var value = input[0];
75 var $ = reviver || noop;
76 var tmp = _typeof(value) === object && value ? revive(input, new Set(), value, $) : value;
77 return $.call({
78 '': tmp
79 }, '', tmp);
80 };
81
82 /**
83 * Converts a JS value into a specialized flatted string.
84 * @param {any} value
85 * @param {((this: any, key: string, value: any) => any) | (string | number)[] | null | undefined} [replacer]
86 * @param {string | number | undefined} [space]
87 * @returns {string}
88 */
89 var stringify = function stringify(value, replacer, space) {
90 var $ = replacer && _typeof(replacer) === object ? function (k, v) {
91 return k === '' || -1 < replacer.indexOf(k) ? v : void 0;
92 } : replacer || noop;
93 var known = new Map();
94 var input = [];
95 var output = [];
96 var i = +set(known, input, $.call({
97 '': value
98 }, '', value));
99 var firstRun = !i;
100 while (i < input.length) {
101 firstRun = true;
102 output[i] = $stringify(input[i++], replace, space);
103 }
104 return '[' + output.join(',') + ']';
105 function replace(key, value) {
106 if (firstRun) {
107 firstRun = !firstRun;
108 return value;
109 }
110 var after = $.call(this, key, value);
111 switch (_typeof(after)) {
112 case object:
113 if (after === null) return after;
114 case primitive:
115 return known.get(after) || set(known, input, after);
116 }
117 return after;
118 }
119 };
120
121 /**
122 * Converts a generic value into a JSON serializable object without losing recursion.
123 * @param {any} value
124 * @returns {any}
125 */
126 var toJSON = function toJSON(value) {
127 return $parse(stringify(value));
128 };
129
130 /**
131 * Converts a previously serialized object with recursion into a recursive one.
132 * @param {any} value
133 * @returns {any}
134 */
135 var fromJSON = function fromJSON(value) {
136 return parse($stringify(value));
137 };
138
139 exports.fromJSON = fromJSON;
140 exports.parse = parse;
141 exports.stringify = stringify;
142 exports.toJSON = toJSON;
143
144 return exports;
145
146})({});
Note: See TracBrowser for help on using the repository browser.