@discoveryjs/json-ext
Version:
A set of utilities that extend the use of JSON
15 lines (13 loc) • 9.41 kB
JavaScript
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global.jsonExt = factory());
}(typeof globalThis != 'undefined' ? globalThis : typeof window != 'undefined' ? window : typeof global != 'undefined' ? global : typeof self != 'undefined' ? self : this, (function () {
var exports=(()=>{var L=Object.defineProperty;var M=Object.getOwnPropertyDescriptor;var _=Object.getOwnPropertyNames;var K=Object.prototype.hasOwnProperty;var U=(e,t)=>{for(var n in t)L(e,n,{get:t[n],enumerable:!0})},Y=(e,t,n,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let i of _(t))!K.call(e,i)&&i!==n&&L(e,i,{get:()=>t[i],enumerable:!(r=M(t,i))||r.enumerable});return e};var G=e=>Y(L({},"__esModule",{value:!0}),e);var st={};U(st,{createStringifyWebStream:()=>V,parseChunked:()=>B,parseFromWebStream:()=>$,stringifyChunked:()=>w,stringifyInfo:()=>W});function j(e){return typeof e=="object"&&e!==null&&(typeof e[Symbol.iterator]=="function"||typeof e[Symbol.asyncIterator]=="function")}function O(e,t,n,r){switch(n&&typeof n.toJSON=="function"&&(n=n.toJSON()),r!==null&&(n=r.call(e,String(t),n)),typeof n){case"function":case"symbol":n=void 0;break;case"object":if(n!==null){let i=n.constructor;(i===String||i===Number||i===Boolean)&&(n=n.valueOf())}break}return n}function F(e){return typeof e=="function"?e:Array.isArray(e)?[...new Set(e.map(n=>{let r=n&&n.constructor;return r===String||r===Number?String(n):null}).filter(n=>typeof n=="string"))]:null}function N(e){return typeof e=="number"?!Number.isFinite(e)||e<1?!1:" ".repeat(Math.min(e,10)):typeof e=="string"&&e.slice(0,10)||!1}var k=1,H=2,Q=new TextDecoder;function X(e,t){return e.name==="SyntaxError"&&t.jsonParseOffset&&(e.message=e.message.replace(/at position (\d+)/,(n,r)=>"at position "+(Number(r)+t.jsonParseOffset))),e}function Z(e,t){let n=e.length;e.length+=t.length;for(let r=0;r<t.length;r++)e[n+r]=t[r]}async function B(e){let t=typeof e=="function"?e():e;if(j(t)){let n=new z;try{for await(let r of t){if(typeof r!="string"&&!ArrayBuffer.isView(r))throw new TypeError("Invalid chunk: Expected string, TypedArray or Buffer");n.push(r)}return n.finish()}catch(r){throw X(r,n)}}throw new TypeError("Invalid chunk emitter: Expected an Iterable, AsyncIterable, generator, async generator, or a function returning an Iterable or AsyncIterable")}var z=class{constructor(){this.value=void 0,this.valueStack=null,this.stack=new Array(100),this.lastFlushDepth=0,this.flushDepth=0,this.stateString=!1,this.stateStringEscape=!1,this.pendingByteSeq=null,this.pendingChunk=null,this.chunkOffset=0,this.jsonParseOffset=0}parseAndAppend(t,n){this.stack[this.lastFlushDepth-1]===k?(n&&(this.jsonParseOffset--,t="{"+t+"}"),Object.assign(this.valueStack.value,JSON.parse(t))):(n&&(this.jsonParseOffset--,t="["+t+"]"),Z(this.valueStack.value,JSON.parse(t)))}prepareAddition(t){let{value:n}=this.valueStack;if(Array.isArray(n)?n.length!==0:Object.keys(n).length!==0){if(t[0]===",")return this.jsonParseOffset++,t.slice(1);if(t[0]!=="}"&&t[0]!=="]")return this.jsonParseOffset-=3,"[[]"+t}return t}flush(t,n,r){let i=t.slice(n,r);if(this.jsonParseOffset=this.chunkOffset+n,this.pendingChunk!==null&&(i=this.pendingChunk+i,this.jsonParseOffset-=this.pendingChunk.length,this.pendingChunk=null),this.flushDepth===this.lastFlushDepth)this.flushDepth>0?this.parseAndAppend(this.prepareAddition(i),!0):(this.value=JSON.parse(i),this.valueStack={value:this.value,prev:null});else if(this.flushDepth>this.lastFlushDepth){for(let s=this.flushDepth-1;s>=this.lastFlushDepth;s--)i+=this.stack[s]===k?"}":"]";this.lastFlushDepth===0?(this.value=JSON.parse(i),this.valueStack={value:this.value,prev:null}):this.parseAndAppend(this.prepareAddition(i),!0);for(let s=this.lastFlushDepth||1;s<this.flushDepth;s++){let o=this.valueStack.value;if(this.stack[s-1]===k){let h;for(h in o);o=o[h]}else o=o[o.length-1];this.valueStack={value:o,prev:this.valueStack}}}else{i=this.prepareAddition(i);for(let s=this.lastFlushDepth-1;s>=this.flushDepth;s--)this.jsonParseOffset--,i=(this.stack[s]===k?"{":"[")+i;this.parseAndAppend(i,!1);for(let s=this.lastFlushDepth-1;s>=this.flushDepth;s--)this.valueStack=this.valueStack.prev}this.lastFlushDepth=this.flushDepth}push(t){if(typeof t!="string"){if(this.pendingByteSeq!==null){let s=t;t=new Uint8Array(this.pendingByteSeq.length+s.length),t.set(this.pendingByteSeq),t.set(s,this.pendingByteSeq.length),this.pendingByteSeq=null}if(t[t.length-1]>127)for(let s=0;s<t.length;s++){let o=t[t.length-1-s];if(o>>6===3){s++,(s!==4&&o>>3===30||s!==3&&o>>4===14||s!==2&&o>>5===6)&&(this.pendingByteSeq=t.slice(t.length-s),t=t.slice(0,-s));break}}t=Q.decode(t)}let n=t.length,r=0,i=0;t:for(let s=0;s<n;s++){if(this.stateString){for(;s<n;s++)if(this.stateStringEscape)this.stateStringEscape=!1;else switch(t.charCodeAt(s)){case 34:this.stateString=!1;continue t;case 92:this.stateStringEscape=!0}break}switch(t.charCodeAt(s)){case 34:this.stateString=!0,this.stateStringEscape=!1;break;case 44:i=s;break;case 123:i=s+1,this.stack[this.flushDepth++]=k;break;case 91:i=s+1,this.stack[this.flushDepth++]=H;break;case 93:case 125:i=s+1,this.flushDepth--,this.flushDepth<this.lastFlushDepth&&(this.flush(t,r,i),r=i);break;case 9:case 10:case 13:case 32:r===s&&r++,i===s&&i++;break}}i>r&&this.flush(t,r,i),i<n&&(this.pendingChunk!==null?this.pendingChunk+=t:this.pendingChunk=t.slice(i,n)),this.chunkOffset+=n}finish(){return this.pendingChunk!==null&&(this.flush("",0,0),this.pendingChunk=null),this.value}};function T(e){return/[^\x20\x21\x23-\x5B\x5D-\uD799]/.test(e)?JSON.stringify(e):'"'+e+'"'}function*w(e,t,n){(t===null||Array.isArray(t)||typeof t!="object")&&(t={replacer:t,space:n});let r=Number(t.highWaterMark)||16384,i=F(t.replacer);n=N(t.space);let s="",o=0,h=null,g=!1,S=new WeakSet,b=!1,a=Object.keys;if(Array.isArray(i)){let f=i;a=()=>f,i=null}for(P(m,e,null);h!==null;){for(b=!0;h!==null&&!h.awaiting&&(h.handler(),!!b););b=!1,yield s,s=""}function m(){let{value:f}=h;E(),c({"":f},"",f,()=>{})}function x(f){g===!1?g=!0:u(","),u(n?`
${n.repeat(o)}${T(f)}: `:T(f)+":")}function D(){let f=h;if(f.index===f.keys.length){u(n&&g?`
${n.repeat(o-1)}}`:"}"),E();return}let d=f.keys[f.index];c(f.value,d,f.value[d],x),f.index++}function l(f){f!==0&&u(","),n&&u(`
${n.repeat(o)}`)}function A(){let f=h;if(f.index===f.value.length){n&&f.index!==0?u(`
${n.repeat(o-1)}]`):u("]"),E();return}c(f.value,f.index,f.value[f.index],l),f.index++}function c(f,d,p,C){p=O(f,d,p,i),p===null||typeof p!="object"?(C!==x||p!==void 0)&&(C(d),I(p)):Array.isArray(p)?(C(d),y(p),o++,u("["),P(A,p,null)):(C(d),y(p),o++,u("{"),P(D,p,a(p)))}function y(f){if(S.has(f))throw new TypeError("Converting circular structure to JSON");S.add(f)}function I(f){switch(typeof f){case"string":u(T(f));break;case"number":u(Number.isFinite(f)?f:"null");break;case"boolean":u(f?"true":"false");break;case"undefined":case"object":u("null");break;default:throw new TypeError(`Do not know how to serialize a ${f.constructor?.name||typeof f}`)}}function P(f,d,p){return g=!1,h={handler:f,value:d,index:0,keys:p,prev:h}}function E(){let{handler:f,value:d}=h;(f===D||f===A)&&(S.delete(d),o--),h=h.prev,g=!0}function u(f){s+=f,b=s.length<r}}var q=typeof Object.hasOwn=="function"?Object.hasOwn:(e,t)=>Object.hasOwnProperty.call(e,t),R={8:"\\b",9:"\\t",10:"\\n",12:"\\f",13:"\\r",34:'\\"',92:"\\\\"},v=Array.from({length:2048}).map((e,t)=>q(R,t)?2:t<32?6:t<128?1:2);function tt(e){return e>=55296&&e<=56319}function et(e){return e>=56320&&e<=57343}function J(e){let t=0,n=!1;for(let r=0;r<e.length;r++){let i=e.charCodeAt(r);if(i<2048)t+=v[i];else if(tt(i)){t+=6,n=!0;continue}else et(i)?t=n?t-2:t+6:t+=3;n=!1}return t+2}function nt(e){switch(typeof e){case"string":return J(e);case"number":return Number.isFinite(e)?String(e).length:4;case"boolean":return e?4:5;case"undefined":case"object":return 4;default:return 0}}function it(e){return e=N(e),typeof e=="string"?e.length:0}function W(e,t,n){(t===null||Array.isArray(t)||typeof t!="object")&&(t={replacer:t,space:n});let r=null,i=F(t.replacer),s=!!t.continueOnCircular;Array.isArray(i)&&(r=new Set(i),i=null),n=it(n);let o=new WeakMap,h=new Set,g=new Set,S={"":e},b=!1,a=0;return m(S,"",e),{bytes:isNaN(a)?1/0:a,circular:[...g]};function m(x,D,l){if(!b)if(l=O(x,D,l,i),l===null||typeof l!="object")l!==void 0||Array.isArray(x)?a+=nt(l):x===S&&(a+=9);else{if(h.has(l)){g.add(l),a+=4,s||(b=!0);return}if(o.has(l)){a+=o.get(l);return}if(Array.isArray(l)){let A=a;a+=2,h.add(l);for(let c=0;c<l.length;c++)m(l,c,l[c]);l.length>1&&(a+=l.length-1),h.delete(l),n>0&&l.length>0&&(a+=(1+(h.size+1)*n)*l.length,a+=1+h.size*n),o.set(l,a-A)}else{let A=a,c=0;a+=2,h.add(l);for(let y in l)if(q(l,y)&&(r===null||r.has(y))){let I=a;m(l,y,l[y]),I!==a&&(a+=J(y)+1,c++)}c>1&&(a+=c-1),h.delete(l),n>0&&c>0&&(a+=(1+(h.size+1)*n+1)*c,a+=1+h.size*n),o.set(l,a-A)}}}}function $(e){return B(j(e)?e:async function*(){let t=e.getReader();for(;;){let{value:n,done:r}=await t.read();if(r)break;yield n}})}function V(e,t,n){return typeof ReadableStream.from=="function"?ReadableStream.from(w(e,t,n)):new ReadableStream({start(){this.generator=w(e,t,n)},pull(r){let{value:i,done:s}=this.generator.next();s?r.close():r.enqueue(i)},cancel(){this.generator=null}})}return G(st);})();
return exports;
})));
//# sourceMappingURL=json-ext.min.js.map