-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
65 lines (64 loc) · 1.66 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
function hasValue(value, yes, no) {
if (typeof value === 'undefined') {
if (typeof no === 'function') {
no()
}
else {
return typeof no === 'undefined'? false: no
}
}
else {
if (typeof yes === 'function') {
yes()
}
else {
return typeof yes === 'undefined'? true: yes
}
}
}
var spare = function spare () {
var throwError = function () {
throw new Error('node_modules/spare/index.js: You should use `spare(data, defaultValue)` or `spare(data, attr, defaultValue)`\r\n The use of the error: spare(' + Array.from(arguments).join(', ') + ')')
}
var data = arguments[0]
var attr
var defaultValue
switch(arguments.length) {
case 0:
throwError()
break
case 1:
throwError()
break
case 2:
attr = undefined
defaultValue = arguments[1]
break
case 3:
attr = arguments[1]
defaultValue = arguments[2]
break
default:
throwError()
}
var attrArray
var output
if (attr) {
attrArray = attr.split('.')
var findUndefinedObj = attrArray.some(function (key, index) {
if (!hasValue(data[key])) {
return true
}
data = typeof data[key] === 'undefined'? {}: data[key]
})
if (findUndefinedObj) {
output = defaultValue
}
}
if (hasValue(output)) {
return output
}
return hasValue(data, data, defaultValue)
}
spare.settings = require('./settings')
module.exports = spare