blob: c13d143862dddef12ab7c5677c79aaeb2ae0a657 (
plain)
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
|
'use strict'
class FiggyPudding {
constructor (specs, opts, providers) {
this.specs = specs || {}
this.opts = opts || (() => false)
this.providers = providers
this.isFiggyPudding = true
}
get (key) {
return pudGet(this, key, true)
}
}
function pudGet (pud, key, validate) {
let spec = pud.specs[key]
if (typeof spec === 'string') {
key = spec
spec = pud.specs[key]
}
if (validate && !spec && (!pud.opts.other || !pud.opts.other(key))) {
throw new Error(`invalid config key requested: ${key}`)
} else {
if (!spec) { spec = {} }
let ret
for (let p of pud.providers) {
if (p.isFiggyPudding) {
ret = pudGet(p, key, false)
} else if (typeof p.get === 'function') {
ret = p.get(key)
} else {
ret = p[key]
}
if (ret !== undefined) {
break
}
}
if (ret === undefined && spec.default !== undefined) {
if (typeof spec.default === 'function') {
return spec.default()
} else {
return spec.default
}
} else {
return ret
}
}
}
module.exports = figgyPudding
function figgyPudding (specs, opts) {
function factory () {
return new FiggyPudding(
specs,
opts,
[].slice.call(arguments).filter(x => x != null && typeof x === 'object')
)
}
return factory
}
|