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