package/package.json000644 0000003034 3560116604 011546 0ustar00000000 000000 { "name": "flatted", "version": "2.0.1", "description": "A super light and fast circular JSON parser.", "unpkg": "min.js", "main": "cjs/index.js", "module": "esm/index.js", "types": "types.d.ts", "scripts": { "bench": "node test/bench.js", "build": "npm run cjs && npm test && npm run esm && npm run min && npm run size", "coveralls": "cat ./coverage/lcov.info | coveralls", "cjs": "cp index.js cjs/index.js; echo 'module.exports = Flatted;' >> cjs/index.js", "esm": "cp index.js esm/index.js; echo 'export default Flatted;' >> esm/index.js; echo 'export var parse = Flatted.parse;' >> esm/index.js; echo 'export var stringify = Flatted.stringify;' >> esm/index.js", "min": "echo '/*! (c) 2018, Andrea Giammarchi, (ISC) */'>min.js && uglifyjs index.js --support-ie8 -c -m >> min.js", "size": "cat index.js | wc -c;cat min.js | wc -c;gzip -c9 min.js | wc -c;cat min.js | brotli | wc -c", "test": "istanbul cover test/index.js" }, "repository": { "type": "git", "url": "git+https://github.com/WebReflection/flatted.git" }, "keywords": [ "circular", "JSON", "fast", "parser", "minimal" ], "author": "Andrea Giammarchi", "license": "ISC", "bugs": { "url": "https://github.com/WebReflection/flatted/issues" }, "homepage": "https://github.com/WebReflection/flatted#readme", "devDependencies": { "circular-json": "latest", "circular-json-es6": "latest", "coveralls": "latest", "istanbul": "latest", "jsan": "latest", "uglify-js": "latest" } } package/index.js000644 0000006771 3560116604 010740 0ustar00000000 000000 var Flatted = (function (Primitive, primitive) { /*! * ISC License * * Copyright (c) 2018, Andrea Giammarchi, @WebReflection * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR * PERFORMANCE OF THIS SOFTWARE. */ var Flatted = { parse: function parse(text, reviver) { var input = JSON.parse(text, Primitives).map(primitives); var value = input[0]; var $ = reviver || noop; var tmp = typeof value === 'object' && value ? revive(input, new Set, value, $) : value; return $.call({'': tmp}, '', tmp); }, stringify: function stringify(value, replacer, space) { for (var firstRun, known = new Map, input = [], output = [], $ = replacer && typeof replacer === typeof input ? function (k, v) { if (k === '' || -1 < replacer.indexOf(k)) return v; } : (replacer || noop), i = +set(known, input, $.call({'': value}, '', value)), replace = function (key, value) { if (firstRun) { firstRun = !firstRun; return value; // this was invoking twice each root object // return i < 1 ? value : $.call(this, key, value); } var after = $.call(this, key, value); switch (typeof after) { case 'object': if (after === null) return after; case primitive: return known.get(after) || set(known, input, after); } return after; }; i < input.length; i++ ) { firstRun = true; output[i] = JSON.stringify(input[i], replace, space); } return '[' + output.join(',') + ']'; } }; return Flatted; function noop(key, value) { return value; } function revive(input, parsed, output, $) { return Object.keys(output).reduce( function (output, key) { var value = output[key]; if (value instanceof Primitive) { var tmp = input[value]; if (typeof tmp === 'object' && !parsed.has(tmp)) { parsed.add(tmp); output[key] = $.call(output, key, revive(input, parsed, tmp, $)); } else { output[key] = $.call(output, key, tmp); } } else output[key] = $.call(output, key, value); return output; }, output ); } function set(known, input, value) { var index = Primitive(input.push(value) - 1); known.set(value, index); return index; } // the two kinds of primitives // 1. the real one // 2. the wrapped one function primitives(value) { return value instanceof Primitive ? Primitive(value) : value; } function Primitives(key, value) { return typeof value === primitive ? new Primitive(value) : value; } }(String, 'string')); package/LICENSE000644 0000001375 3560116604 010273 0ustar00000000 000000 ISC License Copyright (c) 2018, Andrea Giammarchi, @WebReflection Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. package/min.js000644 0000002000 3560116604 010371 0ustar00000000 000000 /*! (c) 2018, Andrea Giammarchi, (ISC) */ var Flatted=function(a,l){return{parse:function(n,t){var e=JSON.parse(n,i).map(f),r=e[0],u=t||s,c="object"==typeof r&&r?function u(c,f,n,i){return Object.keys(n).reduce(function(n,t){var e=n[t];if(e instanceof a){var r=c[e];"object"!=typeof r||f.has(r)?n[t]=i.call(n,t,r):(f.add(r),n[t]=i.call(n,t,u(c,f,r,i)))}else n[t]=i.call(n,t,e);return n},n)}(e,new Set,r,u):r;return u.call({"":c},"",c)},stringify:function(n,e,t){function r(n,t){if(u)return u=!u,t;var e=a.call(this,n,t);switch(typeof e){case"object":if(null===e)return e;case l:return c.get(e)||p(c,f,e)}return e}for(var u,c=new Map,f=[],i=[],a=e&&typeof e==typeof f?function(n,t){if(""===n||-1`*` represented as string to avoid conflicts with numbers ```js // logic example var a = [{one: 1}, {two: '2'}]; a[0].a = a; // a is the main object, will be at index '0' // {one: 1} is the second object, index '1' // {two: '2'} the third, in '2', and it has a string // which will be found at index '3' Flatted.stringify(a); // [["1","2"],{"one":1,"a":"0"},{"two":"3"},"2"] // a[one,two] {one: 1, a} {two: '2'} '2' ``` package/SPECS.md000644 0000006773 3560116604 010474 0ustar00000000 000000 # Flatted Specifications This document describes operations performed to produce, or parse, the flatted output. ## stringify(any) => flattedString The output is always an `Array` that contains at index `0` the given value. If the value is an `Array` or an `Object`, per each property value passed through the callback, return the value as is if it's not an `Array`, an `Object`, or a `string`. In case it's an `Array`, an `Object`, or a `string`, return the index as `string`, associated through a `Map`. Giving the following example: ```js flatted.stringify('a'); // ["a"] flatted.stringify(['a']); // [["1"],"a"] flatted.stringify(['a', 1, 'b']); // [["1",1,"2"],"a","b"] ``` There is an `input` containing `[array, "a", "b"]`, where the `array` has indexes `"1"` and `"2"` as strings, indexes that point respectively at `"a"` and `"b"` within the input `[array, "a", "b"]`. The exact same happens for objects. ```js flatted.stringify('a'); // ["a"] flatted.stringify({a: 'a'}); // [{"a":"1"},"a"] flatted.stringify({a: 'a', n: 1, b: 'b'}); // [{"a":"1","n":1,"b":"2"},"a","b"] ``` Every object, string, or array, encountered during serialization will be stored once as stringified index. ```js // per each property/value of the object/array if (any == null || !/object|string/.test(typeof any)) return any; if (!map.has(any)) { const index = String(arr.length); arr.push(any); map.set(any, index); } return map.get(any); ``` This, performed before going through all properties, grants unique indexes per reference. The stringified indexes ensure there won't be conflicts with regularly stored numbers. ## parse(flattedString) => any Everything that is a `string` is wrapped as `new String`, but strings in the array, from index `1` on, is kept as regular `string`. ```js const input = JSON.parse('[{"a":"1"},"b"]', Strings).map(strings); // convert strings primitives into String instances function Strings(key, value) { return typeof value === 'string' ? new String(value) : value; } // converts String instances into strings primitives function strings(value) { return value instanceof String ? String(value) : value; } ``` The `input` array will have a regular `string` at index `1`, but its object at index `0` will have an `instanceof String` as `.a` property. That is the key to place back values from the rest of the array, so that per each property of the object at index `0`, if the value is an `instanceof` String, something not serializable via JSON, it means it can be used to retrieve the position of its value from the `input` array. If such `value` is an object and it hasn't been parsed yet, add it as parsed and go through all its properties/values. ```js // outside any loop ... const parsed = new Set; // ... per each property/value ... if (value instanceof Primitive) { const tmp = input[parseInt(value)]; if (typeof tmp === 'object' && !parsed.has(tmp)) { parsed.add(tmp); output[key] = tmp; if (typeof tmp === 'object' && tmp != null) { // perform this same logic per // each nested property/value ... } } else { output[key] = tmp; } } else output[key] = tmp; ``` As summary, the whole logic is based on polluting the de-serialization with a kind of variable that is unexpected, hence secure to use as directive to retrieve an index with a value. The usage of a `Map` and a `Set` to flag known references/strings as visited/stored makes **flatted** a rock solid, fast, and compact, solution. package/types.d.ts000644 0000000734 3560116604 011222 0ustar00000000 000000 /** * Fast and minimal circular JSON parser. * logic example ```js var a = [{one: 1}, {two: '2'}]; a[0].a = a; // a is the main object, will be at index '0' // {one: 1} is the second object, index '1' // {two: '2'} the third, in '2', and it has a string // which will be found at index '3' Flatted.stringify(a); // [["1","2"],{"one":1,"a":"0"},{"two":"3"},"2"] // a[one,two] {one: 1, a} {two: '2'} '2' ``` */ declare const Flatted: typeof JSON; export = Flatted; package/.github/FUNDING.yml000644 0000000562 3560116604 012440 0ustar00000000 000000 # These are supported funding model platforms github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2] patreon: # not working due missing www. open_collective: # ko_fi: # Replace with a single Ko-fi username tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel custom: https://www.patreon.com/webreflection package/cjs/index.js000644 0000007023 3560116604 011506 0ustar00000000 000000 var Flatted = (function (Primitive, primitive) { /*! * ISC License * * Copyright (c) 2018, Andrea Giammarchi, @WebReflection * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR * PERFORMANCE OF THIS SOFTWARE. */ var Flatted = { parse: function parse(text, reviver) { var input = JSON.parse(text, Primitives).map(primitives); var value = input[0]; var $ = reviver || noop; var tmp = typeof value === 'object' && value ? revive(input, new Set, value, $) : value; return $.call({'': tmp}, '', tmp); }, stringify: function stringify(value, replacer, space) { for (var firstRun, known = new Map, input = [], output = [], $ = replacer && typeof replacer === typeof input ? function (k, v) { if (k === '' || -1 < replacer.indexOf(k)) return v; } : (replacer || noop), i = +set(known, input, $.call({'': value}, '', value)), replace = function (key, value) { if (firstRun) { firstRun = !firstRun; return value; // this was invoking twice each root object // return i < 1 ? value : $.call(this, key, value); } var after = $.call(this, key, value); switch (typeof after) { case 'object': if (after === null) return after; case primitive: return known.get(after) || set(known, input, after); } return after; }; i < input.length; i++ ) { firstRun = true; output[i] = JSON.stringify(input[i], replace, space); } return '[' + output.join(',') + ']'; } }; return Flatted; function noop(key, value) { return value; } function revive(input, parsed, output, $) { return Object.keys(output).reduce( function (output, key) { var value = output[key]; if (value instanceof Primitive) { var tmp = input[value]; if (typeof tmp === 'object' && !parsed.has(tmp)) { parsed.add(tmp); output[key] = $.call(output, key, revive(input, parsed, tmp, $)); } else { output[key] = $.call(output, key, tmp); } } else output[key] = $.call(output, key, value); return output; }, output ); } function set(known, input, value) { var index = Primitive(input.push(value) - 1); known.set(value, index); return index; } // the two kinds of primitives // 1. the real one // 2. the wrapped one function primitives(value) { return value instanceof Primitive ? Primitive(value) : value; } function Primitives(key, value) { return typeof value === primitive ? new Primitive(value) : value; } }(String, 'string')); module.exports = Flatted; package/esm/index.js000644 0000007135 3560116604 011517 0ustar00000000 000000 var Flatted = (function (Primitive, primitive) { /*! * ISC License * * Copyright (c) 2018, Andrea Giammarchi, @WebReflection * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR * PERFORMANCE OF THIS SOFTWARE. */ var Flatted = { parse: function parse(text, reviver) { var input = JSON.parse(text, Primitives).map(primitives); var value = input[0]; var $ = reviver || noop; var tmp = typeof value === 'object' && value ? revive(input, new Set, value, $) : value; return $.call({'': tmp}, '', tmp); }, stringify: function stringify(value, replacer, space) { for (var firstRun, known = new Map, input = [], output = [], $ = replacer && typeof replacer === typeof input ? function (k, v) { if (k === '' || -1 < replacer.indexOf(k)) return v; } : (replacer || noop), i = +set(known, input, $.call({'': value}, '', value)), replace = function (key, value) { if (firstRun) { firstRun = !firstRun; return value; // this was invoking twice each root object // return i < 1 ? value : $.call(this, key, value); } var after = $.call(this, key, value); switch (typeof after) { case 'object': if (after === null) return after; case primitive: return known.get(after) || set(known, input, after); } return after; }; i < input.length; i++ ) { firstRun = true; output[i] = JSON.stringify(input[i], replace, space); } return '[' + output.join(',') + ']'; } }; return Flatted; function noop(key, value) { return value; } function revive(input, parsed, output, $) { return Object.keys(output).reduce( function (output, key) { var value = output[key]; if (value instanceof Primitive) { var tmp = input[value]; if (typeof tmp === 'object' && !parsed.has(tmp)) { parsed.add(tmp); output[key] = $.call(output, key, revive(input, parsed, tmp, $)); } else { output[key] = $.call(output, key, tmp); } } else output[key] = $.call(output, key, value); return output; }, output ); } function set(known, input, value) { var index = Primitive(input.push(value) - 1); known.set(value, index); return index; } // the two kinds of primitives // 1. the real one // 2. the wrapped one function primitives(value) { return value instanceof Primitive ? Primitive(value) : value; } function Primitives(key, value) { return typeof value === primitive ? new Primitive(value) : value; } }(String, 'string')); export default Flatted; export var parse = Flatted.parse; export var stringify = Flatted.stringify;