helpers.js 965 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. 'use strict';
  2. const cmp = require('semver/functions/cmp');
  3. const semver = require('semver/functions/coerce');
  4. const has = Function.call.bind({}.hasOwnProperty);
  5. function compare(a, operator, b) {
  6. return cmp(semver(a), operator, semver(b));
  7. }
  8. function filterOutStabilizedProposals(modules) {
  9. const modulesSet = new Set(modules);
  10. for (const $module of modulesSet) {
  11. if ($module.startsWith('esnext.') && modulesSet.has($module.replace(/^esnext\./, 'es.'))) {
  12. modulesSet.delete($module);
  13. }
  14. }
  15. return [...modulesSet];
  16. }
  17. function intersection(list, order) {
  18. const set = list instanceof Set ? list : new Set(list);
  19. return order.filter(name => set.has(name));
  20. }
  21. function sortObjectByKey(object, fn) {
  22. return Object.keys(object).sort(fn).reduce((memo, key) => {
  23. memo[key] = object[key];
  24. return memo;
  25. }, {});
  26. }
  27. module.exports = {
  28. compare,
  29. filterOutStabilizedProposals,
  30. has,
  31. intersection,
  32. semver,
  33. sortObjectByKey,
  34. };