-
Notifications
You must be signed in to change notification settings - Fork 15
/
index.js
52 lines (41 loc) · 1.24 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
'use strict'
const { fromCallback } = require('catering')
const after = require('after')
const Finder = require('./lib/finder')
const DEFAULTS = { browsers: require('./lib/browsers') }
const kPromise = Symbol('promise')
module.exports = function detect (names, opts, done) {
if (typeof names === 'string') {
names = [names]
} else if (!Array.isArray(names)) {
done = opts
opts = names
names = null
}
if (typeof opts === 'function') {
done = opts
opts = Object.assign({}, DEFAULTS)
} else {
opts = Object.assign({}, DEFAULTS, opts)
}
done = fromCallback(done, kPromise)
if (!names || !names.length) {
names = Object.keys(opts.browsers)
}
const result = []
const finish = (err) => err ? done(err) : done(null, result, totalMethods)
const next = after(names.length, finish)
// For debugging: count the number
// of ways we found the browsers.
let totalMethods = 0
names.forEach(function (name) {
const browser = opts.browsers[name]
if (!browser) throw new Error('No such browser is defined: ' + name)
new Finder(name, browser).run((res, methods) => {
for (const b of res.values()) result.push(b)
totalMethods += methods
next()
})
})
return done[kPromise]
}