core-js-compat 3.4.3 → 3.4.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,37 +1,73 @@
1
- [`core-js-compat` package](https://github.com/zloirock/core-js/packages/core-js-compat) contains data about the necessity of [`core-js`](https://github.com/zloirock/core-js) modules and API for getting a list of required core-js modules by browserslist query.
1
+ [`core-js-compat` package](https://github.com/zloirock/core-js/tree/master/packages/core-js-compat) contains data about the necessity of [`core-js`](https://github.com/zloirock/core-js) modules and API for getting a list of required core-js modules by browserslist query.
2
2
 
3
3
  ```js
4
4
  const {
5
- list, // array of required modules
6
- targets, // object with targets for each module
5
+ list, // array of required modules
6
+ targets, // object with targets for each module
7
7
  } = require('core-js-compat')({
8
- targets: '> 2.5%', // browserslist query
9
- filter: 'es.', // optional filter - string-prefix, regexp or list of modules
8
+ targets: '> 2.5%', // browserslist query, by default - all engines
9
+ filter: /^(es|web)\./, // optional filter - string-prefix, regexp or list of modules
10
+ version: '3.4', // used `core-js` version, by default - the latest
10
11
  });
11
12
 
12
13
  console.log(targets);
13
14
  /* =>
14
15
  {
15
- 'es.symbol.description': { ios: '12.0-12.1' },
16
- 'es.array.reverse': { ios: '12.0-12.1' },
17
- 'es.string.replace': { firefox: '63', ios: '12.0-12.1' },
18
- 'es.string.trim': { ios: '12.0-12.1' },
19
- 'es.promise': { firefox: '63' },
20
- 'es.promise.finally': { firefox: '63' },
21
- 'es.array-buffer.slice': { ios: '12.0-12.1' },
22
- 'es.typed-array.int8-array': { ios: '12.0-12.1' },
23
- 'es.typed-array.uint8-array': { ios: '12.0-12.1' },
24
- 'es.typed-array.uint8-clamped-array': { ios: '12.0-12.1' },
25
- 'es.typed-array.int16-array': { ios: '12.0-12.1' },
26
- 'es.typed-array.uint16-array': { ios: '12.0-12.1' },
27
- 'es.typed-array.int32-array': { ios: '12.0-12.1' },
28
- 'es.typed-array.uint32-array': { ios: '12.0-12.1' },
29
- 'es.typed-array.float32-array': { ios: '12.0-12.1' },
30
- 'es.typed-array.float64-array': { ios: '12.0-12.1' },
31
- 'es.typed-array.from': { ios: '12.0-12.1' },
32
- 'es.typed-array.of': { ios: '12.0-12.1' }
16
+ 'es.symbol.match-all': { ios: '12.2-12.4' },
17
+ 'es.array.unscopables.flat': { ios: '12.2-12.4' },
18
+ 'es.array.unscopables.flat-map': { ios: '12.2-12.4' },
19
+ 'es.math.hypot': { chrome: '77' },
20
+ 'es.promise.all-settled': { firefox: '69', ios: '12.2-12.4' },
21
+ 'es.promise.finally': { ios: '12.2-12.4' },
22
+ 'es.string.match-all': { chrome: '77', firefox: '69', ios: '12.2-12.4' },
23
+ 'es.string.replace': { firefox: '69', ios: '12.2-12.4' },
24
+ 'es.typed-array.float32-array': { ios: '12.2-12.4' },
25
+ 'es.typed-array.float64-array': { ios: '12.2-12.4' },
26
+ 'es.typed-array.int8-array': { ios: '12.2-12.4' },
27
+ 'es.typed-array.int16-array': { ios: '12.2-12.4' },
28
+ 'es.typed-array.int32-array': { ios: '12.2-12.4' },
29
+ 'es.typed-array.uint8-array': { ios: '12.2-12.4' },
30
+ 'es.typed-array.uint8-clamped-array': { ios: '12.2-12.4' },
31
+ 'es.typed-array.uint16-array': { ios: '12.2-12.4' },
32
+ 'es.typed-array.uint32-array': { ios: '12.2-12.4' },
33
+ 'es.typed-array.from': { ios: '12.2-12.4' },
34
+ 'es.typed-array.of': { ios: '12.2-12.4' },
35
+ 'web.dom-collections.iterator': { ios: '12.2-12.4' },
36
+ 'web.immediate': { chrome: '77', firefox: '69', ios: '12.2-12.4' },
37
+ 'web.url': { ios: '12.2-12.4' },
38
+ 'web.url.to-json': { ios: '12.2-12.4' },
39
+ 'web.url-search-params': { ios: '12.2-12.4' }
33
40
  }
34
41
  */
35
42
  ```
36
43
 
44
+ Additional API:
45
+
46
+ ```js
47
+ // equals of of the method from the example above
48
+ require('core-js-compat/compat')({ targets, filter, version }); // => { list: Array<ModuleName>, targets: { [ModuleName]: { [EngineName]: EngineVersion } } }
49
+ // or
50
+ require('core-js-compat').compat({ targets, filter, version }); // => { list: Array<ModuleName>, targets: { [ModuleName]: { [EngineName]: EngineVersion } } }
51
+
52
+ // full compat data:
53
+ require('core-js-compat/data'); // => { [ModuleName]: { [EngineName]: EngineVersion } }
54
+ // or
55
+ require('core-js-compat').data; // => { [ModuleName]: { [EngineName]: EngineVersion } }
56
+
57
+ // map of modules by `core-js` entry points:
58
+ require('core-js-compat/entries'); // => { [EntryPoint]: Array<ModuleName> }
59
+ // or
60
+ require('core-js-compat').entries; // => { [EntryPoint]: Array<ModuleName> }
61
+
62
+ // full list of modules:
63
+ require('core-js-compat/modules'); // => Array<ModuleName>
64
+ // or
65
+ require('core-js-compat').modules; // => Array<ModuleName>
66
+
67
+ // the subset of modules which available in the passed `core-js` version:
68
+ require('core-js-compat/get-modules-list-for-target-version')('3.3'); // => Array<ModuleName>
69
+ // or
70
+ require('core-js-compat').getModulesListForTargetVersion('3.3'); // => Array<ModuleName>
71
+ ```
72
+
37
73
  If you want to add new / update data about modules required for target engines, [follow this instruction](https://github.com/zloirock/core-js/blob/master/CONTRIBUTING.md#updating-core-js-compat-data).
package/compat.js ADDED
@@ -0,0 +1,104 @@
1
+ 'use strict';
2
+ const browserslist = require('browserslist');
3
+ const { compare, has, intersection } = require('./helpers');
4
+ const data = require('./data');
5
+ const getModulesListForTargetVersion = require('./get-modules-list-for-target-version');
6
+ const modules = require('./modules');
7
+
8
+ const mapping = new Map([
9
+ ['and_chr', 'chrome'],
10
+ ['and_ff', 'firefox'],
11
+ ['ie_mob', 'ie'],
12
+ ['ios_saf', 'ios'],
13
+ ['op_mob', 'opera_mobile'],
14
+ ]);
15
+
16
+ const validTargets = new Set([
17
+ 'android',
18
+ 'chrome',
19
+ 'edge',
20
+ 'electron',
21
+ 'firefox',
22
+ 'ie',
23
+ 'ios',
24
+ 'node',
25
+ 'opera',
26
+ 'opera_mobile',
27
+ 'phantom',
28
+ 'safari',
29
+ 'samsung',
30
+ ]);
31
+
32
+ function normalizeBrowsersList(list) {
33
+ return list.map(it => {
34
+ let [engine, version] = it.split(' ');
35
+ if (mapping.has(engine)) {
36
+ engine = mapping.get(engine);
37
+ } else if (engine === 'android' && compare(version, '>', '4.4.4')) {
38
+ engine = 'chrome';
39
+ }
40
+ return [engine, version];
41
+ }).filter(([engine]) => validTargets.has(engine));
42
+ }
43
+
44
+ function reduceByMinVersion(list) {
45
+ const targets = new Map();
46
+ for (const [engine, version] of list) {
47
+ if (!targets.has(engine) || compare(version, '<=', targets.get(engine))) {
48
+ targets.set(engine, version);
49
+ }
50
+ }
51
+ return targets;
52
+ }
53
+
54
+ function checkModule(name, targets) {
55
+ if (!has(data, name)) throw new TypeError(`Incorrect module: ${ name }`);
56
+
57
+ const requirements = data[name];
58
+ const result = {
59
+ required: false,
60
+ targets: {},
61
+ };
62
+
63
+ for (const [engine, version] of targets) {
64
+ if (!has(requirements, engine) || compare(version, '<', requirements[engine])) {
65
+ result.required = true;
66
+ result.targets[engine] = version;
67
+ }
68
+ }
69
+
70
+ return result;
71
+ }
72
+
73
+ module.exports = function ({ targets, filter, version }) {
74
+ const list = browserslist(targets);
75
+ const engines = normalizeBrowsersList(list);
76
+ const reducedTargets = reduceByMinVersion(engines);
77
+
78
+ const result = {
79
+ list: [],
80
+ targets: {},
81
+ };
82
+
83
+ let $modules = Array.isArray(filter) ? filter : modules;
84
+
85
+ if (filter instanceof RegExp) {
86
+ $modules = $modules.filter(it => filter.test(it));
87
+ } else if (typeof filter == 'string') {
88
+ $modules = $modules.filter(it => it.startsWith(filter));
89
+ }
90
+
91
+ if (version) {
92
+ $modules = intersection($modules, getModulesListForTargetVersion(version));
93
+ }
94
+
95
+ for (const key of $modules) {
96
+ const check = checkModule(key, reducedTargets);
97
+ if (check.required) {
98
+ result.list.push(key);
99
+ result.targets[key] = check.targets;
100
+ }
101
+ }
102
+
103
+ return result;
104
+ };