save code
This commit is contained in:
+15
@@ -0,0 +1,15 @@
|
||||
ISC License
|
||||
|
||||
Copyright (c) 2018-2020, 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.
|
||||
+125
@@ -0,0 +1,125 @@
|
||||
'use strict';
|
||||
/// <reference types="../types/index.d.ts" />
|
||||
|
||||
// (c) 2020-present Andrea Giammarchi
|
||||
|
||||
const {parse: $parse, stringify: $stringify} = JSON;
|
||||
const {keys} = Object;
|
||||
|
||||
const Primitive = String; // it could be Number
|
||||
const primitive = 'string'; // it could be 'number'
|
||||
|
||||
const ignore = {};
|
||||
const object = 'object';
|
||||
|
||||
const noop = (_, value) => value;
|
||||
|
||||
const primitives = value => (
|
||||
value instanceof Primitive ? Primitive(value) : value
|
||||
);
|
||||
|
||||
const Primitives = (_, value) => (
|
||||
typeof value === primitive ? new Primitive(value) : value
|
||||
);
|
||||
|
||||
const revive = (input, parsed, output, $) => {
|
||||
const lazy = [];
|
||||
for (let ke = keys(output), {length} = ke, y = 0; y < length; y++) {
|
||||
const k = ke[y];
|
||||
const value = output[k];
|
||||
if (value instanceof Primitive) {
|
||||
const tmp = input[value];
|
||||
if (typeof tmp === object && !parsed.has(tmp)) {
|
||||
parsed.add(tmp);
|
||||
output[k] = ignore;
|
||||
lazy.push({k, a: [input, parsed, tmp, $]});
|
||||
}
|
||||
else
|
||||
output[k] = $.call(output, k, tmp);
|
||||
}
|
||||
else if (output[k] !== ignore)
|
||||
output[k] = $.call(output, k, value);
|
||||
}
|
||||
for (let {length} = lazy, i = 0; i < length; i++) {
|
||||
const {k, a} = lazy[i];
|
||||
output[k] = $.call(output, k, revive.apply(null, a));
|
||||
}
|
||||
return output;
|
||||
};
|
||||
|
||||
const set = (known, input, value) => {
|
||||
const index = Primitive(input.push(value) - 1);
|
||||
known.set(value, index);
|
||||
return index;
|
||||
};
|
||||
|
||||
/**
|
||||
* Converts a specialized flatted string into a JS value.
|
||||
* @param {string} text
|
||||
* @param {(this: any, key: string, value: any) => any} [reviver]
|
||||
* @returns {any}
|
||||
*/
|
||||
const parse = (text, reviver) => {
|
||||
const input = $parse(text, Primitives).map(primitives);
|
||||
const value = input[0];
|
||||
const $ = reviver || noop;
|
||||
const tmp = typeof value === object && value ?
|
||||
revive(input, new Set, value, $) :
|
||||
value;
|
||||
return $.call({'': tmp}, '', tmp);
|
||||
};
|
||||
exports.parse = parse;
|
||||
|
||||
/**
|
||||
* Converts a JS value into a specialized flatted string.
|
||||
* @param {any} value
|
||||
* @param {((this: any, key: string, value: any) => any) | (string | number)[] | null | undefined} [replacer]
|
||||
* @param {string | number | undefined} [space]
|
||||
* @returns {string}
|
||||
*/
|
||||
const stringify = (value, replacer, space) => {
|
||||
const $ = replacer && typeof replacer === object ?
|
||||
(k, v) => (k === '' || -1 < replacer.indexOf(k) ? v : void 0) :
|
||||
(replacer || noop);
|
||||
const known = new Map;
|
||||
const input = [];
|
||||
const output = [];
|
||||
let i = +set(known, input, $.call({'': value}, '', value));
|
||||
let firstRun = !i;
|
||||
while (i < input.length) {
|
||||
firstRun = true;
|
||||
output[i] = $stringify(input[i++], replace, space);
|
||||
}
|
||||
return '[' + output.join(',') + ']';
|
||||
function replace(key, value) {
|
||||
if (firstRun) {
|
||||
firstRun = !firstRun;
|
||||
return value;
|
||||
}
|
||||
const 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;
|
||||
}
|
||||
};
|
||||
exports.stringify = stringify;
|
||||
|
||||
/**
|
||||
* Converts a generic value into a JSON serializable object without losing recursion.
|
||||
* @param {any} value
|
||||
* @returns {any}
|
||||
*/
|
||||
const toJSON = value => $parse(stringify(value));
|
||||
exports.toJSON = toJSON;
|
||||
|
||||
/**
|
||||
* Converts a previously serialized object with recursion into a recursive one.
|
||||
* @param {any} value
|
||||
* @returns {any}
|
||||
*/
|
||||
const fromJSON = value => parse($stringify(value));
|
||||
exports.fromJSON = fromJSON;
|
||||
+1
@@ -0,0 +1 @@
|
||||
self.Flatted=function(t){"use strict";const{parse:e,stringify:n}=JSON,{keys:r}=Object,s=String,o="string",c={},l="object",a=(t,e)=>e,f=t=>t instanceof s?s(t):t,i=(t,e)=>typeof e===o?new s(e):e,u=(t,e,n,o)=>{const a=[];for(let f=r(n),{length:i}=f,u=0;u<i;u++){const r=f[u],i=n[r];if(i instanceof s){const s=t[i];typeof s!==l||e.has(s)?n[r]=o.call(n,r,s):(e.add(s),n[r]=c,a.push({k:r,a:[t,e,s,o]}))}else n[r]!==c&&(n[r]=o.call(n,r,i))}for(let{length:t}=a,e=0;e<t;e++){const{k:t,a:r}=a[e];n[t]=o.call(n,t,u.apply(null,r))}return n},p=(t,e,n)=>{const r=s(e.push(n)-1);return t.set(n,r),r},y=(t,n)=>{const r=e(t,i).map(f),s=r[0],o=n||a,c=typeof s===l&&s?u(r,new Set,s,o):s;return o.call({"":c},"",c)},g=(t,e,r)=>{const s=e&&typeof e===l?(t,n)=>""===t||-1<e.indexOf(t)?n:void 0:e||a,c=new Map,f=[],i=[];let u=+p(c,f,s.call({"":t},"",t)),y=!u;for(;u<f.length;)y=!0,i[u]=n(f[u++],g,r);return"["+i.join(",")+"]";function g(t,e){if(y)return y=!y,e;const n=s.call(this,t,e);switch(typeof n){case l:if(null===n)return n;case o:return c.get(n)||p(c,f,n)}return n}};return t.fromJSON=t=>y(n(t)),t.parse=y,t.stringify=g,t.toJSON=t=>e(g(t)),t}({});
|
||||
+1
@@ -0,0 +1 @@
|
||||
const{parse:t,stringify:e}=JSON,{keys:n}=Object,l=String,o="string",r={},s="object",c=(t,e)=>e,a=t=>t instanceof l?l(t):t,f=(t,e)=>typeof e===o?new l(e):e,i=(t,e,o,c)=>{const a=[];for(let f=n(o),{length:i}=f,p=0;p<i;p++){const n=f[p],i=o[n];if(i instanceof l){const l=t[i];typeof l!==s||e.has(l)?o[n]=c.call(o,n,l):(e.add(l),o[n]=r,a.push({k:n,a:[t,e,l,c]}))}else o[n]!==r&&(o[n]=c.call(o,n,i))}for(let{length:t}=a,e=0;e<t;e++){const{k:t,a:n}=a[e];o[t]=c.call(o,t,i.apply(null,n))}return o},p=(t,e,n)=>{const o=l(e.push(n)-1);return t.set(n,o),o},u=(e,n)=>{const l=t(e,f).map(a),o=l[0],r=n||c,p=typeof o===s&&o?i(l,new Set,o,r):o;return r.call({"":p},"",p)},h=(t,n,l)=>{const r=n&&typeof n===s?(t,e)=>""===t||-1<n.indexOf(t)?e:void 0:n||c,a=new Map,f=[],i=[];let u=+p(a,f,r.call({"":t},"",t)),h=!u;for(;u<f.length;)h=!0,i[u]=e(f[u++],y,l);return"["+i.join(",")+"]";function y(t,e){if(h)return h=!h,e;const n=r.call(this,t,e);switch(typeof n){case s:if(null===n)return n;case o:return a.get(n)||p(a,f,n)}return n}},y=e=>t(h(e)),g=t=>u(e(t));export{g as fromJSON,u as parse,h as stringify,y as toJSON};
|
||||
Reference in New Issue
Block a user