urltool
Version:
Node Module & Browser Library for parsing, formatting and serializing URLs
1 lines • 6.63 kB
JavaScript
;function _classCallCheck(a,b){if(!(a instanceof b))throw new TypeError("Cannot call a class as a function")}function _defineProperties(a,b){for(var c,d=0;d<b.length;d++)c=b[d],c.enumerable=c.enumerable||!1,c.configurable=!0,"value"in c&&(c.writable=!0),Object.defineProperty(a,c.key,c)}function _createClass(a,b,c){return b&&_defineProperties(a.prototype,b),c&&_defineProperties(a,c),Object.defineProperty(a,"prototype",{writable:!1}),a}function _slicedToArray(a,b){return _arrayWithHoles(a)||_iterableToArrayLimit(a,b)||_unsupportedIterableToArray(a,b)||_nonIterableRest()}function _nonIterableRest(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function _iterableToArrayLimit(a,b){var c=null==a?null:"undefined"!=typeof Symbol&&a[Symbol.iterator]||a["@@iterator"];if(null!=c){var d,e,f=[],g=!0,h=!1;try{for(c=c.call(a);!(g=(d=c.next()).done)&&(f.push(d.value),!(b&&f.length===b));g=!0);}catch(a){h=!0,e=a}finally{try{g||null==c["return"]||c["return"]()}finally{if(h)throw e}}return f}}function _arrayWithHoles(a){if(Array.isArray(a))return a}function _createForOfIteratorHelper(a,b){var c="undefined"!=typeof Symbol&&a[Symbol.iterator]||a["@@iterator"];if(!c){if(Array.isArray(a)||(c=_unsupportedIterableToArray(a))||b&&a&&"number"==typeof a.length){c&&(a=c);var d=0,e=function(){};return{s:e,n:function(){return d>=a.length?{done:!0}:{done:!1,value:a[d++]}},e:function(a){throw a},f:e}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var f,g=!0,h=!1;return{s:function(){c=c.call(a)},n:function(){var a=c.next();return g=a.done,a},e:function(a){h=!0,f=a},f:function(){try{g||null==c["return"]||c["return"]()}finally{if(h)throw f}}}}function _unsupportedIterableToArray(a,b){if(a){if("string"==typeof a)return _arrayLikeToArray(a,b);var c=Object.prototype.toString.call(a).slice(8,-1);return"Object"===c&&a.constructor&&(c=a.constructor.name),"Map"===c||"Set"===c?Array.from(a):"Arguments"===c||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(c)?_arrayLikeToArray(a,b):void 0}}function _arrayLikeToArray(a,b){(null==b||b>a.length)&&(b=a.length);for(var c=0,d=Array(b);c<b;c++)d[c]=a[c];return d}var UrlTool={defaultFormattingTemplate:"{protocol}://{host}/{path}/{query}{hash}",parseQuery:function(a){if(a===void 0||null===a||""===a)return{};a=a.trim(),a.startsWith("?")&&(a=a.substring(1));var b,c={},d=a.split("&"),e=_createForOfIteratorHelper(d);try{for(e.s();!(b=e.n()).done;){var f=b.value,g=f.split("="),h=_slicedToArray(g,2),i=h[0],j=h[1];c[decodeURIComponent(i)||""]=decodeURIComponent(j||"")}}catch(a){e.e(a)}finally{e.f()}try{delete c[""]}catch(a){}return c},stringifyQuery:function(a){for(var b=1<arguments.length&&arguments[1]!==void 0?arguments[1]:"?",c=[],d=0,e=Object.entries(a);d<e.length;d++){var f=_slicedToArray(e[d],2),g=f[0],h=f[1];c.push("".concat(encodeURIComponent(g),"=").concat(encodeURIComponent(h)))}return c.length?"".concat(b).concat(c.join("&")):""},Url:function(){function a(b){if(_classCallCheck(this,a),"string"!=typeof b)throw new TypeError("Url must be a string");this._protocol="",this._host="",this._port="",this._path="",this._query="",this._hash="",this._raw=b,this._parse()}return _createClass(a,[{key:"_parse",value:function(){var a=this._raw;if(this._raw.match(/([a-zA-Z-_\d]*?):\/\//)&&0===this._raw.match(/([a-zA-Z-_\d]*?):\/\//).index){var b=this._raw.match(/([a-zA-Z-_\d]*?):\/\//)[1];this._protocol=0<b.length?b:null,a=null===this._protocol?this._raw.substring(this._raw.indexOf("://")+3):this._raw.substring(this._raw.match(/([a-zA-Z-_\d]*?):\/\//)[0].length)}else this._protocol=this._raw.startsWith("//")?"http":null;for(;a.startsWith("/");)a=a.substring(1);var c=a.split("/"),d=c[0]||"",e=c.slice(1).join("/")||"",f=e.split("?"),g=f[0]||"",h=1<f.length?f.slice(1).join("?"):"",i=1<e.split("#").length?e.split("#").slice(1).join("#"):"";h.endsWith("#"+i)&&(h=h.substring(0,h.length-i.length-1));var j=null;if(d.includes(":")){var k=d.split(":");d=k[0],j=k[1]}else j="https"===this._protocol?"443":"http"===this._protocol?"80":"ftp"===this._protocol?"21":"ftps"===this._protocol?"990":"sftp"===this._protocol||"ssh"===this._protocol?"22":"telnet"===this._protocol?"23":"gopher"===this._protocol?"70":"80";this._hash=i,this._path=g,this._query=h,this._host=d,this._port=j,0<this._query.length&&(this._query="?"+this._query),0<this._hash.length&&(this._hash="#"+this._hash),!this._protocol&&j&&("443"===j?this._protocol="https":"80"===j?this._protocol="http":"21"===j?this._protocol="ftp":"990"===j?this._protocol="ftps":"22"===j?this._protocol="ssh":"23"===j?this._protocol="telnet":"70"===j?this._protocol="gopher":this._protocol="http"),this._raw=this.format()}},{key:"format",value:function(){var a=0<arguments.length&&void 0!==arguments[0]?arguments[0]:UrlTool.defaultFormattingTemplate,b=a;return b=b.replace(/{protocol}/g,this.protocol),b=b.replace(/{hostWithoutPort}/g,this.hostWithoutPort),b=b.replace(/{host}/g,this.host),b=b.replace(/{path}\//g,this.pathWithSlash),b=b.replace(/{path}/g,this.path),b=b.replace(/{query}/g,this.query),b=b.replace(/{hash}/g,this.hash),b=b.replace(/{port}/g,this.__portwcolumn),b}},{key:"toString",value:function(){return this.format()}},{key:"protocol",get:function(){return this._protocol},set:function(a){this._protocol=a,this._parse()}},{key:"hostWithoutPort",get:function(){return this._host},set:function(){throw new Error("Can't set hostWithoutPort. Use host instead.")}},{key:"host",get:function(){return this._host+this.__portwcolumn},set:function(a){this._host=a,this._parse()}},{key:"__portwcolumn",get:function(){return 0>=this._port.length?"":"https"===this.protocol&&"443"===this._port||"http"===this.protocol&&"80"===this._port?"":":"+this._port}},{key:"port",get:function(){return 0>=this._port.length?"":this._port},set:function(a){this._port=a,this._parse()}},{key:"path",get:function(){return this._path},set:function(a){this._path=a,this._parse()}},{key:"pathWithSlash",get:function(){return 0>=this._path.length?"":0<this.query.length?this.path:this._path.endsWith("/")?this._path:this._path+"/"},set:function(){throw new Error("Can't set pathWithSlash. Use path instead.")}},{key:"query",get:function(){return this._query},set:function(a){this._query=a,this._parse()}},{key:"hash",get:function(){return this._hash},set:function(a){this._hash=a,this._parse()}}]),a}()};try{module&&module.exports&&(module.exports=UrlTool)}catch(a){}try{window&&(window.UrlTool=UrlTool)}catch(a){}