UNPKG

ts-search-params

Version:
2 lines 2.27 kB
"use strict";var p=Object.defineProperty;var m=Object.getOwnPropertyDescriptor;var N=Object.getOwnPropertyNames;var Q=Object.prototype.hasOwnProperty;var x=e=>{throw TypeError(e)};var $=(e,n)=>{for(var t in n)p(e,t,{get:n[t],enumerable:!0})},j=(e,n,t,r)=>{if(n&&typeof n=="object"||typeof n=="function")for(let s of N(n))!Q.call(e,s)&&s!==t&&p(e,s,{get:()=>n[s],enumerable:!(r=m(n,s))||r.enumerable});return e};var w=e=>j(p({},"__esModule",{value:!0}),e);var k=(e,n,t)=>n.has(e)||x("Cannot "+t);var o=(e,n,t)=>(k(e,n,"read from private field"),t?t.call(e):n.get(e)),d=(e,n,t)=>n.has(e)?x("Cannot add the same private member more than once"):n instanceof WeakSet?n.add(e):n.set(e,t),O=(e,n,t,r)=>(k(e,n,"write to private field"),r?r.call(e,t):n.set(e,t),t),c=(e,n,t)=>(k(e,n,"access private method"),t);var U={};$(U,{TSSearchParams:()=>g});module.exports=w(U);var i,u,f,a,g=class{constructor(n,t={}){d(this,f);d(this,i);d(this,u);let r=!n||typeof n=="string"?new URLSearchParams(n):n,s={};for(let[y,h]of r.entries())s[y]=S(h);O(this,i,s),O(this,u,{validate:t.validate||(()=>o(this,i)),questionMark:t.questionMark||!1})}assign(n){return Object.assign(o(this,i),n),this}toString(){let n=Object.entries(c(this,f,a).call(this)).reduce((t,[r,s])=>{if(s===void 0)return t;let y=t.length?"&":"",h=encodeURIComponent(r),b=P(s);return t+`${y}${h}=${b}`},"");return o(this,u).questionMark?`?${n}`:n}toObject(){return c(this,f,a).call(this)}get(n){return c(this,f,a).call(this)[n]}};i=new WeakMap,u=new WeakMap,f=new WeakSet,a=function(){return o(this,u).validate(o(this,i))};function S(e){if(T(e,"{","}")||T(e,"[","]"))return JSON.parse(e,(n,t)=>typeof t=="string"?S(t):t);if(T(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 P(e){if(R(e)){let n;return encodeURIComponent(JSON.stringify(e,(t,r)=>R(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 T(e,n,t=n){return e.startsWith(n)&&e.endsWith(t)}function R(e){return typeof e=="object"&&e!==null}0&&(module.exports={TSSearchParams}); //# sourceMappingURL=index.cjs.map