UNPKG

unquery

Version:

Build and control queries with confidence.

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