-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoverscore.js
422 lines (391 loc) · 12.7 KB
/
overscore.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/*jshint -W116 */
import $ from 'jquery';
let getLength, property, createAssigner, collectNonEnumProps, createEscaper;
const MAXthisARRAYthisINDEX = Math.pow(2, 53) - 1;
/** Class Overscore, replacement for Underscore. */
class Overscore {
/**
* @lends Overscore */
/**
* Returns true if the argument is an object, false if not an object.
* @param {} obj - variable to be tested.
* @return {Boolean} True of false.
*/
static isObject(obj) {
var type = typeof obj;
return type === 'function' || type === 'object' && !!obj;
}
/**
* Returns true if the argument is null, false if not null.
* @param {} o - variable to be tested.
* @return {Boolean} True of false.
*/
static isNull(o) {
return o === null;
}
/**
* Returns true if the argument is a function, false if not.
* @param {} obj - variable to be tested.
* @return {Boolean} True of false.
*/
static isFunction(obj) {
return typeof obj === 'function' || false;
}
/**
* Returns true if the argument is undefined, false if not.
* @param {} obj - variable to be tested.
* @return {Boolean} True of false.
*/
static isUndefined(obj) {
return obj === void 0;
}
/**
* Returns true if the argument is an array, false if not.
* @param {} o - variable to be tested.
* @return {Boolean} True of false.
*/
static isArray(o) {
return Array.isArray(o);
}
/**
* Returns true if the object has a property 'key', false if not.
* @param {} obj - Object to be tested.
* @param {} key - Propery name to be test.
* @return {Boolean} True of false.
*/
static has(obj, key) {
return obj !== null && hasOwnProperty.call(obj, key);
}
/**
* Returns array result of the map 'iterator' with the function 'func' over the array 'arr'.
* @param {Object[]} arr - Array of objects.
* @param {} func - Function to be called with every element in the map 'iterator'.
* @return {Object[]} Array of objects mapped results.
*/
static map(arr, func) {
return arr.map(func);
}
/**
* Extend the prototype and the properties of the objects that are in arguments array.
* @param {...Object} arguments - Object to be extended over the first element.
* @return {Object} Object result.
*/
static extend() {
return $.extend.apply(this, arguments);
}
/**
* Returns array result of the iterator with the function 'i' over the array 'arr'.
* @param {Object[]} arr - Object to be tested.
* @param {} i - Function to be called with every element in the map 'iterator'.
* @return {Object[]} Array of objects result.
*/
static each(arr, i) {
return arr.forEach(i);
}
/**
* Returns array of slices of a specified size.
* @param {Object[]} arr - Object to be tested.
* @param {Number} size - Size for the slices.
* @return {Array[]} Array of Array results.
*/
static chunk(array, size) {
return array.reduce(function(res, item, index) {
if (index % size === 0) { res.push([]); }
res[res.length - 1].push(item);
return res;
}, []);
}
/**
* Create an object with prototype and properties from two objects.
* @param {Object} prototype - Prototype object.
* @param {Object} props - Properties object.
* @return {Object} Object result.
*/
static create(prototype, props) {
let _this = this,
Ctor = function() {};
function baseCreate(prototype) {
if (!_this.isObject(prototype)) { return {}; }
if (Object.create) { return Object.create(prototype); }
Ctor.prototype = prototype;
let result = new Ctor();
Ctor.prototype = null;
return result;
}
let result = baseCreate(prototype);
if (props) { this.extendOwn(result, props); }
return result;
}
/**
* Extend only the properties of the objects that are in arguments array.
* @param {...Object} arguments - Object to be extended over the first element.
* @return {Object} Object result.
*/
static extendOwn() {
createAssigner(this.keys.bind(this)).apply(this, arguments);
}
/**
* Get the keys array from an object.
* @param {Object} obj - Object.
* @return {String[]} Array of the keys found.
*/
static keys(obj) {
let hasEnumBug = !{ toString: null }.propertyIsEnumerable('toString'),
keys = [];
if (!this.isObject(obj)) { return []; }
if (Object.keys) { return Object.keys(obj); }
for (var key in obj) { if (this.has(obj, key)) { keys.push(key); } }
if (hasEnumBug) { collectNonEnumProps.call(this, obj, keys); }
return keys;
}
/**
* Boils down a list of values into a single value.
* @param {Array} obj - Array object.
* @param {} iteratee - Function to do in the reduction.
* @param {number} memo - Initial state of the reduction.
* @param {Object} context
* @return {} Result of the reduce.
*/
static reduce(obj, iteratee, memo, context) {
var keys = !this.isArrayLike(obj) && this.keys(obj),
length = (keys || obj).length,
index = 0;
iteratee = this.optimizeCb(iteratee, context, 4);
if (arguments.length < 3) {
memo = obj[keys ? keys[index] : index];
index += 1;
}
return this.iterator(obj, iteratee, memo, keys, index, length);
}
/**
* Returns a (stably) sorted copy of list, ranked in ascending order by the results of running each value through iteratee.
* iteratee may also be the string name of the property to sort by.
* @param {Array} obj - Array object.
* @param {} iteratee - Function to sort by, or any field to sort by.
* @param {Object} context
* @return {Array}
*/
static sortBy(obj, iteratee, context) {
iteratee = this.cb(iteratee, context);
return this.pluck(this.map(obj, function(value, index, list) {
return {
value: value,
index: index,
criteria: iteratee(value, index, list)
};
}).sort(function(left, right) {
var a = left.criteria;
var b = right.criteria;
if (a !== b) {
if (a > b || a === void 0) { return 1; }
if (a < b || b === void 0) { return -1; }
}
return left.index - right.index;
}), 'value');
}
static uniqBy(arr, iteratee) {
if (typeof iteratee === 'string') {
let f = [];
return arr.filter(function(n) {
return f.indexOf(n[iteratee]) == -1 && f.push(n[iteratee]);
});
}
if (typeof iteratee === 'function') {
let f = [];
return arr.filter(function(n) {
return f.indexOf(iteratee(n)) == -1 && f.push(iteratee(n));
});
}
return arr.filter((v, i, a) => a.indexOf(v) == i);
}
/**
* A convenient version of what is perhaps the most common use-case for map: extracting a list of property values.
* @param {Array} obj - Array object.
* @param {string} key
* @return {Array}
*/
static pluck(obj, key) {
return this.map(obj, property(key));
}
static iterator(obj, iteratee, memo, keys, index, length) {
for (; index >= 0 && index < length; index += 1) {
var currentKey = keys ? keys[index] : index;
memo = iteratee(memo, obj[currentKey], currentKey, obj);
}
return memo;
}
static optimizeCb(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);
};
}
static isArrayLike(collection) {
var length = getLength(collection);
return typeof length === 'number' && length >= 0 && length <= MAXthisARRAYthisINDEX;
}
static cb(value, context, argCount) {
if (value === null) { return this.identity; }
if (this.isFunction(value)) { return this.optimizeCb(value, context, argCount); }
if (this.isObject(value)) { return this.matcher(value); }
return property(value);
}
static template(text, settings) {
if (!this.templateSettings) this.templateSettings = { interpolate: /\{\{(.+?)\}\}/g };
settings = this.defaults({}, settings, this.templateSettings);
let template, argument, escapes = {
'\'': '\'',
'\\': '\\',
'\r': 'r',
'\n': 'n',
'\u2028': 'u2028',
'\u2029': 'u2029'
},
matcher = RegExp([
(settings.escape || /(.)^/).source,
(settings.interpolate || /(.)^/).source,
(settings.evaluate || /(.)^/).source
].join('|') + '|$', 'g'),
index = 0,
source = '__p+=\'';
text.replace(matcher, function(match, escape, interpolate, evaluate, offset) {
source += text.slice(index, offset).replace(/\\|'|\r|\n|\u2028|\u2029/g, function(match) {
return '\\' + escapes[match];
});
index = offset + match.length;
if (escape) {
source += '\'+\n((__t=(' + escape + '))==null?\'\':this.escape(__t))+\n\'';
} else if (interpolate) {
source += '\'+\n((__t=(' + interpolate + '))==null?\'\':__t)+\n\'';
} else if (evaluate) {
source += '\';\n' + evaluate + '\n__p+=\'';
}
return match;
});
source += '\';\n';
if (!settings.variable) { source = 'with(obj||{}){\n' + source + '}\n'; }
source = 'var __t,__p=\'\',__j=Array.prototype.join,' +
'print=function(){__p+=__j.call(arguments,"");};\n' +
source + 'return __p;\n';
try {
var render = new Function(settings.variable || 'obj', source); // jshint ignore:line
} catch (e) {
e.source = source;
throw e;
}
template = function(data) {
return render.call(this, data);
};
argument = settings.variable || 'obj';
template.source = 'function(' + argument + '){\n' + source + '}';
return template;
}
static defaults() {
return createAssigner(this.allKeys.bind(this), true).apply(this, arguments);
}
static allKeys(obj) {
let key, keys = [],
hasEnumBug = !{ toString: null }.propertyIsEnumerable('toString');
if (!this.isObject(obj)) { return []; }
for (key in obj) {
if (obj.hasOwnProperty(key)) {
keys.push(key);
}
}
if (hasEnumBug) { collectNonEnumProps.call(this, obj, keys); }
return keys;
}
static escape() {
return createEscaper.call(this, {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
'\'': ''',
'`': '`'
}).apply(this, arguments);
}
static unescape() {
return createEscaper.call(this, {
''': '\'',
'`': '`',
'&': '&',
'>': '>',
'<': '<',
'"': '"',
}).apply(this, arguments);
}
static result(object, property, fallback) {
return !object ? fallback || null :
this.isFunction(object[property]) ? object[property].call(object) : object[property];
}
}
property = function(key) {
return function(obj) {
return obj === null ? void 0 : obj[key];
};
};
createAssigner = 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;
};
};
collectNonEnumProps = function(obj, keys) {
let nonEnumerableProps = ['valueOf', 'isPrototypeOf', 'toString',
'propertyIsEnumerable', 'hasOwnProperty', 'toLocaleString'
],
nonEnumIdx = nonEnumerableProps.length,
constructor = obj.constructor,
proto = (this.isFunction(constructor) && constructor.prototype) || Object.prototype,
prop = 'constructor';
if (this.has(obj, prop) && !this.contains(keys, prop)) { keys.push(prop); }
while (nonEnumIdx--) {
prop = nonEnumerableProps[nonEnumIdx];
if (prop in obj && obj[prop] !== proto[prop] && !this.contains(keys, prop)) {
keys.push(prop);
}
}
};
createEscaper = function(map) {
var escaper = function(match) {
return map[match];
};
var source = '(?:' + this.keys(map).join('|') + ')';
var testRegexp = RegExp(source);
var replaceRegexp = RegExp(source, 'g');
return function(string) {
string = string === null ? '' : '' + string;
return testRegexp.test(string) ? string.replace(replaceRegexp, escaper) : string;
};
};
getLength = property('length');
export default Overscore;