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