Last active
November 13, 2015 13:53
-
-
Save vshjxyz/5636130028ca4ebdb500 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const handler = (request, response) => response('Hello, World!'); | |
const routes = { | |
home: handler, | |
foo: { | |
bar: handler, | |
baz: handler, | |
qux: handler | |
}, | |
ids: { | |
1: handler, | |
2: handler, | |
3: handler | |
}, | |
hello: { | |
world: handler | |
}, | |
one: { | |
two: { | |
three: { | |
four: handler | |
} | |
} | |
} | |
}; | |
function *flatten(obj, path='') { | |
for (let key of Object.keys(obj)) { | |
const value = obj[key]; | |
let flattened = path + '/' + key; | |
if (typeof value === 'function') { | |
yield { | |
[flattened]: value | |
}; | |
} else { | |
yield *flatten(value, flattened); | |
} | |
} | |
}; | |
[...flatten(routes)].map((object) => { | |
console.log(JSON.stringify(Object.keys(object))); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment