@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.6 kB
JavaScript
var e,t;e=this,t=()=>(()=>{"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(":",""),c=r?.host,i=r?.pathname,p=`${a}://${c}`,u=()=>{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:()=>u(),getQueryString:()=>u().queryString,getQuery:e=>e?u().collection[e]:u().collection,addToQuery:e=>{const t=u().collection;return Object.assign(t,e),{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+s)},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:`${p}${i}`,siteUrl:p,protocol:a,host:c,path:i,readUrl:l}};return t=t.urlHelper})(),"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define("urlHelper",[],t):"object"==typeof exports?exports.urlHelper=t():e.urlHelper=t();