Created
October 12, 2017 20:45
-
-
Save pyldin601/989472b35cce972caa3326deafc6fd14 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 deps1 = { | |
mongo: [], | |
tzinfo: ['thread_safe'], | |
uglifier: ['execjs'], | |
execjs: ['thread_safe', 'json'], | |
redis: [], | |
}; | |
const sortDeps = (depsMap) => { | |
const reduce = (deps) => deps.reduce( | |
(acc, dep) => [ | |
...acc, | |
...reduce(depsMap[dep] || []), | |
dep | |
], | |
[] | |
); | |
return reduce(Object.keys(depsMap)) | |
.filter((itm, i, array) => array.indexOf(itm) === i); | |
}; | |
console.log(sortDeps(deps1)); | |
// => ['mongo', 'thread_safe', 'tzinfo', 'json', 'execjs', 'uglifier', 'redis']; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment