-
Notifications
You must be signed in to change notification settings - Fork 2
/
version-selector.js
36 lines (35 loc) · 1.62 KB
/
version-selector.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
module.exports = selectVersions
function selectVersions(versionScheme, versionRange, limit, limitBy, orderedVersionRepoTupleList, consumer, logger) {
if (!orderedVersionRepoTupleList || !orderedVersionRepoTupleList.length) {
logger.debug('No version given.');
return;
}
let usedVersionFragments = new Set();
if (typeof versionRange === "string") {
versionRange = versionScheme.range(versionRange);
}
let idx = 0;
do {
let candidate = orderedVersionRepoTupleList[idx];
if (versionRange.test(candidate.version)) {
const candidateFragment = extractVersionFragment(versionScheme, candidate.version, limitBy);
if (!usedVersionFragments.has(candidateFragment)) {
consumer(candidate);
usedVersionFragments.add(candidateFragment);
} else {
logger.debug('Skipping version ' + candidate.version + ': version fragment already present');
}
} else {
logger.debug('Skipping version ' + candidate.version + ': does not match range "' + versionRange.format() + '"');
}
} while (++idx < orderedVersionRepoTupleList.length && usedVersionFragments.size < limit)
}
function extractVersionFragment(versionScheme, version, fragment) {
let parsedVersion = versionScheme.parse(version);
switch (fragment) {
case 'major': return parsedVersion.major;
case 'minor': return parsedVersion.major + '.' + parsedVersion.minor;
case 'patch': return parsedVersion.major + '.' + parsedVersion.minor + '.' + parsedVersion.patch;
default: return version;
}
}