-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
165 lines (145 loc) · 3.85 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
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
;(function(root) {
var isObject = function isObject(obj) {
// Implementation lifted from underscore
return typeof obj === 'function' || typeof obj === 'object' && !!obj;
};
var isArray = function isArray(arr) {
return Array.isArray ? Array.isArray(arr) : arr instanceof Array || (arr && arr.constructor && arr.constructor.name === 'Array');
};
var any = function any(list, func) {
var i;
var l = list.length;
for (i = 0; i < l; i++) {
if (func(list[i])) {
return true;
}
}
return false;
};
var every = function every(list, func) {
var i;
var l = list.length;
for (i = 0; i < l; i++) {
if (!func(list[i])) {
return false;
}
}
return true;
};
var isEqual = function isEqual(a, b) {
if (a === b) {
return true;
}
if (a.constructor !== b.constructor) {
return false;
}
if (typeof a === 'object') {
if (a instanceof Array) {
if (a.length !== b.length) {
return false;
}
var i;
var l = a.length;
for (i = 0; i < l; i++) {
if (!isEqual(a[i], b[i])) {
return false;
}
}
return true;
} else {
for (var k in a) {
if (!(k in b) || !isEqual(a[k], b[k])) {
return false;
}
}
return true;
}
} else {
return false;
}
};
var anyOrAll = function anyOrAll(method, _args) {
var paths = [], obj = {};
// args is of type Arguments
var args = [].slice.call(_args);
// Allow array or list of strings
if (isArray(args[1])) {
obj = args[0];
paths = args[1];
} else {
paths = args;
obj = paths.shift();
}
if (isObject(obj)) {
return method(paths, function(path) {
return _safe.safe(obj, path);
});
} else {
return false;
}
};
var _safe = {
safe: function (obj, path, otherwise) {
if (!path) {
return otherwise;
}
obj = isObject(obj) ? obj : {};
var props = path.split('.');
if (props.length === 1) {
if (typeof obj[props[0]] === 'undefined') {
return otherwise;
} else if (obj[props[0]] === null) {
return typeof otherwise === 'undefined' ? null : otherwise;
} else {
return obj[props.shift()];
}
} else {
var prop = props.shift();
return isObject(obj[prop]) ? _safe.safe(obj[prop], props.join('.'), otherwise) : otherwise;
}
},
expand: function (obj, path, thing) {
if (!path || typeof thing === 'undefined') {
return;
}
obj = isObject(obj) && obj !== null ? obj : {};
var props = path.split('.');
if (props.length === 1) {
obj[props.shift()] = thing;
} else {
var prop = props.shift();
if (!(prop in obj)) {
obj[prop] = {};
}
_safe.expand(obj[prop], props.join('.'), thing);
}
},
ensure: function (obj, path, disallowed, otherwise) {
if (arguments.length === 3) {
otherwise = disallowed;
disallowed = [];
}
var current = _safe.safe(obj, path);
if (typeof current === 'undefined' || current === null || any(disallowed, function(item) { return isEqual(current, item); })) {
_safe.expand(obj, path, otherwise);
}
},
allOf: function() {
return anyOrAll.apply(null, [every].concat(arguments));
},
anyOf: function() {
return anyOrAll.apply(null, [any].concat(arguments));
},
noneOf: function() {
return !_safe.anyOf.apply(null, arguments);
}
};
if (typeof exports !== 'undefined') {
if (typeof module !== 'undefined' && module.exports) {
module.exports = _safe;
}
exports._safe = _safe;
}
root._ = root._ || {};
root._._safe = _safe;
})(this);