ts-search-params
Version:
Type Safe Search Params
2 lines • 1.78 kB
JavaScript
var T=e=>{throw TypeError(e)};var h=(e,n,t)=>n.has(e)||T("Cannot "+t);var i=(e,n,t)=>(h(e,n,"read from private field"),t?t.call(e):n.get(e)),d=(e,n,t)=>n.has(e)?T("Cannot add the same private member more than once"):n instanceof WeakSet?n.add(e):n.set(e,t),p=(e,n,t,r)=>(h(e,n,"write to private field"),r?r.call(e,t):n.set(e,t),t),c=(e,n,t)=>(h(e,n,"access private method"),t);var s,o,u,a,O=class{constructor(n,t={}){d(this,u);d(this,s);d(this,o);let r=!n||typeof n=="string"?new URLSearchParams(n):n,f={};for(let[g,y]of r.entries())f[g]=R(y);p(this,s,f),p(this,o,{validate:t.validate||(()=>i(this,s)),questionMark:t.questionMark||!1})}assign(n){return Object.assign(i(this,s),n),this}toString(){let n=Object.entries(c(this,u,a).call(this)).reduce((t,[r,f])=>{if(f===void 0)return t;let g=t.length?"&":"",y=encodeURIComponent(r),S=b(f);return t+`${g}${y}=${S}`},"");return i(this,o).questionMark?`?${n}`:n}toObject(){return c(this,u,a).call(this)}get(n){return c(this,u,a).call(this)[n]}};s=new WeakMap,o=new WeakMap,u=new WeakSet,a=function(){return i(this,o).validate(i(this,s))};function R(e){if(k(e,"{","}")||k(e,"[","]"))return JSON.parse(e,(n,t)=>typeof t=="string"?R(t):t);if(k(e,'"'))return e.slice(1,-1);if(e==="true"||e==="false")return e==="true";if(e==="null")return null;if(e==="NaN")return NaN;if(e!=="undefined")return!isNaN(+e)&&(+e).toString()===e?+e:e}function b(e){if(x(e)){let n;return encodeURIComponent(JSON.stringify(e,(t,r)=>x(r)?(n=r,r):typeof r=="string"?`"${r}"`:typeof r>"u"&&!Array.isArray(n)?void 0:String(r)))}return e&&typeof e=="string"?encodeURIComponent(`"${e}"`):String(e)}function k(e,n,t=n){return e.startsWith(n)&&e.endsWith(t)}function x(e){return typeof e=="object"&&e!==null}export{O as TSSearchParams};
//# sourceMappingURL=index.js.map