-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
192 lines (160 loc) · 5.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/**
* safe.accessors (c) 2016 Peter Pavlovich <pavlovich@gmail.com>
* safe.accessors is freely distributable under the terms of the MIT license.
* Documentation: https://github.com/pavlovich/safe.accessors
* Version '1.0.10'
*/
;(function() {
if(typeof _ == 'undefined'){
throw 'safe.accessors requires lowdash or underscore.';
}
/** Used to determine if values are of the language type `Object`. */
var objectTypes = {
'function': true,
'object': true
};
/** Detect free variable `exports`. */
var freeExports = objectTypes[typeof exports] && exports && !exports.nodeType && exports;
/** Detect free variable `module`. */
var freeModule = objectTypes[typeof module] && module && !module.nodeType && module;
/** Detect free variable `global` from Node.js. */
var freeGlobal = freeExports && freeModule && typeof global == 'object' && global;
/** Detect free variable `self`. */
var freeSelf = objectTypes[typeof self] && self && self.Object && self;
/** Detect free variable `window`. */
var freeWindow = objectTypes[typeof window] && window && window.Object && window;
/** Detect the popular CommonJS extension `module.exports`. */
var moduleExports = freeModule && freeModule.exports === freeExports && freeExports;
/**
* Used as a reference to the global object.
*
* The `this` value is used if it is the global object to avoid Greasemonkey's
* restricted `window` object, otherwise the `window` object is used.
*/
var root = freeGlobal || ((freeWindow !== (this && this.window)) && freeWindow) || freeSelf || this;
var sa = {};
var isVoid = function isVoid(value){
return _.isNull(value) || _.isUndefined(value);
};
var safeCall = function safeCall(obj, path, defaultValue, functionToCall, context, allowNull, allowUndefined, defaultReturn, allowNullResult, allowUndefinedResult){
var shouldAllowNullResult = _.isUndefined(allowNullResult) ? true : allowNullResult;
var shouldAllowUndefinedResult = _.isUndefined(allowUndefinedResult) ? true : allowUndefinedResult;
var res = safeGet(obj, path, defaultValue, allowNull, allowUndefined);
var hasFunction = _.isFunction(functionToCall);
//var hasDefault = !_.isUndefined(defaultValue);
if(hasFunction) {
if (_.isNull(res)) {
if(allowNull) {
res = functionToCall.call(context, res);
}else {
return defaultReturn;
}
}else if(_.isUndefined(res)){
if(allowUndefined){
res = functionToCall.call(context, res);
}else{
return defaultReturn;
}
}else {
res = functionToCall.call(context, res);
}
}
return _.isUndefined(res) ? (shouldAllowUndefinedResult ? res : defaultReturn) : (_.isNull(res) ? (shouldAllowNullResult ? res : defaultReturn) : res);
};
var safeGet = function safeGet(obj, path, defaultValue, allowNull, allowUndefined){
var res = obj;
var hasPath = _.isString(path) && !_.isEmpty(path);
var hasDefault = !_.isUndefined(defaultValue);
if(_.isUndefined(obj)){
if(hasPath){
return allowUndefined ? undefined : (hasDefault ? (_.isNull(defaultValue) ? (allowNull ? defaultValue : obj) : defaultValue) : obj);
}else{
return allowUndefined ? obj : (hasDefault ? (_.isNull(defaultValue) ? (allowNull ? defaultValue : obj) : defaultValue) : obj);
}
}
if(_.isNull(obj)){
if(hasPath){
return hasDefault ? defaultValue : undefined;
}else{
return allowNull ? obj : (hasDefault ? defaultValue : (allowUndefined ? defaultValue : obj));
}
}
if(hasPath){
var attribNames = path.split('.');
var tripped = false;
res = obj;
_.each(attribNames, function(attribName){
res = res[attribName];
if(_.isVoid(res)){
tripped = true;
return false;
}
});
res = tripped ? (allowUndefined ? undefined : (hasDefault ? (_.isNull(defaultValue) ? (allowNull ? defaultValue : undefined) : defaultValue) : undefined)) : res;
}
return res;
};
var safeSet = function safeSet(obj, path, value, overwrite, returnValueSet){
var isValidString = function(obj){
return !_.isVoid(obj) && !_.isEmpty(obj);
};
var target = _.isVoid(obj) ? {} : obj;
var result = returnValueSet ? value : target;
var shouldOverwrite = _.isVoid(overwrite) ? true : overwrite;
if(!isValidString(path)){
//TODO I think this is wrong.
return overwrite ? value : (returnValueSet ? value : target);
}
var pathComponents = path.split('.');
var lastComponent = _.last(pathComponents);
_.reduce(pathComponents,
function(memo, key){
if(isValidString(key)) {
if (lastComponent === key) {
if (_.isVoid(memo[key])) {
memo[key] = value;
result = value;
} else {
if (shouldOverwrite) {
memo[key] = value;
result = value;
} else {
result = memo[key];
}
}
return result;
}else{
if(_.isVoid(memo[key])){
memo[key] = {};
return memo[key];
}else{
return memo[key];
}
}
}else{
console.log('path provided contains a null/empty component: ' + path);
return memo;
}
}, target);
return returnValueSet ? result : target;
};
sa.VERSION = 'null';
sa.isVoid = isVoid;
sa.safeGet = safeGet;
sa.safeSet = safeSet;
sa.safeCall = safeCall;
sa.install = function install(obj){
obj.isVoid = sa.isVoid;
obj.safeGet = sa.safeGet;
obj.safeSet = sa.safeSet;
obj.safeCall = sa.safeCall;
};
if(freeModule) {
freeModule.exports = sa;
} else {
root.sa = sa;
}
if(typeof _ !== 'undefined'){
sa.install(_);
}
}.call(this));