find-my-way
Advanced tools
Comparing version 1.0.1 to 1.1.0
@@ -238,2 +238,6 @@ 'use strict' | ||
Router.prototype.prettyPrint = function () { | ||
return this.tree.prettyPrint('', true) | ||
} | ||
module.exports = Router | ||
@@ -240,0 +244,0 @@ |
36
node.js
@@ -67,2 +67,38 @@ 'use strict' | ||
Node.prototype.prettyPrint = function (prefix, tail) { | ||
var paramName = '' | ||
var map = this.map || {} | ||
var methods = Object.keys(map).filter(method => map[method].handler) | ||
if (this.prefix === ':') { | ||
methods.forEach((method, index) => { | ||
var params = this.map[method].params | ||
var param = params[params.length - 1] | ||
if (methods.length > 1) { | ||
if (index === 0) { | ||
paramName += param + ` (${method})\n` | ||
return | ||
} | ||
paramName += ' ' + prefix + ':' + param + ` (${method})` | ||
paramName += (index === methods.length - 1 ? '' : '\n') | ||
} else { | ||
paramName = params[params.length - 1] + ` (${method})` | ||
} | ||
}) | ||
} else if (methods.length) { | ||
paramName = ` (${methods.join('|')})` | ||
} | ||
var tree = `${prefix}${tail ? '└── ' : '├── '}${this.prefix}${paramName}\n` | ||
prefix = `${prefix}${tail ? ' ' : '│ '}` | ||
for (var i = 0; i < this.numberOfChildren - 1; i++) { | ||
tree += this.children[i].prettyPrint(prefix, false) | ||
} | ||
if (this.numberOfChildren > 0) { | ||
tree += this.children[this.numberOfChildren - 1].prettyPrint(prefix, true) | ||
} | ||
return tree | ||
} | ||
module.exports = Node |
{ | ||
"name": "find-my-way", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "Crazy fast http radix based router", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -89,2 +89,17 @@ # find-my-way | ||
<a name="pretty-print"></a> | ||
#### prettyPrint() | ||
Prints the representation of the internal radix tree, useful for debugging. | ||
```js | ||
findMyWay.on('GET', '/test', () => {}) | ||
findMyWay.on('GET', '/test/hello', () => {}) | ||
findMyWay.on('GET', '/hello/world', () => {}) | ||
console.log(findMyWay.prettyPrint()) | ||
// └── / | ||
// ├── test (GET) | ||
// │ └── /hello (GET) | ||
// └── hello/world (GET) | ||
``` | ||
<a name="acknowledgements"></a> | ||
@@ -91,0 +106,0 @@ ## Acknowledgements |
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
32954
15
891
117