route-node
Advanced tools
Comparing version 3.3.0 to 3.4.0
@@ -0,1 +1,16 @@ | ||
<a name="3.4.0"></a> | ||
# [3.4.0](https://github.com/troch/route-node/compare/v3.3.0...v3.4.0) (2018-08-06) | ||
### Features | ||
* add a method to sort all descendants ([1e2f4b7](https://github.com/troch/route-node/commit/1e2f4b7)) | ||
### Performance Improvements | ||
* improve route addition performance by controlling when sorting is performed ([07430f0](https://github.com/troch/route-node/commit/07430f0)) | ||
<a name="3.3.0"></a> | ||
@@ -2,0 +17,0 @@ # [3.3.0](https://github.com/troch/route-node/compare/v3.2.1...v3.3.0) (2018-07-11) |
@@ -201,3 +201,7 @@ 'use strict'; | ||
var sortChildren = (function (originalChildren) { return function (left, right) { | ||
function sortChildren(children) { | ||
var originalChildren = children.slice(0); | ||
return children.sort(sortPredicate(originalChildren)); | ||
} | ||
var sortPredicate = function (originalChildren) { return function (left, right) { | ||
var leftPath = left.path | ||
@@ -255,3 +259,3 @@ .replace(/<.*?>/g, '') | ||
return originalChildren.indexOf(left) - originalChildren.indexOf(right); | ||
}; }); | ||
}; }; | ||
@@ -264,6 +268,7 @@ var defaultBuildOptions = { | ||
var RouteNode = /** @class */ (function () { | ||
function RouteNode(name, path, childRoutes, cb, parent) { | ||
function RouteNode(name, path, childRoutes, cb, parent, finalSort, sort) { | ||
if (name === void 0) { name = ''; } | ||
if (path === void 0) { path = ''; } | ||
if (childRoutes === void 0) { childRoutes = []; } | ||
if (finalSort === void 0) { finalSort = true; } | ||
this.name = name; | ||
@@ -276,3 +281,6 @@ this.absolute = /^~/.test(path); | ||
this.checkParents(); | ||
this.add(childRoutes, cb); | ||
this.add(childRoutes, cb, finalSort ? false : sort !== false); | ||
if (finalSort) { | ||
this.sortDescendants(); | ||
} | ||
return this; | ||
@@ -295,4 +303,5 @@ } | ||
}; | ||
RouteNode.prototype.add = function (route, cb) { | ||
RouteNode.prototype.add = function (route, cb, sort) { | ||
var _this = this; | ||
if (sort === void 0) { sort = true; } | ||
if (route === undefined || route === null) { | ||
@@ -302,3 +311,3 @@ return; | ||
if (route instanceof Array) { | ||
route.forEach(function (r) { return _this.add(r, cb); }); | ||
route.forEach(function (r) { return _this.add(r, cb, sort); }); | ||
return; | ||
@@ -311,3 +320,3 @@ } | ||
route.setParent(this); | ||
this.addRouteNode(route); | ||
this.addRouteNode(route, sort); | ||
} | ||
@@ -318,3 +327,3 @@ else { | ||
} | ||
var routeNode = new RouteNode(route.name, route.path, route.children, cb, this); | ||
var routeNode = new RouteNode(route.name, route.path, route.children, cb, this, false, sort); | ||
var fullName = routeNode | ||
@@ -327,3 +336,3 @@ .getParentSegments([routeNode]) | ||
} | ||
this.addRouteNode(routeNode); | ||
this.addRouteNode(routeNode, sort); | ||
} | ||
@@ -342,2 +351,11 @@ return this; | ||
}; | ||
RouteNode.prototype.sortChildren = function () { | ||
if (this.children.length) { | ||
sortChildren(this.children); | ||
} | ||
}; | ||
RouteNode.prototype.sortDescendants = function () { | ||
this.sortChildren(); | ||
this.children.forEach(function (child) { return child.sortDescendants(); }); | ||
}; | ||
RouteNode.prototype.buildPath = function (routeName, params, options) { | ||
@@ -383,3 +401,4 @@ if (params === void 0) { params = {}; } | ||
}; | ||
RouteNode.prototype.addRouteNode = function (route, cb) { | ||
RouteNode.prototype.addRouteNode = function (route, sort) { | ||
if (sort === void 0) { sort = true; } | ||
var names = route.name.split('.'); | ||
@@ -398,5 +417,5 @@ if (names.length === 1) { | ||
this.children.push(route); | ||
// Push greedy spats to the bottom of the pile | ||
var originalChildren = this.children.slice(0); | ||
this.children.sort(sortChildren(originalChildren)); | ||
if (sort) { | ||
this.sortChildren(); | ||
} | ||
} | ||
@@ -403,0 +422,0 @@ else { |
@@ -199,3 +199,7 @@ import { build, omit, parse } from 'search-params'; | ||
var sortChildren = (function (originalChildren) { return function (left, right) { | ||
function sortChildren(children) { | ||
var originalChildren = children.slice(0); | ||
return children.sort(sortPredicate(originalChildren)); | ||
} | ||
var sortPredicate = function (originalChildren) { return function (left, right) { | ||
var leftPath = left.path | ||
@@ -253,3 +257,3 @@ .replace(/<.*?>/g, '') | ||
return originalChildren.indexOf(left) - originalChildren.indexOf(right); | ||
}; }); | ||
}; }; | ||
@@ -262,6 +266,7 @@ var defaultBuildOptions = { | ||
var RouteNode = /** @class */ (function () { | ||
function RouteNode(name, path, childRoutes, cb, parent) { | ||
function RouteNode(name, path, childRoutes, cb, parent, finalSort, sort) { | ||
if (name === void 0) { name = ''; } | ||
if (path === void 0) { path = ''; } | ||
if (childRoutes === void 0) { childRoutes = []; } | ||
if (finalSort === void 0) { finalSort = true; } | ||
this.name = name; | ||
@@ -274,3 +279,6 @@ this.absolute = /^~/.test(path); | ||
this.checkParents(); | ||
this.add(childRoutes, cb); | ||
this.add(childRoutes, cb, finalSort ? false : sort !== false); | ||
if (finalSort) { | ||
this.sortDescendants(); | ||
} | ||
return this; | ||
@@ -293,4 +301,5 @@ } | ||
}; | ||
RouteNode.prototype.add = function (route, cb) { | ||
RouteNode.prototype.add = function (route, cb, sort) { | ||
var _this = this; | ||
if (sort === void 0) { sort = true; } | ||
if (route === undefined || route === null) { | ||
@@ -300,3 +309,3 @@ return; | ||
if (route instanceof Array) { | ||
route.forEach(function (r) { return _this.add(r, cb); }); | ||
route.forEach(function (r) { return _this.add(r, cb, sort); }); | ||
return; | ||
@@ -309,3 +318,3 @@ } | ||
route.setParent(this); | ||
this.addRouteNode(route); | ||
this.addRouteNode(route, sort); | ||
} | ||
@@ -316,3 +325,3 @@ else { | ||
} | ||
var routeNode = new RouteNode(route.name, route.path, route.children, cb, this); | ||
var routeNode = new RouteNode(route.name, route.path, route.children, cb, this, false, sort); | ||
var fullName = routeNode | ||
@@ -325,3 +334,3 @@ .getParentSegments([routeNode]) | ||
} | ||
this.addRouteNode(routeNode); | ||
this.addRouteNode(routeNode, sort); | ||
} | ||
@@ -340,2 +349,11 @@ return this; | ||
}; | ||
RouteNode.prototype.sortChildren = function () { | ||
if (this.children.length) { | ||
sortChildren(this.children); | ||
} | ||
}; | ||
RouteNode.prototype.sortDescendants = function () { | ||
this.sortChildren(); | ||
this.children.forEach(function (child) { return child.sortDescendants(); }); | ||
}; | ||
RouteNode.prototype.buildPath = function (routeName, params, options) { | ||
@@ -381,3 +399,4 @@ if (params === void 0) { params = {}; } | ||
}; | ||
RouteNode.prototype.addRouteNode = function (route, cb) { | ||
RouteNode.prototype.addRouteNode = function (route, sort) { | ||
if (sort === void 0) { sort = true; } | ||
var names = route.name.split('.'); | ||
@@ -396,5 +415,5 @@ if (names.length === 1) { | ||
this.children.push(route); | ||
// Push greedy spats to the bottom of the pile | ||
var originalChildren = this.children.slice(0); | ||
this.children.sort(sortChildren(originalChildren)); | ||
if (sort) { | ||
this.sortChildren(); | ||
} | ||
} | ||
@@ -401,0 +420,0 @@ else { |
@@ -661,3 +661,7 @@ (function (global, factory) { | ||
var sortChildren = (function (originalChildren) { return function (left, right) { | ||
function sortChildren(children) { | ||
var originalChildren = children.slice(0); | ||
return children.sort(sortPredicate(originalChildren)); | ||
} | ||
var sortPredicate = function (originalChildren) { return function (left, right) { | ||
var leftPath = left.path | ||
@@ -715,3 +719,3 @@ .replace(/<.*?>/g, '') | ||
return originalChildren.indexOf(left) - originalChildren.indexOf(right); | ||
}; }); | ||
}; }; | ||
@@ -724,6 +728,7 @@ var defaultBuildOptions = { | ||
var RouteNode = /** @class */ (function () { | ||
function RouteNode(name, path, childRoutes, cb, parent) { | ||
function RouteNode(name, path, childRoutes, cb, parent, finalSort, sort) { | ||
if (name === void 0) { name = ''; } | ||
if (path === void 0) { path = ''; } | ||
if (childRoutes === void 0) { childRoutes = []; } | ||
if (finalSort === void 0) { finalSort = true; } | ||
this.name = name; | ||
@@ -736,3 +741,6 @@ this.absolute = /^~/.test(path); | ||
this.checkParents(); | ||
this.add(childRoutes, cb); | ||
this.add(childRoutes, cb, finalSort ? false : sort !== false); | ||
if (finalSort) { | ||
this.sortDescendants(); | ||
} | ||
return this; | ||
@@ -755,4 +763,5 @@ } | ||
}; | ||
RouteNode.prototype.add = function (route, cb) { | ||
RouteNode.prototype.add = function (route, cb, sort) { | ||
var _this = this; | ||
if (sort === void 0) { sort = true; } | ||
if (route === undefined || route === null) { | ||
@@ -762,3 +771,3 @@ return; | ||
if (route instanceof Array) { | ||
route.forEach(function (r) { return _this.add(r, cb); }); | ||
route.forEach(function (r) { return _this.add(r, cb, sort); }); | ||
return; | ||
@@ -771,3 +780,3 @@ } | ||
route.setParent(this); | ||
this.addRouteNode(route); | ||
this.addRouteNode(route, sort); | ||
} | ||
@@ -778,3 +787,3 @@ else { | ||
} | ||
var routeNode = new RouteNode(route.name, route.path, route.children, cb, this); | ||
var routeNode = new RouteNode(route.name, route.path, route.children, cb, this, false, sort); | ||
var fullName = routeNode | ||
@@ -787,3 +796,3 @@ .getParentSegments([routeNode]) | ||
} | ||
this.addRouteNode(routeNode); | ||
this.addRouteNode(routeNode, sort); | ||
} | ||
@@ -802,2 +811,11 @@ return this; | ||
}; | ||
RouteNode.prototype.sortChildren = function () { | ||
if (this.children.length) { | ||
sortChildren(this.children); | ||
} | ||
}; | ||
RouteNode.prototype.sortDescendants = function () { | ||
this.sortChildren(); | ||
this.children.forEach(function (child) { return child.sortDescendants(); }); | ||
}; | ||
RouteNode.prototype.buildPath = function (routeName, params, options) { | ||
@@ -843,3 +861,4 @@ if (params === void 0) { params = {}; } | ||
}; | ||
RouteNode.prototype.addRouteNode = function (route, cb) { | ||
RouteNode.prototype.addRouteNode = function (route, sort) { | ||
if (sort === void 0) { sort = true; } | ||
var names = route.name.split('.'); | ||
@@ -858,5 +877,5 @@ if (names.length === 1) { | ||
this.children.push(route); | ||
// Push greedy spats to the bottom of the pile | ||
var originalChildren = this.children.slice(0); | ||
this.children.sort(sortChildren(originalChildren)); | ||
if (sort) { | ||
this.sortChildren(); | ||
} | ||
} | ||
@@ -863,0 +882,0 @@ else { |
@@ -76,3 +76,5 @@ import { Path } from 'path-parser' | ||
cb?: Callback, | ||
parent?: RouteNode | ||
parent?: RouteNode, | ||
finalSort: boolean = true, | ||
sort?: boolean | ||
) { | ||
@@ -88,4 +90,8 @@ this.name = name | ||
this.add(childRoutes, cb) | ||
this.add(childRoutes, cb, finalSort ? false : sort !== false) | ||
if (finalSort) { | ||
this.sortDescendants() | ||
} | ||
return this | ||
@@ -110,3 +116,7 @@ } | ||
public add(route: Route | Route[], cb?: Callback): this { | ||
public add( | ||
route: Route | Route[], | ||
cb?: Callback, | ||
sort: boolean = true | ||
): this { | ||
if (route === undefined || route === null) { | ||
@@ -117,3 +127,3 @@ return | ||
if (route instanceof Array) { | ||
route.forEach(r => this.add(r, cb)) | ||
route.forEach(r => this.add(r, cb, sort)) | ||
return | ||
@@ -128,3 +138,3 @@ } | ||
route.setParent(this) | ||
this.addRouteNode(route) | ||
this.addRouteNode(route, sort) | ||
} else { | ||
@@ -142,3 +152,5 @@ if (!route.name || !route.path) { | ||
cb, | ||
this | ||
this, | ||
false, | ||
sort | ||
) | ||
@@ -155,3 +167,3 @@ const fullName = routeNode | ||
} | ||
this.addRouteNode(routeNode) | ||
this.addRouteNode(routeNode, sort) | ||
} | ||
@@ -175,2 +187,13 @@ | ||
public sortChildren() { | ||
if (this.children.length) { | ||
sortChildren(this.children) | ||
} | ||
} | ||
public sortDescendants() { | ||
this.sortChildren() | ||
this.children.forEach(child => child.sortDescendants()) | ||
} | ||
public buildPath( | ||
@@ -239,3 +262,3 @@ routeName: string, | ||
private addRouteNode(route: RouteNode, cb?: () => void): this { | ||
private addRouteNode(route: RouteNode, sort: boolean = true): this { | ||
const names = route.name.split('.') | ||
@@ -265,7 +288,6 @@ | ||
this.children.push(route) | ||
// Push greedy spats to the bottom of the pile | ||
const originalChildren = this.children.slice(0) | ||
this.children.sort(sortChildren(originalChildren)) | ||
if (sort) { | ||
this.sortChildren() | ||
} | ||
} else { | ||
@@ -272,0 +294,0 @@ // Locate parent node |
import RouteNode from './RouteNode' | ||
export default (originalChildren: RouteNode[]) => ( | ||
export default function sortChildren(children: RouteNode[]) { | ||
const originalChildren = children.slice(0) | ||
return children.sort(sortPredicate(originalChildren)) | ||
} | ||
const sortPredicate = (originalChildren: RouteNode[]) => ( | ||
left: RouteNode, | ||
@@ -5,0 +11,0 @@ right: RouteNode |
{ | ||
"name": "route-node", | ||
"version": "3.3.0", | ||
"version": "3.4.0", | ||
"description": "A package to create a tree of named routes", | ||
@@ -5,0 +5,0 @@ "main": "dist/cjs/route-node.js", |
@@ -73,3 +73,6 @@ [![npm version](https://badge.fury.io/js/route-node.svg)](http://badge.fury.io/js/route-node) | ||
__Performance__ | ||
Node children need to be sorted for matching purposes. By default this operation happens after having added all routes. | ||
__matchPath(path: string, options?: MatchOptions): RouteNodeState | null__ | ||
@@ -76,0 +79,0 @@ |
@@ -47,14 +47,16 @@ import { Path } from 'path-parser'; | ||
parent?: RouteNode; | ||
constructor(name?: string, path?: string, childRoutes?: Route[], cb?: Callback, parent?: RouteNode); | ||
constructor(name?: string, path?: string, childRoutes?: Route[], cb?: Callback, parent?: RouteNode, finalSort?: boolean, sort?: boolean); | ||
getParentSegments(segments?: RouteNode[]): RouteNode[]; | ||
setParent(parent: any): void; | ||
setPath(path?: string): void; | ||
add(route: Route | Route[], cb?: Callback): this; | ||
add(route: Route | Route[], cb?: Callback, sort?: boolean): this; | ||
addNode(name: string, path: string): this; | ||
getPath(routeName: string): string; | ||
getNonAbsoluteChildren(): RouteNode[]; | ||
sortChildren(): void; | ||
sortDescendants(): void; | ||
buildPath(routeName: string, params?: object, options?: BuildOptions): string; | ||
buildState(name: string, params?: object): RouteNodeState | null; | ||
matchPath(path: string, options?: MatchOptions): RouteNodeState | null; | ||
private addRouteNode(route, cb?); | ||
private addRouteNode(route, sort?); | ||
private checkParents(); | ||
@@ -61,0 +63,0 @@ private hasParentsParams(); |
import RouteNode from './RouteNode'; | ||
declare const _default: (originalChildren: RouteNode[]) => (left: RouteNode, right: RouteNode) => number; | ||
export default _default; | ||
export default function sortChildren(children: RouteNode[]): RouteNode[]; |
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
239831
2604
97