@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.53 kB
JavaScript
(()=>{"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,l=r?.href;const s=r?.search,a=r?.protocol?.replace(":",""),i=r?.host,c=r?.pathname,u=`${a}://${i}`,p=()=>{if(d){return d}const e=new URLSearchParams(s),t={};for(const[o,r]of e.entries()){t[o]=r}return d={params:e,queryString:e.toString(),search:s,keys:Array.from(e.keys()),values:Array.from(e.values()),collection:t},d};let d=null;return{getPage:()=>{const e=l?.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=>{let[t,o]=e;return`${encodeURIComponent(t)}=${encodeURIComponent(o)}`})).join("&")}},getHash:()=>n?.substring(1),setHash:e=>{n=e},deleteHash:()=>{history.pushState("",o.title,c+s)},goTo:e=>(r.href=e,!1),open:function(e){let o=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"_blank",r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:"";return t?.open(e,o,r)},onChange:e=>{"function"==typeof e&&t?.addEventListener("hashchange",e)},fullUrl:`${u}${c}`,siteUrl:u,protocol:a,host:i,path:c,readUrl:l}};window.urlHelper=t.urlHelper})();