source: imaps-frontend/node_modules/flatted/cjs/index.js

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

Update repo after prototype presentation

  • Property mode set to 100644
File size: 3.4 KB
RevLine 
[d565449]1'use strict';
2/// <reference types="../types/index.d.ts" />
3
4// (c) 2020-present Andrea Giammarchi
5
6const {parse: $parse, stringify: $stringify} = JSON;
7const {keys} = Object;
8
9const Primitive = String; // it could be Number
10const primitive = 'string'; // it could be 'number'
11
12const ignore = {};
13const object = 'object';
14
15const noop = (_, value) => value;
16
17const primitives = value => (
18 value instanceof Primitive ? Primitive(value) : value
19);
20
21const Primitives = (_, value) => (
22 typeof value === primitive ? new Primitive(value) : value
23);
24
25const 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
50const 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 */
62const 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};
71exports.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 */
80const 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};
109exports.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 */
116const toJSON = value => $parse(stringify(value));
117exports.toJSON = toJSON;
118
119/**
120 * Converts a previously serialized object with recursion into a recursive one.
121 * @param {any} value
122 * @returns {any}
123 */
124const fromJSON = value => parse($stringify(value));
125exports.fromJSON = fromJSON;
Note: See TracBrowser for help on using the repository browser.