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