Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pretty print handler names #169

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 27 additions & 18 deletions lib/pretty-print.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,34 @@
function prettyPrintFlattenedNode (flattenedNode, prefix, tail) {
var paramName = ''
var methods = new Set(flattenedNode.nodes.map(node => node.method))
const handlers = flattenedNode.nodes.map(node => ({ method: node.method, ...node.handler }))

if (flattenedNode.prefix.includes(':')) {
flattenedNode.nodes.forEach((node, index) => {
var params = node.handler.params
var param = params[params.length - 1]
if (methods.size > 1) {
if (index === 0) {
paramName += param + ` (${node.method})\n`
return
}
paramName += prefix + ' :' + param + ` (${node.method})`
paramName += (index === methods.size - 1 ? '' : '\n')
} else {
paramName = params[params.length - 1] + ` (${node.method})`
handlers.forEach((handler, index) => {
let suffix = `(${handler.method}`
if (handler.handler && handler.handler.name.length > 0) {
suffix += ' ' + handler.handler.name
}
suffix += ')'

let name = ''
if (flattenedNode.prefix.includes(':')) {
var params = handler.params
name = params[params.length - 1]
if (index > 0) {
name = ':' + name
}
})
} else if (methods.size) {
paramName = ` (${Array.from(methods).join('|')})`
}
} else if (index > 0) {
name = flattenedNode.prefix
}

if (index === 0) {
paramName += name + ` ${suffix}`
return
} else {
paramName += '\n'
}

paramName += prefix + ' ' + name + ` ${suffix}`
})

var tree = `${prefix}${tail ? '└── ' : '├── '}${flattenedNode.prefix}${paramName}\n`

Expand Down
23 changes: 23 additions & 0 deletions test/pretty-print.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,26 @@ test('pretty print - parametric routes with same parent and followed by a static
t.is(typeof tree, 'string')
t.equal(tree, expected)
})

test('pretty print - parametric routes with named handlers', t => {
t.plan(2)

const findMyWay = FindMyWay()
findMyWay.on('GET', '/test', function testRoot () {})
findMyWay.on('GET', '/test/hello/:id', function getHello () {})
findMyWay.on('POST', '/test/hello/:id', function postHello () {})
findMyWay.on('GET', '/test/helloworld', function helloWorld () {})

const tree = findMyWay.prettyPrint()

const expected = `└── /test (GET testRoot)
└── /hello
├── /
│ └── :id (GET getHello)
│ :id (POST postHello)
└── world (GET helloWorld)
`

t.is(typeof tree, 'string')
t.equal(tree, expected)
})