find-my-way
Advanced tools
Comparing version 5.2.0 to 5.3.0
@@ -22,4 +22,4 @@ 'use strict' | ||
this.unconstrainedHandler = options.unconstrainedHandler || null // optimized reference to the handler that will match most of the time | ||
this.children = options.children || {} | ||
this.numberOfChildren = Object.keys(this.children).length | ||
this.staticChildren = options.staticChildren || {} | ||
this.numberOfChildren = Object.keys(this.staticChildren).length | ||
this.kind = options.kind || this.types.STATIC | ||
@@ -41,6 +41,10 @@ this.regex = options.regex || null | ||
Node.prototype.addChild = function (node) { | ||
var label = '' | ||
const label = node.prefix[0] | ||
switch (node.kind) { | ||
case this.types.STATIC: | ||
label = node.prefix[0] | ||
assert( | ||
this.staticChildren[label] === undefined, | ||
`There is already a child with label '${label}'` | ||
) | ||
this.staticChildren[label] = node | ||
break | ||
@@ -50,8 +54,8 @@ case this.types.PARAM: | ||
case this.types.MULTI_PARAM: | ||
assert(this.parametricChild === null, 'There is already a parametric child') | ||
this.parametricChild = node | ||
label = ':' | ||
break | ||
case this.types.MATCH_ALL: | ||
assert(this.wildcardChild === null, 'There is already a wildcard child') | ||
this.wildcardChild = node | ||
label = '*' | ||
break | ||
@@ -62,8 +66,2 @@ default: | ||
assert( | ||
this.children[label] === undefined, | ||
`There is already a child with label '${label}'` | ||
) | ||
this.children[label] = node | ||
this.numberOfChildren++ | ||
@@ -86,7 +84,5 @@ | ||
if (parametricBrother) { | ||
for (const child of Object.values(this.children)) { | ||
if (child && child !== parametricBrother) { | ||
child.parametricBrother = parametricBrother | ||
child._saveParametricBrother(parametricBrother) | ||
} | ||
for (const child of Object.values(this.staticChildren)) { | ||
child.parametricBrother = parametricBrother | ||
child._saveParametricBrother(parametricBrother) | ||
} | ||
@@ -105,8 +101,9 @@ } | ||
if (wildcardBrother) { | ||
for (const child of Object.values(this.children)) { | ||
if (child && child !== wildcardBrother) { | ||
child.wildcardBrother = wildcardBrother | ||
child._saveWildcardBrother(wildcardBrother) | ||
} | ||
for (const child of Object.values(this.staticChildren)) { | ||
child.wildcardBrother = wildcardBrother | ||
child._saveWildcardBrother(wildcardBrother) | ||
} | ||
if (this.parametricChild !== null) { | ||
this.parametricChild.wildcardBrother = wildcardBrother | ||
} | ||
} | ||
@@ -117,3 +114,3 @@ } | ||
this.prefix = prefix | ||
this.children = {} | ||
this.staticChildren = {} | ||
this.handlers = [] | ||
@@ -135,3 +132,3 @@ this.unconstrainedHandler = null | ||
prefix: this.prefix.slice(length), | ||
children: this.children, | ||
staticChildren: this.staticChildren, | ||
kind: this.kind, | ||
@@ -160,8 +157,4 @@ method: this.method, | ||
Node.prototype.findByLabel = function (path) { | ||
return this.children[path[0]] | ||
} | ||
Node.prototype.findStaticMatchingChild = function (path, pathIndex) { | ||
const child = this.children[path[pathIndex]] | ||
const child = this.staticChildren[path[pathIndex]] | ||
if (child !== undefined) { | ||
@@ -168,0 +161,0 @@ for (let i = 0; i < child.prefix.length; i++) { |
53
index.js
@@ -168,5 +168,4 @@ 'use strict' | ||
if (i !== len - 1 && path.charCodeAt(i + 1) === 58) { | ||
// It's a double colon. Let's just replace it with a single colon and go ahead | ||
path = path.slice(0, i) + path.slice(i + 1) | ||
len = path.length | ||
// It's a double colon. Let's just skip it with and go ahead | ||
i += 2 | ||
continue | ||
@@ -255,7 +254,2 @@ } | ||
const route = path | ||
var prefix = '' | ||
var pathLen = 0 | ||
var prefixLen = 0 | ||
var len = 0 | ||
var max = 0 | ||
var node = null | ||
@@ -271,14 +265,19 @@ | ||
while (true) { | ||
prefix = currentNode.prefix | ||
prefixLen = prefix.length | ||
pathLen = path.length | ||
len = 0 | ||
const prefix = currentNode.prefix | ||
let len = 0 | ||
// search for the longest common prefix | ||
max = pathLen < prefixLen ? pathLen : prefixLen | ||
while (len < max && path[len] === prefix[len]) len++ | ||
for (; len < Math.min(path.length, prefix.length); len++) { | ||
if (path.charCodeAt(len) === 58 && path.charCodeAt(len + 1) === 58) { | ||
path = path.slice(0, len) + path.slice(len + 1) | ||
} | ||
if (path[len] !== prefix[len]) { | ||
break | ||
} | ||
} | ||
// the longest common prefix is smaller than the current prefix | ||
// let's split the node and add a new child | ||
if (len < prefixLen) { | ||
if (len < prefix.length) { | ||
node = currentNode.split(len) | ||
@@ -288,3 +287,3 @@ | ||
// the handler should be added to the current node, to a child otherwise | ||
if (len === pathLen) { | ||
if (len === path.length) { | ||
assert(!currentNode.getHandler(constraints), `Method '${method}' already declared for route '${route}' with constraints '${JSON.stringify(constraints)}'`) | ||
@@ -308,7 +307,18 @@ currentNode.addHandler(handler, params, store, constraints) | ||
// but is higher than the prefix | ||
} else if (len < pathLen) { | ||
} else if (len < path.length) { | ||
// remove the prefix | ||
path = path.slice(len) | ||
// check if there is a child with the label extracted from the new path | ||
node = currentNode.findByLabel(path) | ||
if (path.charCodeAt(0) === 58) { | ||
if (path.charCodeAt(1) === 58) { | ||
node = currentNode.staticChildren[':'] | ||
} else { | ||
node = currentNode.parametricChild | ||
} | ||
} else if (path.charCodeAt(0) === 42) { | ||
node = currentNode.wildcardChild | ||
} else { | ||
node = currentNode.staticChildren[path[0]] | ||
} | ||
// there is a child within the given label, we must go deepen in the tree | ||
@@ -319,2 +329,9 @@ if (node) { | ||
} | ||
for (let i = 0; i < path.length; i++) { | ||
if (path.charCodeAt(i) === 58 && path.charCodeAt(i + 1) === 58) { | ||
path = path.slice(0, i) + path.slice(i + 1) | ||
} | ||
} | ||
// there are not children within the given label, let's create a new one! | ||
@@ -321,0 +338,0 @@ node = new Node({ method: method, prefix: path, kind: kind, handlers: null, regex: regex, constrainer: this.constrainer }) |
@@ -256,22 +256,30 @@ 'use strict' | ||
if (node.children) { | ||
for (const child of Object.values(node.children)) { | ||
// split on the slash separator but use a regex to lookahead and not actually match it, preserving it in the returned string segments | ||
const childPrefixSegments = child.prefix.split(pathRegExp) | ||
let cursor = flattened | ||
let parent | ||
for (const segment of childPrefixSegments) { | ||
parent = cursor | ||
cursor = cursor.children[segment] | ||
if (!cursor) { | ||
cursor = { | ||
prefix: segment, | ||
nodes: [], | ||
children: {} | ||
} | ||
parent.children[segment] = cursor | ||
const nodeChildren = Object.values(node.staticChildren) | ||
if (node.parametricChild) { | ||
nodeChildren.unshift(node.parametricChild) | ||
} | ||
if (node.wildcardChild) { | ||
nodeChildren.unshift(node.wildcardChild) | ||
} | ||
for (const child of nodeChildren) { | ||
// split on the slash separator but use a regex to lookahead and not actually match it, preserving it in the returned string segments | ||
const childPrefixSegments = child.prefix.split(pathRegExp) | ||
let cursor = flattened | ||
let parent | ||
for (const segment of childPrefixSegments) { | ||
parent = cursor | ||
cursor = cursor.children[segment] | ||
if (!cursor) { | ||
cursor = { | ||
prefix: segment, | ||
nodes: [], | ||
children: {} | ||
} | ||
parent.children[segment] = cursor | ||
} | ||
flattenNode(cursor, child) | ||
} | ||
flattenNode(cursor, child) | ||
} | ||
@@ -278,0 +286,0 @@ } |
{ | ||
"name": "find-my-way", | ||
"version": "5.2.0", | ||
"version": "5.3.0", | ||
"description": "Crazy fast http radix based router", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
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
269835
67
6910