UNPKG

unquery

Version:

Build and control queries with confidence.

2 lines (1 loc) 2.99 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});const{isArray:e}=Array,t=e=>"string"==typeof e,r=e=>encodeURIComponent(e),n=e=>{try{return decodeURIComponent(e)}catch{return e}};const o={arrayFormat:"none",skipNull:!1,skipUnknown:!0},a=(...e)=>{for(const t of e){const e=o[t];if(null!=e)return e}return null};var c;function l(e,{type:t,customCallback:r}){return null==e?null:(e=n(e),t&c.STRING?""+e:t&c.NUMBER?""!==e?+e:null:t&c.BOOL?Boolean(e):t&c.CUSTOM?r(e):e)}!function(e){e[e.STRING=1]="STRING",e[e.NUMBER=2]="NUMBER",e[e.BOOL=4]="BOOL",e[e.ARRAY=8]="ARRAY",e[e.CUSTOM=16]="CUSTOM"}(c||(c={}));const s=(r,a,i)=>{a=a||{},i={...o,...i||{}};const{skipNull:u,skipUnknown:p,arrayFormat:y}=i,m=Object.create({});return t(r)?((r=r.trim().replace(/^[?#&]/,""))&&r.split("&").forEach(e=>{const[t=e,r]=(e=>{const t=e.indexOf("=");return-1===t?[]:[e.slice(0,t),e.slice(t+1)]})(e),{key:u,isArray:f}=function({key:e,value:t,arrayFormat:r}){let n;switch(r){case"index":n=/\[(\d*)\]$/.test(e),e=e.replace(/\[(\d*)\]$/,"");break;case"bracket":n=/\[\]$/.test(e),e=e.replace(/\[\]$/,"");break;case"comma":n=(""+t).indexOf(",")>-1}return{key:e,isArray:n}}({key:n(t),value:r,arrayFormat:y}),k=!(u in a);if(k&&p)return;const O=a[u]||{},R={innerType:f&&k&&s.string(),...O};"comma"===i.arrayFormat&&(m[u]=null),function({options:e,type:t=c.STRING,key:r,value:n,queryObject:a,innerType:s,customCallback:i}){if(s)if(a[r])a[r].push(l(n,s));else if(null==n)a[r]=[null];else{"comma"==={...o,...e}.arrayFormat?a[r]=n.split(",").map(e=>l(e,s)):a[r]=[l(n,s)]}else a[r]=l(n,{type:t,customCallback:i})}({options:i,...R,key:u,value:r,queryObject:m})}),Object.keys(a).forEach(t=>{const r=m[t];(!u&&null==r||e(r)&&1===r.length&&null===r[0])&&(m[t]=null)}),m):m};s.string=()=>({type:c.STRING}),s.number=()=>({type:c.NUMBER}),s.bool=()=>({type:c.BOOL}),s.array=e=>({type:c.ARRAY,innerType:e||c.STRING}),s.custom=e=>({type:c.CUSTOM,customCallback:e||(e=>e)});const i=(e,t,n)=>{if("comma"===n){const n=e.map(e=>r(e)).join(",");return n?`${t}=${n}`:null}const o=(e=>t=>e.map((e,n)=>`${t(n)}=${r(e)}`).join("&"))(e);switch(n){case"index":return o(e=>`${t}[${e}]`);case"bracket":return o(()=>t+"[]");default:return o(()=>""+t)}};function u(n,o){if(t(n))return n.trim().replace(/^[?#&]/,"");const{arrayFormat:c}={arrayFormat:a("arrayFormat"),...o};return Object.keys(n).map(t=>{const o=n[t];return null==o?"":e(o)?i(o,t,c):`${r(t)}=${r(o)}`}).filter(Boolean).join("&")}const p=(e="")=>history.replaceState(null,null,`${location.pathname.replace(/\/$/,"")}${e}${location.hash}`);exports.Unquery=s,exports.addLocationURL=(e,t)=>{const r=location.search?"&":"?";p(`${location.search}${r}${u(e,t)}`)},exports.clearLocationURL=()=>p(),exports.replaceLocationURL=(e,t)=>{const r=u(e,t);p(r&&"?"+r)},exports.setOptions=t=>{if((n=t)&&"object"==typeof n&&!e(n))for(const e in t)e in o&&(o[e]=t[e]);else r=`Updated options "${t}" was not an object`,console.warn("Unquery: "+r);var r,n;return o},exports.stringify=u;