-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
65 lines (56 loc) · 1.89 KB
/
index.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
57
58
59
60
61
62
63
64
65
'use strict'
const ua = require('polyfill-ua')
module.exports = Feature
// filter a list of transforms against a list of agents
Feature.filter = function (transforms, agent) {
transforms = transforms.filter(function (transform) {
return !transform.filter // if .filter does not exist, it is assumed to be true
|| transform.filter(agent)
})
// remove supersets
label_top:
for (let i = 0; i < transforms.length; i++) {
let transform = transforms[i]
let supersets = transform.supersets
if (!supersets || !supersets.length) continue
for (let j = 0; j < supersets.length; j++) {
let superset = supersets[j]
for (let k = 0; k < transforms.length; k++) {
if (transforms[k].name !== superset) continue
transforms.splice(i--, 1)
continue label_top
}
}
}
return transforms
}
function Feature(options) {
if (!(this instanceof Feature)) return new Feature(options)
// the name of this feature
this.name = options.name
// a shortname for this feature
this.shortName = options.shortName || options.name
// the module associated with this feature
this.moduleName = options.moduleName || options.module
// optional URL to download this file
if (options.url) {
this.url = options.url[0] === '/'
? 'https://raw.githubusercontent.com' + options.url
: options.url
}
// github repo
this.repository = options.repository || options.repo
// supersets, i.e. packages that contain this feature
this.supersets = options.supersets || []
if (typeof options.filter === 'function') {
this.filter = options.filter
} else if (typeof options.browsers === 'object') {
this.filter = ua.compile(options.browsers)
} else if (typeof options.caniuse === 'string') {
this.filter = ua.caniuse(options.caniuse)
}
}
// by default, a feature is always enabled
Feature.prototype.filter = function () {
return true
}