UNPKG

@acrool/js-utils

Version:

Common javascript utils methods for project development

2 lines (1 loc) 1.82 kB
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const c=require("../regPattern-fdf73f5e.cjs");function l(e={}){const o=[];return Object.keys(e).forEach(n=>{const t=e[n];typeof t<"u"&&o.push(`${encodeURIComponent(n)}=${encodeURIComponent(t)}`)}),o.length>0?`${o.join("&")}`:""}function u(e=""){const o=decodeURIComponent(e).replace(/^.*\?/,"").split("&"),n={};if(o[0]!=="")return o.forEach(t=>{const r=t.split("=");r[1]&&(n[r[0]]=r[1])}),Object.keys(n).length>0?n:void 0}function g(e){if(e){const n=new RegExp(c.regPattern.protocolDomain).exec(e);if(n!==null)return n[0]}return""}function f(e){const n=new RegExp(c.regPattern.domain).exec(e);if(n&&n[0]){const t=n[0].split(".");if(t.length===2)return n[0];if(t.length===3)return[t[1],t[2]].join(".")}return""}function s(e){const n=new RegExp(c.regPattern.domain).exec(e);if(n&&n[0]){const t=n[0].split(".");if(t.length===3)return t[0]}return""}function a(e){const o=[];if(e.length===0)return"";if(typeof e[0]!="string")throw new TypeError("Url must be a string. Received "+e[0]);e[0].match(/^[^/:]+:\/*$/)&&e.length>1&&(e[0]=e.shift()+e[0]),e[0].match(/^file:\/\/\//)?e[0]=e[0].replace(/^([^/:]+):\/*/,"$1:///"):e[0]=e[0].replace(/^([^/:]+):\/*/,"$1://");for(let r=0;r<e.length;r++){let i=e[r];if(typeof i!="string")throw new TypeError("Url must be a string. Received "+i);i!==""&&(r>0&&(i=i.replace(/^[\/]+/,"")),r<e.length-1?i=i.replace(/[\/]+$/,""):i=i.replace(/[\/]+$/,"/"),o.push(i))}let n=o.join("/");n=n.replace(/\/(\?|&|#[^!])/g,"$1");const t=n.split("?");return n=t.shift()+(t.length>0?"?":"")+t.join("&"),n}function p(...e){const o=Array.from(Array.isArray(e[0])?e[0]:e);return a(o)}exports.decodeQueryString=u;exports.encodeQueryString=l;exports.getMainDomain=f;exports.getProtocolDomain=g;exports.getSubDomain=s;exports.urlJoin=p;