[79a0317] | 1 | 'use strict';
|
---|
| 2 | // eslint-disable-next-line es/no-object-hasown -- safe
|
---|
| 3 | const has = Object.hasOwn || Function.call.bind({}.hasOwnProperty);
|
---|
| 4 |
|
---|
| 5 | const VERSION_PATTERN = /(\d+)(?:\.(\d+))?(?:\.(\d+))?/;
|
---|
| 6 |
|
---|
| 7 | class SemVer {
|
---|
| 8 | constructor(input) {
|
---|
| 9 | const match = VERSION_PATTERN.exec(input);
|
---|
| 10 | if (!match) throw new TypeError(`Invalid version: ${ input }`);
|
---|
| 11 | const [, $major, $minor, $patch] = match;
|
---|
| 12 | this.major = +$major;
|
---|
| 13 | this.minor = $minor ? +$minor : 0;
|
---|
| 14 | this.patch = $patch ? +$patch : 0;
|
---|
| 15 | }
|
---|
| 16 | toString() {
|
---|
| 17 | return `${ this.major }.${ this.minor }.${ this.patch }`;
|
---|
| 18 | }
|
---|
| 19 | }
|
---|
| 20 |
|
---|
| 21 | function semver(input) {
|
---|
| 22 | return input instanceof SemVer ? input : new SemVer(input);
|
---|
| 23 | }
|
---|
| 24 |
|
---|
| 25 | function compare($a, operator, $b) {
|
---|
| 26 | const a = semver($a);
|
---|
| 27 | const b = semver($b);
|
---|
| 28 | for (const component of ['major', 'minor', 'patch']) {
|
---|
| 29 | if (a[component] < b[component]) return operator === '<' || operator === '<=' || operator === '!=';
|
---|
| 30 | if (a[component] > b[component]) return operator === '>' || operator === '>=' || operator === '!=';
|
---|
| 31 | } return operator === '==' || operator === '<=' || operator === '>=';
|
---|
| 32 | }
|
---|
| 33 |
|
---|
| 34 | function filterOutStabilizedProposals(modules) {
|
---|
| 35 | const modulesSet = new Set(modules);
|
---|
| 36 |
|
---|
| 37 | for (const $module of modulesSet) {
|
---|
| 38 | if ($module.startsWith('esnext.') && modulesSet.has($module.replace(/^esnext\./, 'es.'))) {
|
---|
| 39 | modulesSet.delete($module);
|
---|
| 40 | }
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | return [...modulesSet];
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | function intersection(list, order) {
|
---|
| 47 | const set = list instanceof Set ? list : new Set(list);
|
---|
| 48 | return order.filter(name => set.has(name));
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | function sortObjectByKey(object, fn) {
|
---|
| 52 | return Object.keys(object).sort(fn).reduce((memo, key) => {
|
---|
| 53 | memo[key] = object[key];
|
---|
| 54 | return memo;
|
---|
| 55 | }, {});
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | module.exports = {
|
---|
| 59 | compare,
|
---|
| 60 | filterOutStabilizedProposals,
|
---|
| 61 | has,
|
---|
| 62 | intersection,
|
---|
| 63 | semver,
|
---|
| 64 | sortObjectByKey,
|
---|
| 65 | };
|
---|