/
utils.js
279 lines (239 loc) · 7.74 KB
/
utils.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/* Most of these functions are from the wonderful Underscore package http://underscorejs.org/
This file exists so that the planoutjs library doesn't depend on a few unneeded third party dependencies
so that consumers of the library don't have to include dependencies such as underscore. As well, this helps reduce
the file size of the resulting library.
*/
var trimTrailingWhitespace = function(str) {
return str.replace(/^\s+|\s+$/g, '');
};
var getParameterByName = function(name) {
var hasLocation = typeof location !== 'undefined';
var hasWindow = typeof window !== 'undefined';
var queryParamVal;
if (hasLocation) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
queryParamVal = results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
else {
queryParamVal = "";
}
if (queryParamVal === null || queryParamVal === undefined || queryParamVal.length === 0) {
if (hasWindow && window.localStorage !== undefined && window.localStorage !== null) {
return window.localStorage.getItem(name);
}
}
return queryParamVal;
};
var deepCopy = function (obj) {
var newObj = obj;
if (obj && typeof obj === 'object') {
newObj = Object.prototype.toString.call(obj) === "[object Array]" ? [] : {};
for (var i in obj) {
newObj[i] = deepCopy(obj[i]);
}
}
return newObj;
};
var isObject = function(obj) {
var type = typeof obj;
return type === 'function' || type === 'object' && !!obj;
};
var isArray = function(object) {
if (Array.isArray) {
return Array.isArray(object);
} else {
return Object.prototype.toString.call(object) === '[object Array]';
}
};
var isFunction = function(obj) {
return typeof obj == 'function' || false;
};
//extend helpers
var keys = function(obj) {
if (!isObject(obj)) return [];
if (Object.keys) return Object.keys(obj);
var keys = [];
for (var key in obj) if (has(obj, key)) keys.push(key);
if (hasEnumBug) collectNonEnumProps(obj, keys);
return keys;
};
var allKeys = function(obj) {
if (!isObject(obj)) return [];
var keys = [];
for (var key in obj) keys.push(key);
if (hasEnumBug) collectNonEnumProps(obj, keys);
return keys;
};
var extendHolder = function(keysFunc, undefinedOnly) {
return function(obj) {
var length = arguments.length;
if (length < 2 || obj == null) return obj;
for (var index = 1; index < length; index++) {
var source = arguments[index],
keys = keysFunc(source),
l = keys.length;
for (var i = 0; i < l; i++) {
var key = keys[i];
if (!undefinedOnly || obj[key] === void 0) obj[key] = source[key];
}
}
return obj;
};
};
//extend functionality from underscore
var extend = extendHolder(allKeys);
var extendOwn = extendHolder(keys);
/* underscore helpers */
var identity = function(value) {
return value;
};
var isMatch = function(object, attrs) {
var keys = keys(attrs), length = keys.length;
if (object == null) return !length;
var obj = Object(object);
for (var i = 0; i < length; i++) {
var key = keys[i];
if (attrs[key] !== obj[key] || !(key in obj)) return false;
}
return true;
};
var matcher = function(attrs) {
attrs = extendOwn({}, attrs);
return function(obj) {
return isMatch(obj, attrs);
};
};
var cb = function(value, context, argCount) {
if (value == null) return identity;
if (isFunction(value)) return optimizeCb(value, context, argCount);
if (isObject(value)) return matcher(value);
return property(value);
};
var optimizeCb = function(func, context, argCount) {
if (context === void 0) return func;
switch (argCount == null ? 3 : argCount) {
case 1: return function(value) {
return func.call(context, value);
};
case 2: return function(value, other) {
return func.call(context, value, other);
};
case 3: return function(value, index, collection) {
return func.call(context, value, index, collection);
};
case 4: return function(accumulator, value, index, collection) {
return func.call(context, accumulator, value, index, collection);
};
}
return function() {
return func.apply(context, arguments);
};
};
//from underscore
var forEach = function(obj, iteratee, context) {
iteratee = optimizeCb(iteratee, context);
var i, length;
if (isArrayLike(obj)) {
for (i = 0, length = obj.length; i < length; i++) {
iteratee(obj[i], i, obj);
}
} else {
var theKeys = keys(obj);
for (i = 0, length = theKeys.length; i < length; i++) {
iteratee(obj[theKeys[i]], theKeys[i], obj);
}
}
return obj;
};
//map functionality from underscore
var map = function(obj, iteratee, context) {
iteratee = cb(iteratee, context);
var theKeys = !isArrayLike(obj) && keys(obj),
length = (theKeys || obj).length,
results = Array(length);
for (var index = 0; index < length; index++) {
var currentKey = theKeys ? theKeys[index] : index;
results[index] = iteratee(obj[currentKey], currentKey, obj);
}
return results;
};
//reduce functionality from underscore
var reduce = function(obj, iteratee, memo, context) {
iteratee = optimizeCb(iteratee, context, 4);
var theKeys = !isArrayLike(obj) && keys(obj),
length = (theKeys || obj).length,
index = 0;
if (arguments.length < 3) {
memo = obj[theKeys ? theKeys[index] : index];
index += 1;
}
for (; index >= 0 && index < length; index ++) {
var currentKey = theKeys ? theKeys[index] : index;
memo = iteratee(memo, obj[currentKey], currentKey, obj);
}
return memo;
};
//clone functionality from underscore
var shallowCopy = function(obj) {
if (!isObject(obj)) return obj;
return isArray(obj) ? obj.slice() : extend({}, obj);
};
/* helper functions from underscore */
var property = function(key) {
return function(obj) {
return obj == null ? void 0 : obj[key];
};
};
var MAX_ARRAY_INDEX = Math.pow(2, 53) - 1;
var getLength = property('length');
var isArrayLike = function(collection) {
var length = getLength(collection);
return typeof length == 'number' && length >= 0 && length <= MAX_ARRAY_INDEX;
};
var has = function(obj, key) {
return obj != null && Object.prototype.hasOwnProperty.call(obj, key);
};
/* All these are helper functions to deal with older versions of IE :(*/
var hasEnumBug = !{toString: null}.propertyIsEnumerable('toString');
var nonEnumerableProps = ['valueOf', 'isPrototypeOf', 'toString',
'propertyIsEnumerable', 'hasOwnProperty', 'toLocaleString'];
function collectNonEnumProps(obj, keys) {
var nonEnumIdx = nonEnumerableProps.length;
var constructor = obj.constructor;
var proto = (isFunction(constructor) && constructor.prototype) || Object.Prototype;
var prop = 'constructor';
if (has(obj, prop) && !contains(keys, prop)) keys.push(prop);
while (nonEnumIdx--) {
prop = nonEnumerableProps[nonEnumIdx];
if (prop in obj && obj[prop] !== proto[prop] && !contains(keys, prop)) {
keys.push(prop);
}
}
}
var contains = function(obj, item, fromIndex, guard) {
if (!isArrayLike(obj)) obj = vals(obj);
if (typeof fromIndex != 'number' || guard) fromIndex = 0;
return obj.indexOf(item) >= 0;
};
var vals = function(obj) {
var objectKeys = keys(obj);
var length = objectKeys.length;
var values = Array(length);
for (var i = 0; i < length; i++) {
values[i] = obj[objectKeys[i]];
}
return values;
};
var range = function(max) {
var l = [];
for (var i = 0; i < max; i++) {
l.push(i);
}
return l;
};
var hasKey = function(obj, key) {
return typeof obj[key] !== 'undefined';
};
export { deepCopy, map, reduce, getParameterByName, forEach, isFunction, trimTrailingWhitespace, hasKey, shallowCopy, extend, isObject, isArray, range };