Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
const test = require('tape');
function resolver(deps) {
function directDeps(className) {
return deps[className] || [];
}
function transitiveDeps(className) {
return directDeps(className).reduce((set, className) => {
[className, ...transitiveDeps(className)].forEach((v) => set.add(v));
return set;
}, new Set());
}
function resolve(className) {
return [...transitiveDeps(className)].sort();
}
function resolveAll() {
return Object.keys(deps).reduce((transitiveDeps, className) => {
transitiveDeps[className] = resolve(className);
return transitiveDeps;
}, {});
}
return { resolveAll };
}
test('resolves dependencies for any given class', (assert) => {
deps = resolver({
A: ['B', 'C'],
B: ['C', 'E'],
C: ['G'],
D: ['A', 'F'],
E: ['F'],
F: ['H']
});
assert.deepEqual(deps.resolveAll(), {
A: ['B', 'C', 'E', 'F', 'G', 'H'],
B: ['C', 'E', 'F', 'G', 'H'],
C: ['G'],
D: ['A', 'B', 'C', 'E', 'F', 'G', 'H'],
E: ['F', 'H'],
F: ['H']
});
assert.end();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment