maishu-router
Advanced tools
Comparing version 1.4.5 to 1.4.8
/*! | ||
* | ||
* maishu-router v1.3.8 | ||
* maishu-router v1.4.6 | ||
* | ||
*/ | ||
!function(e,t){if("object"==typeof exports&&"object"==typeof module)module.exports=t(require("maishu-toolkit"));else if("function"==typeof define&&define.amd)define(["maishu-toolkit"],t);else{var r="object"==typeof exports?t(require("maishu-toolkit")):t(e["maishu-toolkit"]);for(var n in r)("object"==typeof exports?exports:e)[n]=r[n]}}("undefined"==typeof window?global:window,(function(e){return function(e){var t={};function r(n){if(t[n])return t[n].exports;var i=t[n]={i:n,l:!1,exports:{}};return e[n].call(i.exports,i,i.exports,r),i.l=!0,i.exports}return r.m=e,r.c=t,r.d=function(e,t,n){r.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:n})},r.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},r.t=function(e,t){if(1&t&&(e=r(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var n=Object.create(null);if(r.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var i in e)r.d(n,i,function(t){return e[t]}.bind(null,i));return n},r.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return r.d(t,"a",t),t},r.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},r.p="",r(r.s=4)}([function(e,t,r){"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.errors=void 0;const n=r(2);class i extends n.Errors{invalidRouteSegment(e){let t=new Error(`Route segment ${e} is invalid.`);return t.name=i.prototype.invalidRouteSegment.name,t}routeItemExists(e){let t=new Error(`Route '${e}' is exists.`);return t.name=i.prototype.routeItemExists.name,t}invalidPath(e){let t=new Error(`Path '${e}' is invalid path.`);return t.name=i.prototype.invalidPath.name,t}}t.errors=new i},function(e,t,r){"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.Router=void 0;const n=r(2),i=r(0),s=r(5),o=r(6);t.Router=class{constructor(e,t){if(!e)throw i.errors.argumentNull("pattern");this.rules=t||{},this._pattern=e}match(e){let t=new s.PathSegment(e),r=this.createRouterItems(this._pattern);for(;;){let e=t.else?n.pathConcat(t.current,t.else):t.current,i=r.current.isWildcards?r.current.regexp.test(e):r.current.regexp.test(t.current);if(i){if(r.current.isWildcards){r.current.value=e;break}r.current.value=t.current}else{if(!i&&!r.current.isOption)return null;if(!i&&r.current.isOption){r.moveNext();continue}}if(!r.moveNext())break;if(!t.moveNext())break}let i={};for(let e=0;e<r.all.length;e++){let t=r.all[e].name,n=r.all[e].value;null!=t&&(i[t]=n)}return r.current.isWildcards||""==t.else?i:null}createRouterItems(e){var t=new o.RouterItems(e);for(let e=0;e<t.all.length;e++){let r=t.all[e].name;null!=r&&null!=this.rules[r]&&(t.all[e].regexp=this.rules[r])}return t}get pattern(){return this._pattern}}},function(t,r){t.exports=e},function(e,t,r){"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.RouterItem=void 0;const n=r(0);t.RouterItem=class{constructor(e){this._isOption=!1,this._isWildcards=!1,this._name=null;let t=/[*|:|?]{0,1}(\S+)/.exec(e);if(!t)throw n.errors.invalidRouteSegment(e);switch(e[0]){case"*":this._isWildcards=!0,this._regexp=/\S+/,this._name=t[1];break;case":":this._regexp=/\S+/,this._name=t[1];break;case"?":this._regexp=/\S+/,this._name=t[1],this._isOption=!0;break;default:this._regexp=new RegExp(e)}}get isOption(){return this._isOption}get isWildcards(){return this._isWildcards}get name(){return this._name}get regexp(){return this._regexp}set regexp(e){this._regexp=e}get value(){return this._value}set value(e){this._value=e}}},function(e,t,r){"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.createRouter=t.RouterItem=t.Router=void 0;const n=r(1);var i=r(1);Object.defineProperty(t,"Router",{enumerable:!0,get:function(){return i.Router}});var s=r(3);Object.defineProperty(t,"RouterItem",{enumerable:!0,get:function(){return s.RouterItem}}),t.createRouter=function(e,t){return new n.Router(e,t)}},function(e,t,r){"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.PathSegment=void 0;const n=r(0);t.PathSegment=class{constructor(e){if(this._index=0,this.segments=e.split("/").filter(e=>e),0==this.segments.length)throw n.errors.invalidPath(e)}reset(){this._index=0}moveNext(){return this._index>=this.segments.length-1?null:(this._index=this._index+1,this._index)}get current(){return this.segments[this._index]}get index(){return this._index}get else(){return(this.index>=this.segments.length-1?[]:this.segments.slice(this.index+1)).join("/")}}},function(e,t,r){"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.RouterItems=void 0;const n=r(0),i=r(3);t.RouterItems=class{constructor(e){this._items=[],this._index=0;let t=e.split("/").filter(e=>e);for(let e=0;e<t.length;e++){let r=new i.RouterItem(t[e]);if(this._items.filter(e=>e.name==r.name).length>0)throw n.errors.routeItemExists(t[e]);this._items.push(r)}}get all(){return this._items}moveNext(){return this._index>=this._items.length-1?null:(this._index=this._index+1,this._index)}reset(){this._index=0}get index(){return this._index}get current(){return this._items[this._index]}}}])})); | ||
!function(e,t){if("object"==typeof exports&&"object"==typeof module)module.exports=t(require("maishu-toolkit"));else if("function"==typeof define&&define.amd)define(["maishu-toolkit"],t);else{var r="object"==typeof exports?t(require("maishu-toolkit")):t(e["maishu-toolkit"]);for(var n in r)("object"==typeof exports?exports:e)[n]=r[n]}}("undefined"==typeof window?global:window,(function(e){return function(e){var t={};function r(n){if(t[n])return t[n].exports;var i=t[n]={i:n,l:!1,exports:{}};return e[n].call(i.exports,i,i.exports,r),i.l=!0,i.exports}return r.m=e,r.c=t,r.d=function(e,t,n){r.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:n})},r.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},r.t=function(e,t){if(1&t&&(e=r(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var n=Object.create(null);if(r.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var i in e)r.d(n,i,function(t){return e[t]}.bind(null,i));return n},r.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return r.d(t,"a",t),t},r.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},r.p="",r(r.s=4)}([function(e,t,r){"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.errors=void 0;const n=r(2);class i extends n.Errors{invalidRouteSegment(e){let t=new Error(`Route segment ${e} is invalid.`);return t.name=i.prototype.invalidRouteSegment.name,t}routeItemExists(e){let t=new Error(`Route '${e}' is exists.`);return t.name=i.prototype.routeItemExists.name,t}invalidPath(e){let t=new Error(`Path '${e}' is invalid path.`);return t.name=i.prototype.invalidPath.name,t}}t.errors=new i},function(e,t,r){"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.Router=void 0;const n=r(2),i=r(0),s=r(5),u=r(6);t.Router=class{constructor(e,t){if(!e)throw i.errors.argumentNull("pattern");this.rules=t||{},this._pattern=e}match(e){let t=new s.PathSegment(e),r=this.createRouterItems(this._pattern);for(;;){let e=t.else?n.pathConcat(t.current,t.else):t.current,i=r.current.isWildcards?r.current.regexp.test(e):r.current.regexp.test(t.current);if(i){if(r.current.isWildcards){r.current.value=e;break}r.current.value=t.current}else{if(!i&&!r.current.isOption)return null;if(!i&&r.current.isOption){r.moveNext();continue}}if(!r.moveNext())break;if(!t.moveNext())break}let i={};for(let e=0;e<r.all.length;e++){let t=r.all[e].name,n=r.all[e].value;null!=t&&(i[t]=n)}if(!r.current.isWildcards&&""!=t.else)return null;for(let e=0;e<r.all.length;e++)if(null==r.all[e].value&&!r.all[e].isOption&&!r.all[e].isWildcards)return null;return i}createRouterItems(e){var t=new u.RouterItems(e);for(let e=0;e<t.all.length;e++){let r=t.all[e].name;null!=r&&null!=this.rules[r]&&(t.all[e].regexp=this.rules[r])}return t}get pattern(){return this._pattern}}},function(t,r){t.exports=e},function(e,t,r){"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.RouterItem=void 0;const n=r(0);t.RouterItem=class{constructor(e){this._isOption=!1,this._isWildcards=!1,this._name=null;let t=/[*|:|?]{0,1}(\S+)/.exec(e);if(!t)throw n.errors.invalidRouteSegment(e);switch(e[0]){case"*":this._isWildcards=!0,this._regexp=/\S+/,this._name=t[1];break;case":":this._regexp=/\S+/,this._name=t[1];break;case"?":this._regexp=/\S+/,this._name=t[1],this._isOption=!0;break;default:this._regexp=new RegExp(e)}}get isOption(){return this._isOption}get isWildcards(){return this._isWildcards}get name(){return this._name}get regexp(){return this._regexp}set regexp(e){this._regexp=e}get value(){return this._value}set value(e){this._value=e}}},function(e,t,r){"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.createRouter=t.RouterItem=t.Router=void 0;const n=r(1);var i=r(1);Object.defineProperty(t,"Router",{enumerable:!0,get:function(){return i.Router}});var s=r(3);Object.defineProperty(t,"RouterItem",{enumerable:!0,get:function(){return s.RouterItem}}),t.createRouter=function(e,t){return new n.Router(e,t)}},function(e,t,r){"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.PathSegment=void 0;const n=r(0);t.PathSegment=class{constructor(e){if(this._index=0,this.segments=e.split("/").filter(e=>e),0==this.segments.length)throw n.errors.invalidPath(e)}reset(){this._index=0}moveNext(){return this._index>=this.segments.length-1?null:(this._index=this._index+1,this._index)}get current(){return this.segments[this._index]}get index(){return this._index}get else(){return(this.index>=this.segments.length-1?[]:this.segments.slice(this.index+1)).join("/")}}},function(e,t,r){"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.parseRouterString=t.RouterItems=void 0;const n=r(3);function i(e){let t,r,i,s;0==e.endsWith("/")&&(e+="/");let u,o,l=[];for(let d=0;d<e.length;d++)if(o=u,u=e[d],"/"==u){if("\\"==o)continue;if(null==t?t=d+1:null!=t&&null==r&&null==i?r=d-1:null!=i&&(s=d-1),null!=t&&null!=r){console.assert(r>t);var a=e.substring(t,r+1);let u=new n.RouterItem(a);if(null!=i&&null!=s){var c=e.substring(i,s+1);c=c.replace(/\\\//,"/"),u.regexp=new RegExp(c)}l.push(u),t=void 0,r=void 0,i=void 0,s=void 0,d+1<=e.length-1&&(t=d+1)}}else"#"==u?(i=d+1,r=d-1):d==e.length-1&&(null!=t&&null==r&&null==i?r=d:null!=i&&(s=d));return l}t.RouterItems=class{constructor(e){this._items=[],this._index=0,this._items=i(e)}get all(){return this._items}moveNext(){return this._index>=this._items.length-1?null:(this._index=this._index+1,this._index)}reset(){this._index=0}get index(){return this._index}get current(){return this._items[this._index]}},t.parseRouterString=i}])})); | ||
//# sourceMappingURL=index.js.map |
@@ -12,12 +12,4 @@ "use strict"; | ||
constructor(routerString) { | ||
// let arr = routerString.split("/").filter(o => o); | ||
// for (let i = 0; i < arr.length; i++) { | ||
// let item = new RouterItem(arr[i]); | ||
// let isExists = this._items.filter(o => o.name == item.name).length > 0; | ||
// if (isExists) | ||
// throw errors.routeItemExists(arr[i]); | ||
this._items = []; | ||
this._index = 0; | ||
// this._items.push(item); | ||
// } | ||
this._items = parseRouterString(routerString); | ||
@@ -24,0 +16,0 @@ } |
@@ -54,3 +54,3 @@ "use strict"; | ||
for (let i = 0; i < c.all.length; i++) { | ||
if (c.all[i].value == null && !c.all[i].isOption) | ||
if (c.all[i].value == null && !(c.all[i].isOption || c.all[i].isWildcards)) | ||
return null; | ||
@@ -57,0 +57,0 @@ } |
{ | ||
"name": "maishu-router", | ||
"version": "1.4.5", | ||
"version": "1.4.8", | ||
"author": "shu mai", | ||
@@ -24,2 +24,2 @@ "dependencies": { | ||
} | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
41628
426