forked from zloirock/core-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compat.js
56 lines (45 loc) · 1.43 KB
/
compat.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
'use strict';
const { compare, has, intersection } = require('./helpers');
const data = require('./data');
const getModulesListForTargetVersion = require('./get-modules-list-for-target-version');
const modules = require('./modules');
const targetsParser = require('./targets-parser');
function checkModule(name, targets) {
if (!has(data, name)) throw new TypeError(`Incorrect module: ${ name }`);
const requirements = data[name];
const result = {
required: false,
targets: {},
};
for (const [engine, version] of targets) {
if (!has(requirements, engine) || compare(version, '<', requirements[engine])) {
result.required = true;
result.targets[engine] = version;
}
}
return result;
}
module.exports = function ({ targets, filter, version }) {
const parsedTargets = targetsParser(targets);
const result = {
list: [],
targets: {},
};
let $modules = Array.isArray(filter) ? filter : modules;
if (filter instanceof RegExp) {
$modules = $modules.filter(it => filter.test(it));
} else if (typeof filter == 'string') {
$modules = $modules.filter(it => it.startsWith(filter));
}
if (version) {
$modules = intersection($modules, getModulesListForTargetVersion(version));
}
for (const key of $modules) {
const check = checkModule(key, parsedTargets);
if (check.required) {
result.list.push(key);
result.targets[key] = check.targets;
}
}
return result;
};