-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
103 lines (85 loc) · 2.98 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
const defaults = require('bindings')('defaults.node')
const VALID_TYPES = [
'string',
'float',
'integer',
'double',
'boolean',
'array',
'url',
'dictionary',
]
function getAllDefaults(domain) {
if (domain && typeof domain !== 'string') {
throw new TypeError('domain must be a valid string')
}
return defaults.getAllDefaults.call(this, domain)
}
function getUserDefault(type, key, domain) {
if (!VALID_TYPES.includes(type)) {
throw new TypeError(`${type} must be one of ${VALID_TYPES.join(', ')}`)
} else if (domain && typeof domain !== 'string') {
throw new TypeError('domain must be a valid string')
}
return defaults.getUserDefault.call(this, type, key, domain)
}
function setUserDefault(type, key, value, domain) {
if (!VALID_TYPES.includes(type)) {
throw new TypeError(`${type} must be one of ${VALID_TYPES.join(', ')}`)
} else if (domain && typeof domain !== 'string') {
throw new TypeError('domain must be a valid string')
}
const isFloatOrDouble = (n) => !isNaN(parseFloat(n))
const isObject = (o) => Object.prototype.toString.call(o) === '[object Object]'
if (type === 'string' && typeof value !== 'string') {
throw new TypeError('value must be a valid string')
} else if (type === 'double' && !isFloatOrDouble(value)) {
throw new TypeError('value must be a valid double')
} else if (type === 'float' && !isFloatOrDouble(value)) {
throw new TypeError('value must be a valid float')
} else if (type === 'boolean' && typeof value !== 'boolean') {
throw new TypeError('value must be a valid boolean')
} else if (type === 'integer' && !Number.isInteger(value)) {
throw new TypeError('value must be a valid integer')
} else if (type === 'array' && !Array.isArray(value)) {
throw new TypeError('value must be a valid array')
} else if (type == 'dictionary' && !isObject(value)) {
throw new TypeError('value must be a valid dictionary')
} else if (type === 'url' && typeof value !== 'string') {
throw new TypeError('value must be a valid url')
}
return defaults.setUserDefault.call(this, type, key, value, domain)
}
function isKeyManaged(key, domain) {
if (domain && typeof domain !== 'string') {
throw new TypeError('domain must be a valid string')
}
return defaults.isKeyManaged.call(this, key, domain)
}
function removeUserDefault(key, domain) {
if (domain && typeof domain !== 'string') {
throw new TypeError('domain must be a valid string')
}
return defaults.removeUserDefault.call(this, key, domain)
}
function addDomain(name) {
if (typeof name !== 'string') {
throw new TypeError('domain name must be a valid string')
}
return defaults.addDomain.call(this, name)
}
function removeDomain(name) {
if (typeof name !== 'string') {
throw new TypeError('domain name must be a valid string')
}
return defaults.removeDomain.call(this, name)
}
module.exports = {
addDomain,
getAllDefaults,
getUserDefault,
isKeyManaged,
setUserDefault,
removeDomain,
removeUserDefault,
}