massive update, probably broken
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / flatted / cjs / index.js
1 'use strict';
2 /*! (c) 2020 Andrea Giammarchi */
3
4 const {parse: $parse, stringify: $stringify} = JSON;
5 const {keys} = Object;
6
7 const Primitive = String;   // it could be Number
8 const primitive = 'string'; // it could be 'number'
9
10 const ignore = {};
11 const object = 'object';
12
13 const noop = (_, value) => value;
14
15 const primitives = value => (
16   value instanceof Primitive ? Primitive(value) : value
17 );
18
19 const Primitives = (_, value) => (
20   typeof value === primitive ? new Primitive(value) : value
21 );
22
23 const revive = (input, parsed, output, $) => {
24   const lazy = [];
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({k, a: [input, parsed, 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   for (let {length} = lazy, i = 0; i < length; i++) {
42     const {k, a} = lazy[i];
43     output[k] = $.call(output, k, revive.apply(null, a));
44   }
45   return output;
46 };
47
48 const set = (known, input, value) => {
49   const index = Primitive(input.push(value) - 1);
50   known.set(value, index);
51   return index;
52 };
53
54 const parse = (text, reviver) => {
55   const input = $parse(text, Primitives).map(primitives);
56   const value = input[0];
57   const $ = reviver || noop;
58   const tmp = typeof value === object && value ?
59               revive(input, new Set, value, $) :
60               value;
61   return $.call({'': tmp}, '', tmp);
62 };
63 exports.parse = parse;
64
65 const stringify = (value, replacer, space) => {
66   const $ = replacer && typeof replacer === object ?
67             (k, v) => (k === '' || -1 < replacer.indexOf(k) ? v : void 0) :
68             (replacer || noop);
69   const known = new Map;
70   const input = [];
71   const output = [];
72   let i = +set(known, input, $.call({'': value}, '', value));
73   let firstRun = !i;
74   while (i < input.length) {
75     firstRun = true;
76     output[i] = $stringify(input[i++], replace, space);
77   }
78   return '[' + output.join(',') + ']';
79   function replace(key, value) {
80     if (firstRun) {
81       firstRun = !firstRun;
82       return value;
83     }
84     const after = $.call(this, key, value);
85     switch (typeof after) {
86       case object:
87         if (after === null) return after;
88       case primitive:
89         return known.get(after) || set(known, input, after);
90     }
91     return after;
92   }
93 };
94 exports.stringify = stringify;
95
96 const toJSON = any => $parse(stringify(any));
97 exports.toJSON = toJSON;
98 const fromJSON = any => parse($stringify(any));
99 exports.fromJSON = fromJSON;