Skip to content

Instantly share code, notes, and snippets.

@vshjxyz
Last active November 13, 2015 13:53
Show Gist options
  • Save vshjxyz/5636130028ca4ebdb500 to your computer and use it in GitHub Desktop.
Save vshjxyz/5636130028ca4ebdb500 to your computer and use it in GitHub Desktop.
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