Skip to content

Commit

Permalink
simplify work with semver
Browse files Browse the repository at this point in the history
  • Loading branch information
zloirock committed Dec 2, 2019
1 parent 3b9dd46 commit ec33d92
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
'use strict';
const { coerce, lte } = require('semver');
const { normalizeModulesList } = require('./helpers');
const { compare, normalizeModulesList, semver } = require('./helpers');
const modulesByVersions = require('./modules-by-versions');

module.exports = function (raw) {
const corejs = coerce(String(raw));
const corejs = semver(raw);
if (corejs.major !== 3) {
throw RangeError('This version of `core-js-compat` works only with `core-js@3`.');
}
const result = [];
for (const version of Object.keys(modulesByVersions)) {
if (lte(coerce(version), corejs)) {
if (compare(version, '<=', corejs)) {
result.push(...modulesByVersions[version]);
}
}
Expand Down
9 changes: 8 additions & 1 deletion packages/core-js-compat/helpers.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
'use strict';
const data = require('./data');
const { coerce: semver, cmp } = require('semver');
const data = require('./src/data');

const modules = Object.keys(data);

function compare(a, operator, b) {
return cmp(semver(a), operator, semver(b));
}

function normalizeModulesList(list, order) {
const set = new Set(list);
return (Array.isArray(order) ? order : modules).filter(name => set.has(name));
}

module.exports = {
compare,
normalizeModulesList,
semver,
};
17 changes: 4 additions & 13 deletions packages/core-js-compat/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';
const { coerce, lt, lte } = require('semver');
const browserslist = require('browserslist');
const { normalizeModulesList } = require('./helpers');
const { compare, normalizeModulesList } = require('./helpers');
const data = require('./data');
const getModulesListForTargetVersion = require('./get-modules-list-for-target-version');
const has = Function.call.bind({}.hasOwnProperty);
Expand All @@ -28,27 +27,19 @@ const validTargets = new Set([
'samsung',
]);

function coercedLte(a, b) {
return lte(coerce(a), coerce(b));
}

function coercedLt(a, b) {
return lt(coerce(a), coerce(b));
}

function normalizeBrowsersList(list) {
return list.map(it => {
let [engine, version] = it.split(' ');
if (mapping.has(engine)) engine = mapping.get(engine);
else if (engine === 'android' && !coercedLte(version, '4.4.4')) engine = 'chrome';
else if (engine === 'android' && compare(version, '>', '4.4.4')) engine = 'chrome';
return [engine, version];
}).filter(([engine]) => validTargets.has(engine));
}

function reduceByMinVersion(list) {
const targets = new Map();
for (const [engine, version] of list) {
if (!targets.has(engine) || coercedLte(version, targets.get(engine))) {
if (!targets.has(engine) || compare(version, '<=', targets.get(engine))) {
targets.set(engine, version);
}
}
Expand All @@ -63,7 +54,7 @@ function checkModule(name, targets) {
targets: {},
};
for (const [engine, version] of targets) {
if (!has(requirements, engine) || coercedLt(version, requirements[engine])) {
if (!has(requirements, engine) || compare(version, '<', requirements[engine])) {
result.required = true;
result.targets[engine] = version;
}
Expand Down
6 changes: 3 additions & 3 deletions packages/core-js-compat/src/build-data.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';
const { writeFileSync } = require('fs');
const { resolve } = require('path');
const { coerce, lte } = require('semver');
const { compare, semver } = require('../helpers');
const data = require('./data');
const {
ChromeToNode,
Expand All @@ -18,9 +18,9 @@ for (const key in data) {

const map = function (mapping, version, targetKey) {
if (module[targetKey]) return;
const coercedVersion = coerce(String(version));
const source = semver(version);
for (const [from, to] of mapping) {
if (lte(coercedVersion, coerce(String(from)))) {
if (compare(source, '<=', from)) {
module[targetKey] = to;
return;
}
Expand Down

0 comments on commit ec33d92

Please sign in to comment.