[6a3a178] | 1 | var Flatted = (function (Primitive, primitive) {
|
---|
| 2 |
|
---|
| 3 | /*!
|
---|
| 4 | * ISC License
|
---|
| 5 | *
|
---|
| 6 | * Copyright (c) 2018, Andrea Giammarchi, @WebReflection
|
---|
| 7 | *
|
---|
| 8 | * Permission to use, copy, modify, and/or distribute this software for any
|
---|
| 9 | * purpose with or without fee is hereby granted, provided that the above
|
---|
| 10 | * copyright notice and this permission notice appear in all copies.
|
---|
| 11 | *
|
---|
| 12 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
---|
| 13 | * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
---|
| 14 | * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
---|
| 15 | * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
---|
| 16 | * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
|
---|
| 17 | * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
---|
| 18 | * PERFORMANCE OF THIS SOFTWARE.
|
---|
| 19 | */
|
---|
| 20 |
|
---|
| 21 | var Flatted = {
|
---|
| 22 |
|
---|
| 23 | parse: function parse(text, reviver) {
|
---|
| 24 | var input = JSON.parse(text, Primitives).map(primitives);
|
---|
| 25 | var value = input[0];
|
---|
| 26 | var $ = reviver || noop;
|
---|
| 27 | var tmp = typeof value === 'object' && value ?
|
---|
| 28 | revive(input, new Set, value, $) :
|
---|
| 29 | value;
|
---|
| 30 | return $.call({'': tmp}, '', tmp);
|
---|
| 31 | },
|
---|
| 32 |
|
---|
| 33 | stringify: function stringify(value, replacer, space) {
|
---|
| 34 | for (var
|
---|
| 35 | firstRun,
|
---|
| 36 | known = new Map,
|
---|
| 37 | input = [],
|
---|
| 38 | output = [],
|
---|
| 39 | $ = replacer && typeof replacer === typeof input ?
|
---|
| 40 | function (k, v) {
|
---|
| 41 | if (k === '' || -1 < replacer.indexOf(k)) return v;
|
---|
| 42 | } :
|
---|
| 43 | (replacer || noop),
|
---|
| 44 | i = +set(known, input, $.call({'': value}, '', value)),
|
---|
| 45 | replace = function (key, value) {
|
---|
| 46 | if (firstRun) {
|
---|
| 47 | firstRun = !firstRun;
|
---|
| 48 | return value;
|
---|
| 49 | }
|
---|
| 50 | var after = $.call(this, key, value);
|
---|
| 51 | switch (typeof after) {
|
---|
| 52 | case 'object':
|
---|
| 53 | if (after === null) return after;
|
---|
| 54 | case primitive:
|
---|
| 55 | return known.get(after) || set(known, input, after);
|
---|
| 56 | }
|
---|
| 57 | return after;
|
---|
| 58 | };
|
---|
| 59 | i < input.length; i++
|
---|
| 60 | ) {
|
---|
| 61 | firstRun = true;
|
---|
| 62 | output[i] = JSON.stringify(input[i], replace, space);
|
---|
| 63 | }
|
---|
| 64 | return '[' + output.join(',') + ']';
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | };
|
---|
| 68 |
|
---|
| 69 | return Flatted;
|
---|
| 70 |
|
---|
| 71 | function noop(key, value) {
|
---|
| 72 | return value;
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | function revive(input, parsed, output, $) {
|
---|
| 76 | return Object.keys(output).reduce(
|
---|
| 77 | function (output, key) {
|
---|
| 78 | var value = output[key];
|
---|
| 79 | if (value instanceof Primitive) {
|
---|
| 80 | var tmp = input[value];
|
---|
| 81 | if (typeof tmp === 'object' && !parsed.has(tmp)) {
|
---|
| 82 | parsed.add(tmp);
|
---|
| 83 | output[key] = $.call(output, key, revive(input, parsed, tmp, $));
|
---|
| 84 | } else {
|
---|
| 85 | output[key] = $.call(output, key, tmp);
|
---|
| 86 | }
|
---|
| 87 | } else
|
---|
| 88 | output[key] = $.call(output, key, value);
|
---|
| 89 | return output;
|
---|
| 90 | },
|
---|
| 91 | output
|
---|
| 92 | );
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | function set(known, input, value) {
|
---|
| 96 | var index = Primitive(input.push(value) - 1);
|
---|
| 97 | known.set(value, index);
|
---|
| 98 | return index;
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | // the two kinds of primitives
|
---|
| 102 | // 1. the real one
|
---|
| 103 | // 2. the wrapped one
|
---|
| 104 |
|
---|
| 105 | function primitives(value) {
|
---|
| 106 | return value instanceof Primitive ? Primitive(value) : value;
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | function Primitives(key, value) {
|
---|
| 110 | return typeof value === primitive ? new Primitive(value) : value;
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | }(String, 'string'));
|
---|
| 114 | export default Flatted;
|
---|
| 115 | export var parse = Flatted.parse;
|
---|
| 116 | export var stringify = Flatted.stringify;
|
---|