flatted
Version:
A super light and fast circular JSON parser.
1 lines • 1.64 kB
JavaScript
self.Flatted=function(r){"use strict";function n(r){return n="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(r){return typeof r}:function(r){return r&&"function"==typeof Symbol&&r.constructor===Symbol&&r!==Symbol.prototype?"symbol":typeof r},n(r)}var t=JSON.parse,e=JSON.stringify,o=Object.keys,u=String,f="string",i={},a="object",c=function(r,n){return n},l=function(r){if(n(r)===a&&r)for(var t=o(r),e=t.length,i=0;i<e;i++){var c=t[i];n(r[c])===f&&(r[c]=new u(r[c]))}return r},s=function(r,n,t){var e=u(n.push(t)-1);return r.set(t,e),e},y=function(r,e){var f=t(r).map(l),s=e||c,y=f[0];if(n(y)===a&&y){var v=[],p=function(r,t,e,f){return function(c){for(var l=o(c),s=l.length,y=0;y<s;y++){var v=l[y],p=c[v];if(p instanceof u){var h=r[+p];n(h)===a&&h&&!e.has(h)?(e.add(h),c[v]=i,t.push({o:c,k:v,r:h})):c[v]=f.call(c,v,h)}else c[v]!==i&&(c[v]=f.call(c,v,p))}return c}}(f,v,new Set,s);y=p(y);for(var h=0;h<v.length;)p(v[h++].r);for(;h--;){var S=v[h],b=S.o,g=S.k,m=S.r;b[g]=s.call(b,g,m)}}return s.call({"":y},"",y)},v=function(r,t,o){var i=Array.isArray(t)?[]:null;if(i)for(var l=0;l<t.length;l++){var y=t[l];n(y)!==f&&"number"!=typeof y||i.push(u(y))}for(var v={"":r},p=i?function(r,n){return this===v||Array.isArray(this)||-1<i.indexOf(r)?n:void 0}:t||c,h=new Map,S=[],b=[],g=+s(h,S,p.call(v,"",r)),m=!g;g<S.length;)m=!0,b[g]=e(S[g++],O,o);return"["+b.join(",")+"]";function O(r,t){if(m)return m=!m,t;var e=p.call(this,r,t);switch(n(e)){case a:if(null===e)return e;case f:return h.get(e)||s(h,S,e)}return e}};return r.fromJSON=function(r){return y(e(r))},r.parse=y,r.stringify=v,r.toJSON=function(r){return t(v(r))},r}({});