-
Notifications
You must be signed in to change notification settings - Fork 1
/
plan.js
49 lines (44 loc) · 1.08 KB
/
plan.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
const R = require('ramda');
const Immutable = require('immutable');
function plan(impls, target, visited) {
const targetImpls = impls
.filter(x => x.get('interface') === target)
.filter(x => !visited.includes(x))
.toIndexedSeq()
.sortBy(x => x.get('dependencies').size)
.reverse();
const plans = targetImpls
.flatMap(x => {
const deps = x.get('dependencies');
const depImpls = deps.map(dep => {
return plan(impls, dep, visited.add(x));
});
if (!depImpls.every(Boolean)) {
return [];
}
return [x.set('dependencies', depImpls)];
});
return plans.first();
}
function toJS(x) {
return (x && x.toJS) ? x.toJS() : x;
}
module.exports = {
plan: R.curry((implementations, target) => {
implementations = (new Immutable.Seq.Set(implementations))
.map(x => {
const pick = [
'id',
'interface',
'dependencies'
];
const defaults = {
dependencies: []
};
return (new Immutable.Map())
.merge(defaults, R.pick(pick, x));
});
const visited = new Immutable.Set();
return toJS(plan(implementations, target, visited));
})
};