-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
helpers.js
65 lines (55 loc) · 1.78 KB
/
helpers.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
'use strict';
// eslint-disable-next-line es/no-object-hasown -- safe
const has = Object.hasOwn || Function.call.bind({}.hasOwnProperty);
const VERSION_PATTERN = /(\d+)(?:\.(\d+))?(?:\.(\d+))?/;
class SemVer {
constructor(input) {
const match = VERSION_PATTERN.exec(input);
if (!match) throw new TypeError(`Invalid version: ${ input }`);
const [, $major, $minor, $patch] = match;
this.major = +$major;
this.minor = $minor ? +$minor : 0;
this.patch = $patch ? +$patch : 0;
}
toString() {
return `${ this.major }.${ this.minor }.${ this.patch }`;
}
}
function semver(input) {
return input instanceof SemVer ? input : new SemVer(input);
}
function compare($a, operator, $b) {
const a = semver($a);
const b = semver($b);
for (const component of ['major', 'minor', 'patch']) {
if (a[component] < b[component]) return operator === '<' || operator === '<=' || operator === '!=';
if (a[component] > b[component]) return operator === '>' || operator === '>=' || operator === '!=';
} return operator === '==' || operator === '<=' || operator === '>=';
}
function filterOutStabilizedProposals(modules) {
const modulesSet = new Set(modules);
for (const $module of modulesSet) {
if ($module.startsWith('esnext.') && modulesSet.has($module.replace(/^esnext\./, 'es.'))) {
modulesSet.delete($module);
}
}
return [...modulesSet];
}
function intersection(list, order) {
const set = list instanceof Set ? list : new Set(list);
return order.filter(name => set.has(name));
}
function sortObjectByKey(object, fn) {
return Object.keys(object).sort(fn).reduce((memo, key) => {
memo[key] = object[key];
return memo;
}, {});
}
module.exports = {
compare,
filterOutStabilizedProposals,
has,
intersection,
semver,
sortObjectByKey,
};