-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
helpers.js
61 lines (52 loc) · 1.79 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
'use strict';
// eslint-disable-next-line es/no-object-hasown -- safe
const has = Object.hasOwn || Function.call.bind({}.hasOwnProperty);
function semver(input) {
if (input instanceof semver) return input;
// eslint-disable-next-line new-cap -- ok
if (!(this instanceof semver)) return new semver(input);
const match = /(\d+)(?:\.(\d+))?(?:\.(\d+))?/.exec(input);
if (!match) throw TypeError(`Invalid version: ${ input }`);
const [, $major, $minor, $patch] = match;
this.major = +$major;
this.minor = $minor ? +$minor : 0;
this.patch = $patch ? +$patch : 0;
}
semver.prototype.toString = function () {
return `${ this.major }.${ this.minor }.${ this.patch }`;
};
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,
};