UNPKG

unquery

Version:

Build and control queries with confidence.

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