helpers.js 1.8 KB

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