gb-url
Version:
Parse and set url search parameters
2 lines (1 loc) • 1.29 kB
JavaScript
(function(i,f){typeof exports=="object"&&typeof module<"u"?f(exports):typeof define=="function"&&define.amd?define(["exports"],f):(i=typeof globalThis<"u"?globalThis:i||self,f(i.gbUrl={}))})(this,function(i){"use strict";const f=typeof window<"u",u=typeof globalThis<"u"?globalThis:window,c=()=>f?u.location.href:"";function a(t,n){n=n||c();const e=new RegExp("[?&#]"+t+"=([^&#]*)","i"),o=n.match(e);if(o)try{return decodeURIComponent(o[1])||""}catch{return""}return""}function l(t){t=t||(f?u.location:"");const n=typeof t=="object"?c():t;let e=f?t.search:"";if(!n||n.indexOf("?")<0||!e&&typeof e!="function")return"";console.log("uriQuery",e,typeof e);let o=n.match(/(.*?)\?(.*)/),r=e&&e.match(/^\?(.*)/)[1];return e=typeof e=="function"?o&&o:r,console.log("uriQuery2",e,typeof e),e=e.indexOf("#")>-1?e.match(/(.*?)#(.*)/)[1]:e,e}function s(t){t=t||c();let n=l(t)||"",e={};return n.replace(/([^&]*?)\=(.*?)(&|$)/gi,function(o,r,p){console.log("a",o),e[r]=p}),e}function d(t){t=t||{};var n=[];for(var e in t)t[e]&&n.push(e+"="+t[e]);return n.join("&")}function h(t,n){t=t||{},n=n||c();var e=s(n),o;for(var r in t)t[r]&&(e[r]=t[r]);return o=d(e),n.split("?")[0]+(o?"?"+o:"")}i.queryParam=a,i.setParams=h,Object.defineProperties(i,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});