Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

maishu-router

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

maishu-router - npm Package Compare versions

Comparing version 1.4.8 to 1.4.9

4

dist/index.js
/*!
*
* maishu-router v1.4.6
* maishu-router v1.4.9
*
*/
!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}])}));
!function(e,t){if("object"==typeof exports&&"object"==typeof module)module.exports=t();else if("function"==typeof define&&define.amd)define([],t);else{var r=t();for(var n in r)("object"==typeof exports?exports:e)[n]=r[n]}}("undefined"==typeof window?global:window,(function(){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(6);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(5),i=r(0),u=r(7),s=r(2);t.Router=class{constructor(e,t){if(!e)throw i.errors.argumentNull("pattern");this.rules=t||{},this._pattern=e}match(e){let t=new u.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,t.end();break}if(r.current.value=t.current,t.moveNext(),!r.moveNext()||!t.current)break}else{if(!i&&!r.current.isOption)return null;if(!i&&r.current.isOption){if(!r.moveNext())break;continue}}}if(null!=t.current)return null;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 i}createRouterItems(e){var t=new s.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(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,u;0==e.endsWith("/")&&(e+="/");let s,l,o=[];for(let d=0;d<e.length;d++)if(l=s,s=e[d],"/"==s){if("\\"==l)continue;if(null==t?t=d+1:null!=t&&null==r&&null==i?r=d-1:null!=i&&(u=d-1),null!=t&&null!=r){console.assert(r>t);var a=e.substring(t,r+1);let s=new n.RouterItem(a);if(null!=i&&null!=u){var c=e.substring(i,u+1);c=c.replace(/\\\//,"/"),s.regexp=new RegExp(c)}o.push(s),t=void 0,r=void 0,i=void 0,u=void 0,d+1<=e.length-1&&(t=d+1)}}else"#"==s?(i=d+1,r=d-1):d==e.length-1&&(null!=t&&null==r&&null==i?r=d:null!=i&&(u=d));return o}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&&(this._index=this._index+1),this._index>this._items.length-1?null:this._index}reset(){this._index=0}get index(){return this._index}get current(){return this._items[this._index]}},t.parseRouterString=i},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.parseRouterString=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 u=r(3);Object.defineProperty(t,"RouterItem",{enumerable:!0,get:function(){return u.RouterItem}});var s=r(2);Object.defineProperty(t,"parseRouterString",{enumerable:!0,get:function(){return s.parseRouterString}}),t.createRouter=function(e,t){return new n.Router(e,t)}},function(e,t,r){"use strict";function n(...e){if(0==(e=e||[]).length)return"";if(1==e.length)return e[0];let t=e.join("/");return t=t.replace(/(\/+|\\+)/g,"/"),t=t.replace("http:/","http://"),t=t.replace("https:/","https://"),t}r.r(t),r.d(t,"pathConcat",(function(){return n}))},function(e,t,r){"use strict";r.r(t),r.d(t,"Errors",(function(){return n})),r.d(t,"errors",(function(){return i}));class n{argumentNull(e){let t=new Error(`Argument ${e} cannt be null or emtpy.`);return t.name="argumentNull",t}routeDataFieldNull(e){let t=new Error(`The ${e} field of route data cannt be null.`);return t.name="routeDataFieldNull",t}argumentFieldNull(e,t){let r=new Error(`The ${e} field of ${t} cannt be null.`);return r.name="argumentFieldNull",r}argumentTypeIncorrect(e,t){let r=new Error(`Argument ${e} type error, expected type is ${t}.`);return r.name="argumentTypeIncorrect",r}}let i=new n},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&&(this._index=this._index+1),this._index>this.segments.length-1?null:this._index}end(){this._index=this.segments.length}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("/")}}}])}));
//# sourceMappingURL=index.js.map

@@ -1,2 +0,2 @@

import { Errors } from "maishu-toolkit";
import { Errors } from "maishu-toolkit/out/errors";
declare class MyErrors extends Errors {

@@ -3,0 +3,0 @@ invalidRouteSegment(routeSegment: string): Error;

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.errors = void 0;
const maishu_toolkit_1 = require("maishu-toolkit");
class MyErrors extends maishu_toolkit_1.Errors {
const errors_1 = require("maishu-toolkit/out/errors");
class MyErrors extends errors_1.Errors {
invalidRouteSegment(routeSegment) {

@@ -7,0 +7,0 @@ let error = new Error(`Route segment ${routeSegment} is invalid.`);

import { Router } from "./route";
export { Router } from "./route";
export { RouterItem } from "./route-item";
export { parseRouterString } from "./route-item-collection";
export declare function createRouter(pattern: string, rules?: {
[key: string]: RegExp;
}): Router;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createRouter = exports.RouterItem = exports.Router = void 0;
exports.createRouter = exports.parseRouterString = exports.RouterItem = exports.Router = void 0;
const route_1 = require("./route");

@@ -9,2 +9,4 @@ var route_2 = require("./route");

Object.defineProperty(exports, "RouterItem", { enumerable: true, get: function () { return route_item_1.RouterItem; } });
var route_item_collection_1 = require("./route-item-collection");
Object.defineProperty(exports, "parseRouterString", { enumerable: true, get: function () { return route_item_collection_1.parseRouterString; } });
function createRouter(pattern, rules) {

@@ -11,0 +13,0 @@ let router = new route_1.Router(pattern, rules);

@@ -8,2 +8,3 @@ /** 路径分割,用于比较两个路径 */

moveNext(): number | null;
end(): void;
get current(): string;

@@ -10,0 +11,0 @@ get index(): number;

@@ -17,7 +17,12 @@ "use strict";

moveNext() {
if (this._index >= this.segments.length - 1)
if (this._index < this.segments.length) {
this._index = this._index + 1;
}
if (this._index > this.segments.length - 1)
return null;
this._index = this._index + 1;
return this._index;
}
end() {
this._index = this.segments.length;
}
get current() {

@@ -24,0 +29,0 @@ return this.segments[this._index];

@@ -20,5 +20,7 @@ "use strict";

moveNext() {
if (this._index >= this._items.length - 1)
if (this._index < this._items.length) {
this._index = this._index + 1;
}
if (this._index > this._items.length - 1)
return null;
this._index = this._index + 1;
return this._index;

@@ -25,0 +27,0 @@ }

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Router = void 0;
const maishu_toolkit_1 = require("maishu-toolkit");
const path_1 = require("maishu-toolkit/out/path");
const errors_1 = require("./errors");

@@ -19,3 +19,3 @@ const path_segment_1 = require("./path-segment");

do {
let wildcardsName = p.else ? maishu_toolkit_1.pathConcat(p.current, p.else) : p.current;
let wildcardsName = p.else ? path_1.pathConcat(p.current, p.else) : p.current;
let testOK = c.current.isWildcards ? c.current.regexp.test(wildcardsName) : c.current.regexp.test(p.current);

@@ -25,2 +25,3 @@ if (testOK) {

c.current.value = wildcardsName;
p.end();
break;

@@ -30,3 +31,7 @@ }

c.current.value = p.current;
p.moveNext();
}
let cr = c.moveNext();
if (!cr || !p.current)
break;
}

@@ -37,11 +42,11 @@ else if (!testOK && !c.current.isOption) {

else if (!testOK && c.current.isOption) {
c.moveNext();
let r = c.moveNext();
if (!r)
break;
continue;
}
if (!c.moveNext())
break;
if (!p.moveNext()) {
break;
}
} while (true);
if (p.current != null) {
return null;
}
let r = {};

@@ -55,8 +60,8 @@ for (let i = 0; i < c.all.length; i++) {

}
if (!c.current.isWildcards && p.else != "")
return null;
for (let i = 0; i < c.all.length; i++) {
if (c.all[i].value == null && !(c.all[i].isOption || c.all[i].isWildcards))
return null;
}
// if (!c.current.isWildcards && p.else != "")
// return null;
// for (let i = 0; i < c.all.length; i++) {
// if (c.all[i].value == null && !(c.all[i].isOption || c.all[i].isWildcards))
// return null;
// }
return r;

@@ -63,0 +68,0 @@ }

{
"name": "maishu-router",
"version": "1.4.8",
"version": "1.4.9",
"author": "shu mai",

@@ -22,4 +22,5 @@ "dependencies": {

"scripts": {
"build": "webpack"
"build": "webpack",
"test": "mocha"
}
}

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc