maishu-router
Advanced tools
Comparing version 1.4.0 to 1.4.2
@@ -12,1 +12,2 @@ import { RouterItem } from "./route-item"; | ||
} | ||
export declare function parseRouterString(routerString: string): RouterItem[]; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.RouterItems = void 0; | ||
const errors_1 = require("./errors"); | ||
exports.parseRouterString = exports.RouterItems = void 0; | ||
const route_item_1 = require("./route-item"); | ||
let signals = { | ||
star: "*", | ||
colon: ":", | ||
question: "?" | ||
}; | ||
class RouterItems { | ||
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; | ||
let arr = routerString.split("/").filter(o => o); | ||
for (let i = 0; i < arr.length; i++) { | ||
let item = new route_item_1.RouterItem(arr[i]); | ||
let isExists = this._items.filter(o => o.name == item.name).length > 0; | ||
if (isExists) | ||
throw errors_1.errors.routeItemExists(arr[i]); | ||
this._items.push(item); | ||
} | ||
// this._items.push(item); | ||
// } | ||
this._items = parseRouterString(routerString); | ||
} | ||
@@ -39,1 +44,61 @@ get all() { | ||
exports.RouterItems = RouterItems; | ||
function parseRouterString(routerString) { | ||
if (routerString.endsWith("/") == false) | ||
routerString = routerString + "/"; | ||
// let routeRegex: string | undefined; | ||
let nameStart; | ||
let nameEnd; | ||
let regexStart; | ||
let regexEnd; | ||
let routerItems = []; | ||
let currentChar; | ||
let previousChar; | ||
for (let i = 0; i < routerString.length; i++) { | ||
previousChar = currentChar; | ||
currentChar = routerString[i]; | ||
if (currentChar == '/') { | ||
if (previousChar == '\\') | ||
continue; | ||
if (nameStart == undefined) { | ||
nameStart = i + 1; | ||
} | ||
else if (nameStart != undefined && nameEnd == undefined && regexStart == undefined) { | ||
nameEnd = i - 1; | ||
} | ||
else if (regexStart != undefined) { | ||
regexEnd = i - 1; | ||
} | ||
if (nameStart != undefined && nameEnd != undefined) { | ||
console.assert(nameEnd > nameStart); | ||
var routeSegment = routerString.substring(nameStart, nameEnd + 1); | ||
let routeItem = new route_item_1.RouterItem(routeSegment); | ||
if (regexStart != undefined && regexEnd != undefined) { | ||
var routeRegex = routerString.substring(regexStart, regexEnd + 1); | ||
// 替换 \/ 为 / | ||
routeRegex = routeRegex.replace(/\\\//, '/'); | ||
routeItem.regexp = new RegExp(routeRegex); | ||
} | ||
routerItems.push(routeItem); | ||
nameStart = undefined; | ||
nameEnd = undefined; | ||
regexStart = undefined; | ||
regexEnd = undefined; | ||
if (i + 1 <= routerString.length - 1) { | ||
nameStart = i + 1; | ||
} | ||
} | ||
} | ||
else if (currentChar == '#') { | ||
regexStart = i + 1; | ||
nameEnd = i - 1; | ||
} | ||
else if (i == routerString.length - 1) { | ||
if (nameStart != undefined && nameEnd == undefined && regexStart == undefined) | ||
nameEnd = i; | ||
else if (regexStart != undefined) | ||
regexEnd = i; | ||
} | ||
} | ||
return routerItems; | ||
} | ||
exports.parseRouterString = parseRouterString; |
{ | ||
"name": "maishu-router", | ||
"version": "1.4.0", | ||
"version": "1.4.2", | ||
"author": "shu mai", | ||
@@ -5,0 +5,0 @@ "dependencies": { |
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
37850
427