Skip to content

Instantly share code, notes, and snippets.

@pyldin601
Created October 12, 2017 20:45
Show Gist options
  • Save pyldin601/989472b35cce972caa3326deafc6fd14 to your computer and use it in GitHub Desktop.
Save pyldin601/989472b35cce972caa3326deafc6fd14 to your computer and use it in GitHub Desktop.
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