-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathis-defined-type.js
More file actions
172 lines (126 loc) · 4.5 KB
/
is-defined-type.js
File metadata and controls
172 lines (126 loc) · 4.5 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/**
* @copyright 2017 basbase
* @author basbase
* @licence MIT
*/
;(function () {
"use strict";
var root = this;
var prev_isDefinedType = root.isDefinedType;
/**
* Check if a variable in a nested object is defined and optionally also check its type.
*
* @param {Object} input - Object to look in
* @param {(string|string[])} [path] - Path to key in input to check: 'res.user.name' or: ['res','user','name']
* @param {string|string[]} [type] - If path is defined also match this/these type(s), when 'value' is in type return the value of the last checked node when it would otherwise return true
* @param [defaultValue] - If path is not defined or does not match type, return defaultValue. When defaultValue is not undefined will return value when path is defined and matches type
*/
var isDefinedType = function (input, path, type, defaultValue) {
var current = input;
var nodes = [];
var types = [];
var returnValue = false;
var returnDefault = false;
var typeResult = false;
var node, lowerCaseType, i, j;
if (path) {
if (typeof path === 'string') {
nodes = path.split('.');
} else if (isArray(path)) {
nodes = path;
}
}
if (typeof defaultValue !== 'undefined') {
returnValue = true;
returnDefault = true;
}
if (typeof current === 'undefined') {
return returnDefault ? defaultValue : false;
}
/**
* Check each node in the provided path for undefined
*/
for (i = 0; i < nodes.length; i++) {
node = nodes[i];
if (
typeof current[node] === 'undefined'
|| (i < nodes.length - 1 && current[node] === null)
) {
return returnDefault ? defaultValue : false;
} else {
current = current[node];
}
}
if (isArray(type)) {
types = type;
} else if (typeof type === 'string') {
types = [type];
}
if (types.indexOf('value') !== -1) {
types.splice(types.indexOf('value'), 1);
returnValue = true;
}
if (types.length) {
for (j = 0; j < types.length && !typeResult; j++) {
lowerCaseType = types[j].toLowerCase();
switch (lowerCaseType) {
case 'array':
typeResult = isArray(current);
break;
case 'object':
typeResult = typeof current === 'object' && current !== null && !isArray(current);
break;
case 'string':
typeResult = typeof current === 'string';
break;
case 'number':
typeResult = typeof current === 'number';
break;
case 'boolean':
typeResult = typeof current === 'boolean';
break;
case 'null':
typeResult = current === null;
break;
}
}
return typeResult ? (returnValue ? current : true) : (returnDefault ? defaultValue : false);
} else {
return returnValue ? current : true;
}
};
/**
* Check if provided variable is an array.
*
* @param a
* @returns {boolean}
*/
function isArray(a) {
return (
Array.isArray
? Array.isArray(a)
: a && a.toString() === '[object Array]'
)
}
/**
* Provide a way to play nicely with others in the global scope: will revert window.isDefinedType back to the state
* it was in before this scripts was included.
* @returns {isDefinedType}
*/
isDefinedType.noConflict = function () {
root.isDefinedType = prev_isDefinedType;
return isDefinedType;
};
/**
* Allow usage by both browsers and node
*/
if (typeof exports !== 'undefined') {
if (typeof module !== 'undefined' && module.exports) {
exports = module.exports = isDefinedType;
}
exports.isDefinedType = isDefinedType;
}
else {
root.isDefinedType = isDefinedType;
}
}).call(this);