route-node
Advanced tools
Comparing version 1.0.2 to 1.1.0
@@ -371,2 +371,3 @@ define('RouteNode', function () { 'use strict'; | ||
var noop = function noop() {}; | ||
var isSerialisable = function isSerialisable(val) { | ||
@@ -412,2 +413,3 @@ return val !== undefined && val !== null && val !== ''; | ||
var childRoutes = arguments.length <= 2 || arguments[2] === undefined ? [] : arguments[2]; | ||
var cb = arguments[3]; | ||
babelHelpers_classCallCheck(this, RouteNode); | ||
@@ -420,3 +422,3 @@ | ||
this.add(childRoutes); | ||
this.add(childRoutes, cb); | ||
@@ -431,2 +433,5 @@ return this; | ||
var cb = arguments.length <= 1 || arguments[1] === undefined ? noop : arguments[1]; | ||
var originalRoute = undefined; | ||
if (route === undefined || route === null) return; | ||
@@ -436,3 +441,3 @@ | ||
route.forEach(function (r) { | ||
return _this.add(r); | ||
return _this.add(r, cb); | ||
}); | ||
@@ -449,2 +454,3 @@ return; | ||
} | ||
originalRoute = route; | ||
route = new RouteNode(route.name, route.path, route.children); | ||
@@ -504,2 +510,4 @@ } | ||
if (originalRoute) cb(originalRoute); | ||
return this; | ||
@@ -506,0 +514,0 @@ } |
@@ -21,2 +21,3 @@ 'use strict'; | ||
var noop = function noop() {}; | ||
var isSerialisable = function isSerialisable(val) { | ||
@@ -62,2 +63,3 @@ return val !== undefined && val !== null && val !== ''; | ||
var childRoutes = arguments.length <= 2 || arguments[2] === undefined ? [] : arguments[2]; | ||
var cb = arguments[3]; | ||
@@ -71,3 +73,3 @@ _classCallCheck(this, RouteNode); | ||
this.add(childRoutes); | ||
this.add(childRoutes, cb); | ||
@@ -82,2 +84,5 @@ return this; | ||
var cb = arguments.length <= 1 || arguments[1] === undefined ? noop : arguments[1]; | ||
var originalRoute = undefined; | ||
if (route === undefined || route === null) return; | ||
@@ -87,3 +92,3 @@ | ||
route.forEach(function (r) { | ||
return _this.add(r); | ||
return _this.add(r, cb); | ||
}); | ||
@@ -100,2 +105,3 @@ return; | ||
} | ||
originalRoute = route; | ||
route = new RouteNode(route.name, route.path, route.children); | ||
@@ -155,2 +161,4 @@ } | ||
if (originalRoute) cb(originalRoute); | ||
return this; | ||
@@ -157,0 +165,0 @@ } |
@@ -375,2 +375,3 @@ (function (global, factory) { | ||
var noop = function noop() {}; | ||
var isSerialisable = function isSerialisable(val) { | ||
@@ -416,2 +417,3 @@ return val !== undefined && val !== null && val !== ''; | ||
var childRoutes = arguments.length <= 2 || arguments[2] === undefined ? [] : arguments[2]; | ||
var cb = arguments[3]; | ||
babelHelpers_classCallCheck(this, RouteNode); | ||
@@ -424,3 +426,3 @@ | ||
this.add(childRoutes); | ||
this.add(childRoutes, cb); | ||
@@ -435,2 +437,5 @@ return this; | ||
var cb = arguments.length <= 1 || arguments[1] === undefined ? noop : arguments[1]; | ||
var originalRoute = undefined; | ||
if (route === undefined || route === null) return; | ||
@@ -440,3 +445,3 @@ | ||
route.forEach(function (r) { | ||
return _this.add(r); | ||
return _this.add(r, cb); | ||
}); | ||
@@ -453,2 +458,3 @@ return; | ||
} | ||
originalRoute = route; | ||
route = new RouteNode(route.name, route.path, route.children); | ||
@@ -508,2 +514,4 @@ } | ||
if (originalRoute) cb(originalRoute); | ||
return this; | ||
@@ -510,0 +518,0 @@ } |
import Path from 'path-parser'; | ||
const noop = () => {}; | ||
const isSerialisable = val => val !== undefined && val !== null && val !== ''; | ||
@@ -36,3 +37,3 @@ | ||
export default class RouteNode { | ||
constructor(name = '', path = '', childRoutes = []) { | ||
constructor(name = '', path = '', childRoutes = [], cb) { | ||
this.name = name; | ||
@@ -43,3 +44,3 @@ this.path = path; | ||
this.add(childRoutes); | ||
this.add(childRoutes, cb); | ||
@@ -49,7 +50,8 @@ return this; | ||
add(route) { | ||
add(route, cb = noop) { | ||
let originalRoute; | ||
if (route === undefined || route === null) return; | ||
if (route instanceof Array) { | ||
route.forEach(r => this.add(r)); | ||
route.forEach(r => this.add(r, cb)); | ||
return; | ||
@@ -65,2 +67,3 @@ } | ||
} | ||
originalRoute = route; | ||
route = new RouteNode(route.name, route.path, route.children); | ||
@@ -116,2 +119,4 @@ } | ||
if (originalRoute) cb(originalRoute); | ||
return this; | ||
@@ -118,0 +123,0 @@ } |
{ | ||
"name": "route-node", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"description": "A package to create a tree of named routes", | ||
@@ -5,0 +5,0 @@ "main": "dist/commonjs/route-node.js", |
@@ -80,2 +80,6 @@ [![npm version](https://badge.fury.io/js/route-node.svg)](http://badge.fury.io/js/route-node) | ||
## Callbacks | ||
When adding routes (with contructor or `.add`), you can pass a callback which will be executed for each route added successfully to the tree. | ||
## Related packages | ||
@@ -82,0 +86,0 @@ |
@@ -39,2 +39,29 @@ 'use strict'; | ||
it('should callback for each route from a POJO', function () { | ||
var routeA = {name: 'home', path: '/home', extra: 'extra'}; | ||
var routeB = {name: 'profile', path: '/profile', extra: 'extra'}; | ||
var routes = [routeA, routeB]; | ||
var node = new RouteNode(); | ||
var i = 0; | ||
node.add(routes, function(route) { | ||
i = i + 1; | ||
if (i === 1) route.should.equal(routeA); | ||
if (i === 2) route.should.equal(routeB); | ||
}); | ||
i.should.not.equal(0); | ||
i = 0; | ||
var node = new RouteNode('', '', routes, function(route) { | ||
i = i + 1; | ||
if (i === 1) route.should.equal(routeA); | ||
if (i === 2) route.should.equal(routeB); | ||
}); | ||
i.should.not.equal(0); | ||
}); | ||
it('should throw an error when trying to instanciate a RouteNode object with plain objects missing `name` or `path` properties', function () { | ||
@@ -41,0 +68,0 @@ (function () { |
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
110829
2106
91