-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathcompute-currentlevel.js
48 lines (45 loc) · 1.66 KB
/
compute-currentlevel.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
/**
* Module that exports a function that takes a spec object and a list of specs
* that contains it, and that returns an object with a "currentSpecification"
* property set to the "shortname" of the spec object that should be seen as
* the current level for the set of specs with the same series' shortname in
* the list.
*
* Each spec in the list must have "shortname", "series" and "seriesVersion"
* (if needed) properties.
*
* By default, the current level is defined as the last level that is not a
* delta/fork spec, unless a level is explicitly flagged with a "forceCurrent"
* property in the list of specs.
*/
/**
* Exports main function that takes a spec object and a list of specs (which
* must contain the spec object itself) and returns an object with a
* "currentSpecification" property. Function always sets the property (value is
* the name of the spec itself when it is the current level)
*/
export default function (spec, list) {
list = list || [];
if (!spec) {
throw "Invalid spec object passed as parameter";
}
const current = list.reduce((candidate, curr) => {
if (curr.series.shortname === candidate.series.shortname &&
!candidate.forceCurrent &&
curr.seriesComposition !== "fork" &&
curr.seriesComposition !== "delta" &&
(curr.forceCurrent ||
candidate.seriesComposition === "delta" ||
candidate.seriesComposition === "fork" ||
(curr.seriesVersion || "0") > (candidate.seriesVersion || "0"))) {
return curr;
}
else {
return candidate;
}
}, spec);
return {
currentSpecification: current.shortname,
forceCurrent: current.forceCurrent
};
};