UNPKG

build-url-ts

Version:

A small library that builds a URL given its components

3 lines (2 loc) 3.23 kB
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n((e="undefined"!=typeof globalThis?globalThis:e||self).buildUrl={})}(this,function(e){"use strict";var l=function(){return(l=Object.assign||function(e){for(var n,r=1,t=arguments.length;r<t;r++)for(var o in n=arguments[r])Object.prototype.hasOwnProperty.call(n,o)&&(e[o]=n[o]);return e}).apply(this,arguments)};function C(e){return encodeURIComponent(e).replace(/'/g,"%27").replace(/`/g,"%60")}function f(e,n,r,t){void 0===t&&(t=!0);for(var o=[],a=0,i=Object.entries(e);a<i.length;a++){var c=i[a],u=c[0],s=c[1];if(void 0!==s){var l=n?u.toLowerCase():u,f=(t?C:encodeURIComponent)(l);if(Array.isArray(s)){var d=s.filter(function(e){return void 0!==e});if(0===d.length)continue;if(r)for(var p=d.length,v="order_desc"===r?p-1:0,h=0,b=d;h<b.length;h++){var g=b[h],y=(t?C:encodeURIComponent)(j(g,n));switch(r){case"array":o.push("".concat(f,"[]=").concat(y));break;case"order_asc":o.push("".concat(f,"[").concat(v++,"]=").concat(y));break;case"order_desc":o.push("".concat(f,"[").concat(v--,"]=").concat(y));break;default:o.push("".concat(f,"=").concat(y))}}else{var m=d.map(function(e){return j(e,n)}).join(",");o.push("".concat(f,"=").concat((t?C:encodeURIComponent)(m)))}}else{y=(t?C:encodeURIComponent)(j(s,n));o.push("".concat(f,"=").concat(y))}}}return 0<o.length?"?".concat(o.join("&")):""}function j(e,n){if(null===e)return"";if(void 0===e)return"";if("boolean"==typeof e)return e.toString();if(0===e)return"0";if("number"==typeof e&&isNaN(e))return"NaN";if(!e)return"";if(e instanceof Date){var r=e.toString();return n?r.toLowerCase():r}if("object"==typeof e&&!Array.isArray(e)){var t=JSON.stringify(e);return n?t.toLowerCase():t}var o=String(e).trim();return n?o.toLowerCase():o}function d(e,n,r){var t=null!=n?n:"",o=String(e).trim(),a=r?o.toLowerCase():o;if(!a)return t;if("/"===a)return t.endsWith("/")?t:"".concat(t,"/");var i=a.endsWith("/"),c=a.split("/").filter(function(e){return 0<e.length}).join("/"),u=i&&c?"".concat(c,"/"):c,s=t.replace(/\/+$/,"");return u?"".concat(s,"/").concat(u):s}function p(e,n){var r=String(e).trim();if(!r)return"";var t="#".concat(r);return n?t.toLowerCase():t}function n(e,n){var r,t,o,a={},i="";o=null==e?(t="",n):"object"==typeof e?(t="",e):(t=(r=function(e){var n=e.indexOf("#");e.indexOf("?");var r=e,t={},o="";-1!==n&&(o=e.substring(n+1),r=e.substring(0,n));var a=-1!==n?e.substring(0,n):e,i=a.indexOf("?");if(-1!==i){var c=a.substring(i+1),r=a.substring(0,i);if(c)for(var u=0,s=c.split("&");u<s.length;u++){var l=s[u].split("="),f=l[0],d=l[1];f&&(t[decodeURIComponent(f)]=d?decodeURIComponent(d):"")}}return{baseUrl:r,queryParams:t,hash:o}}(e)).baseUrl,a=r.queryParams,i=r.hash,n);var c=t;null!=o&&o.path&&(c=d(o.path,c,o.lowerCase));var u=l(l({},a),(null==o?void 0:o.queryParams)||{});0<Object.keys(u).length&&(c+=f(u,null==o?void 0:o.lowerCase,null==o?void 0:o.disableCSV,!1));var s=void 0!==(null==o?void 0:o.hash)?o.hash:i;return s&&(c+=p(s,null==o?void 0:o.lowerCase)),c}e.appendPath=d,e.buildHash=p,e.buildQueryString=f,e.buildUrl=n,e.default=n,Object.defineProperty(e,"__esModule",{value:!0})}); //# sourceMappingURL=index.js.map