path-parser
Version:
A small utility to parse, match and generate paths
3 lines (2 loc) • 5.82 kB
JavaScript
Object.defineProperty(exports,"__esModule",{value:!0});var r=require("tslib"),t=require("search-params"),e=/[^!$'()*+,;|:]/g,n=function(r){return r.replace(e,(function(r){return encodeURIComponent(r)}))},a={default:n,uri:encodeURI,uriComponent:encodeURIComponent,none:function(r){return r},legacy:encodeURI},i={default:decodeURIComponent,uri:decodeURI,uriComponent:decodeURIComponent,none:function(r){return r},legacy:decodeURIComponent},s=function(r,t,e){var i=a[t]||n;return e?String(r).split("/").map(i).join("/"):i(String(r))},u=function(r){return"("+(r?r.replace(/(^<|>$)/g,""):"[a-zA-Z0-9-_.~%':|=+\\*@$]+")+")"},o=[{name:"url-parameter",pattern:/^:([a-zA-Z0-9-_]*[a-zA-Z0-9]{1})(<(.+?)>)?/,regex:function(r){return new RegExp(u(r[2]))}},{name:"url-parameter-splat",pattern:/^\*([a-zA-Z0-9-_]*[a-zA-Z0-9]{1})/,regex:/([^?]*)/},{name:"url-parameter-matrix",pattern:/^;([a-zA-Z0-9-_]*[a-zA-Z0-9]{1})(<(.+?)>)?/,regex:function(r){return new RegExp(";"+r[1]+"="+u(r[2]))}},{name:"query-parameter",pattern:/^(?:\?|&)(?::)?([a-zA-Z0-9-_]*[a-zA-Z0-9]{1})/},{name:"delimiter",pattern:/^(\/|\?)/,regex:function(r){return new RegExp("\\"+r[0])}},{name:"sub-delimiter",pattern:/^(!|&|-|_|\.|;)/,regex:function(r){return new RegExp(r[0])}},{name:"fragment",pattern:/^([0-9a-zA-Z]+)/,regex:function(r){return new RegExp(r[0])}}],c=function r(t,e){if(void 0===e&&(e=[]),!o.some((function(n){var a=t.match(n.pattern);return!!a&&(e.push({type:n.name,match:a[0],val:a.slice(1,2),otherVal:a.slice(2),regex:n.regex instanceof Function?n.regex(a):n.regex}),a[0].length<t.length&&(e=r(t.substr(a[0].length),e)),!0)})))throw new Error("Could not parse path '"+t+"'");return e},p=function(r){return null!=r},l={urlParamsEncoding:"default"};exports.Path=function(){function e(t,e){if(!t)throw new Error("Missing path in Path constructor");this.path=t,this.options=r.__assign(r.__assign({},l),e),this.tokens=c(t),this.hasUrlParams=this.tokens.filter((function(r){return/^url-parameter/.test(r.type)})).length>0,this.hasSpatParam=this.tokens.filter((function(r){return/splat$/.test(r.type)})).length>0,this.hasMatrixParams=this.tokens.filter((function(r){return/matrix$/.test(r.type)})).length>0,this.hasQueryParams=this.tokens.filter((function(r){return/^query-parameter/.test(r.type)})).length>0,this.spatParams=this.getParams("url-parameter-splat"),this.urlParams=this.getParams(/^url-parameter/),this.queryParams=this.getParams("query-parameter"),this.params=this.urlParams.concat(this.queryParams),this.source=this.tokens.filter((function(r){return void 0!==r.regex})).map((function(r){return r.regex.source})).join("")}return e.createPath=function(r,t){return new e(r,t)},e.prototype.isQueryParam=function(r){return-1!==this.queryParams.indexOf(r)},e.prototype.isSpatParam=function(r){return-1!==this.spatParams.indexOf(r)},e.prototype.test=function(e,n){var a=this,i=r.__assign(r.__assign({caseSensitive:!1,strictTrailingSlash:!1},this.options),n),s=function(r,t){return t?r:"\\/"===r?r:r.replace(/\\\/$/,"")+"(?:\\/)?"}(this.source,i.strictTrailingSlash),u=this.urlTest(e,s+(this.hasQueryParams?"(\\?.*$|$)":"$"),i.caseSensitive,i.urlParamsEncoding);if(!u||!this.hasQueryParams)return u;var o=t.parse(e,i.queryParams);return 0===Object.keys(o).filter((function(r){return!a.isQueryParam(r)})).length?(Object.keys(o).forEach((function(r){return u[r]=o[r]})),u):null},e.prototype.partialTest=function(e,n){var a=this,i=r.__assign(r.__assign({caseSensitive:!1,delimited:!0},this.options),n),s=function(r,t){return t?/(\/)$/.test(r)?r:r+"(\\/|\\?|\\.|;|$)":r}(this.source,i.delimited),u=this.urlTest(e,s,i.caseSensitive,i.urlParamsEncoding);if(!u)return u;if(!this.hasQueryParams)return u;var o=t.parse(e,i.queryParams);return Object.keys(o).filter((function(r){return a.isQueryParam(r)})).forEach((function(r){return function(r,t,e){void 0===e&&(e="");var n=r[t];return r[t]=void 0===n?e:Array.isArray(n)?n.concat(e):[n,e],r}(u,r,o[r])})),u},e.prototype.build=function(e,n){var a=this;void 0===e&&(e={});var i=r.__assign(r.__assign({ignoreConstraints:!1,ignoreSearch:!1,queryParams:{}},this.options),n),o=Object.keys(e).filter((function(r){return!a.isQueryParam(r)})).reduce((function(r,t){if(!p(e[t]))return r;var n=e[t],u=a.isSpatParam(t);return r[t]="boolean"==typeof n?n:Array.isArray(n)?n.map((function(r){return s(r,i.urlParamsEncoding,u)})):s(n,i.urlParamsEncoding,u),r}),{});if(this.urlParams.some((function(r){return!p(e[r])}))){var c=this.urlParams.filter((function(r){return!p(e[r])}));throw new Error("Cannot build path: '"+this.path+"' requires missing parameters { "+c.join(", ")+" }")}if(!i.ignoreConstraints&&!this.tokens.filter((function(r){return/^url-parameter/.test(r.type)&&!/-splat$/.test(r.type)})).every((function(r){return new RegExp("^"+u(r.otherVal[0])+"$").test(o[r.val])})))throw new Error("Some parameters of '"+this.path+"' are of invalid format");var l=this.tokens.filter((function(r){return!1===/^query-parameter/.test(r.type)})).map((function(r){return"url-parameter-matrix"===r.type?";"+r.val+"="+o[r.val[0]]:/^url-parameter/.test(r.type)?o[r.val[0]]:r.match})).join("");if(i.ignoreSearch)return l;var m=this.queryParams.filter((function(r){return-1!==Object.keys(e).indexOf(r)})).reduce((function(r,t){return r[t]=e[t],r}),{}),h=t.build(m,i.queryParams);return h?l+"?"+h:l},e.prototype.getParams=function(r){var t=r instanceof RegExp?function(t){return r.test(t.type)}:function(t){return t.type===r};return this.tokens.filter(t).map((function(r){return r.val[0]}))},e.prototype.urlTest=function(r,t,e,n){var a=this,s=new RegExp("^"+t,e?"":"i"),u=r.match(s);return u?this.urlParams.length?u.slice(1,this.urlParams.length+1).reduce((function(r,t,e){return r[a.urlParams[e]]=(i[n]||decodeURIComponent)(t),r}),{}):{}:null},e}();
//# sourceMappingURL=path-parser.cjs.production.min.js.map
;