forked from TPC-Internet/l10n-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.js
50 lines (45 loc) · 1.21 KB
/
config.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
import objectPath from 'object-path'
export class Config {
constructor (rc, path = null) {
this.rc = rc
if (path == null) {
this.prefix = ''
} else if (Array.isArray(path)) {
this.prefix = path.join('.')
} else {
this.prefix = path
}
}
getSubConfig (path) {
const rc = objectPath.get(this.rc, path, null)
if (rc == null) {
return null
}
return new Config(rc, this.appendPrefix(path))
}
appendPrefix (path) {
let prefix
if (Array.isArray(path)) {
prefix = path.join('.')
} else {
prefix = path
}
if (this.prefix) {
prefix = this.prefix + '.' + prefix
}
return prefix
}
get (path, defaultValue = undefined) {
const value = objectPath.get(this.rc, path, defaultValue)
if (value === undefined) {
throw new Error(`config '${this.appendPrefix(path)}' is required`)
}
return value
}
getLength () {
if (!Array.isArray(this.rc)) {
throw new Error(`config '${this.prefix}' is not array`)
}
return this.rc.length
}
}