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