UNPKG

@knighttower/utility

Version:

UtilityJs is a utility library that provides a collection of utility functions for various tasks. The library is designed to be easy to use and covers the most common use cases.

1 lines 1.39 kB
(()=>{"use strict";var e={d:(t,o)=>{for(var r in o){e.o(o,r)&&!e.o(t,r)&&Object.defineProperty(t,r,{enumerable:!0,get:o[r]})}},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t)},t={};e.d(t,{urlHelper:()=>o});const o=e=>{const t="undefined"!=typeof window?window:void 0,o="undefined"!=typeof document?document:{},r=t?.location??e?.uri;let n=r?.hash,s=r?.href;const a=r?.search,l=r?.protocol?.replace(":",""),c=r?.host,i=r?.pathname,u=`${l}://${c}`,p=()=>{if(d){return d}const e=new URLSearchParams(a),t={};for(const[o,r]of e.entries()){t[o]=r}return d={params:e,queryString:e.toString(),search:a,keys:Array.from(e.keys()),values:Array.from(e.values()),collection:t},d};let d=null;return{getPage:()=>{const e=s?.toLowerCase();return e.split("/").pop().split(".")[0]||"index"},getParams:()=>p(),getQueryString:()=>p().queryString,getQuery:e=>e?p().collection[e]:p().collection,addToQuery:e=>{const t=p().collection;Object.assign(t,e);return{collection:t,queryString:Object.entries(t).map(([e,t])=>`${encodeURIComponent(e)}=${encodeURIComponent(t)}`).join("&")}},getHash:()=>n?.substring(1),setHash:e=>{n=e},deleteHash:()=>{history.pushState("",o.title,i+a)},goTo:e=>(r.href=e,!1),open:(e,o="_blank",r="")=>t?.open(e,o,r),onChange:e=>{"function"==typeof e&&t?.addEventListener("hashchange",e)},fullUrl:`${u}${i}`,siteUrl:u,protocol:l,host:c,path:i,readUrl:s}};window.urlHelper=t.urlHelper})();