-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlowbar.js
384 lines (340 loc) · 8.89 KB
/
lowbar.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
/*
DONE --------------------
1. identity
2. first
3. last
4. each
5. indexOf (can't get binary search to work faster)
6. filter
7. reject
8. uniq
9. map
10. pluck
11. reduce
12. contains
13. every
14. some
Additional: where
15. extend
16. defaults
17. once
18. memoize
* 19. delay // not sure how to test this, not sure if setTimeout is working too
20. shuffle
21. invoke
22. sortBy
23. zip
24. flatten
25. intersection
26. difference
TO DO -----------------
27. throttle
*/
var _ = {};
_.identity = function (value) {
return value;
};
_.first = function (arr, num) {
if (!arr) return undefined;
return !num ? arr[0] : arr.slice(0, num);
};
_.last = function (arr, num) {
if (!arr) return undefined;
var len = arr.length;
return !num ? arr[len - 1] : arr.slice(len - num);
};
_.each = function (list, iteratee, context) {
iteratee = iteratee || _.identity;
context = context || this;
// handle arrays
if (Array.isArray(list)) {
for (var i = 0; i < list.length; i++) {
iteratee.call(context, list[i], i, list);
}
} else if (typeof list === 'object') { // handle objects
for (var key in list) {
iteratee.call(context, list[key], key, list);
}
}
return list;
};
_.indexOf = function (list, value, isSorted) {
if (!list || !value) return -1;
// use binary search for when isSorted is TRUE
if (isSorted === true) {
var offset = null;
while (list.length > 1) { // slice in two - take second half
var secondHalf = list.slice(Math.floor(list.length / 2));
if (secondHalf[0] <= value) { // keep second half
offset += Math.floor(list.length / 2);
list = secondHalf;
} else { // retain the first half
list = list.slice(0, Math.floor(list.length / 2));
}
}
return offset;
}
// perform standard search for when isSorted is not TRUE
var startIndex;
typeof isSorted === 'number' ? startIndex = isSorted + 1 : startIndex = 0;
for (var i = startIndex; i < list.length; i++) {
if (list[i] === value) return i - startIndex;
}
return -1;
};
_.filter = function (list, predicate, context) {
list = list || [];
predicate = predicate || _.identity;
var filteredArray = [];
for (var i = 0; i < list.length; i++) {
if (predicate.call(context, list[i])) {
filteredArray.push(list[i]);
}
}
return filteredArray;
};
_.reject = function (list, predicate, context) {
list = list || [];
predicate = predicate || _.identity;
var filteredArray = [];
for (var i = 0; i < list.length; i++) {
if (!predicate.call(context, list[i])) {
filteredArray.push(list[i]);
}
}
return filteredArray;
};
_.uniq = function (list, isSorted, iteratee) {
list = list || [];
isSorted = isSorted || false;
iteratee = iteratee || _.identity;
var uniqueArray = [];
for (var i = 0; i < list.length; i++) {
if (isSorted && (list[i] === list[i - 1])) {
continue; // skip this iteration if this element is same as previous element
}
if (uniqueArray.indexOf(list[i]) === -1) {
uniqueArray.push(list[i]);
}
}
return uniqueArray;
};
_.map = function (list, iteratee, context) {
list = list || [];
iteratee = iteratee || _.identity;
context = context || this;
var result = [];
if (Array.isArray(list)) {
for (var i = 0; i < list.length; i++) {
result.push(iteratee.call(context, list[i], i, list));
}
} else if (typeof (list) === 'object') {
for (var key in list) {
result.push(iteratee.call(context, list[key], key, list));
}
}
return result;
};
_.pluck = function (list, propertyName) {
return _.map(list, function (obj) {
return obj[propertyName];
});
};
_.reduce = function (list, iteratee, memo, context) {
var copyList = list;
if (memo === undefined) {
// make a copy of the list which we can modify
copyList = list.slice();
memo = copyList.shift();
}
for (var i = 0; i < copyList.length; i++) {
memo = iteratee.call(context, memo, copyList[i], i, copyList);
}
return memo;
};
_.contains = function (list, value, fromIndex) {
fromIndex = fromIndex || 0;
return _.indexOf(list.slice(fromIndex), value) !== -1;
};
_.every = function (list, predicate, context) {
list = list || [];
predicate = predicate || _.identity;
for (var i = 0; i < list.length; i++) {
if (!predicate.call(context, list[i])) return false;
}
return true;
};
_.some = function (list, predicate, context) {
list = list || [];
predicate = predicate || _.identity;
for (var i = 0; i < list.length; i++) {
if (predicate.call(context, list[i])) return true;
}
return false;
};
_.where = function (list, properties) {
properties = properties || {};
var result = [];
_.each(list, function (obj) {
var match = true;
for (var key in properties) {
if (properties[key] !== obj[key]) {
match = false;
break;
}
}
if (match) result.push(obj);
});
return result;
};
_.extend = function (destination, ...sources) {
sources = [...arguments];
sources.shift();
_.each(sources, function (sourceObj) {
destination = Object.assign(destination, sourceObj);
});
return destination;
};
_.defaults = function (object, ...defaults) {
_.each(defaults, function (defaultObj) {
_.each(defaultObj, function (value, key) {
if (object[key] === undefined) object[key] = value;
});
});
return object;
};
_.once = function (myFunction) {
var hasRun = false;
var result;
return function () {
if (!hasRun) {
hasRun = true;
result = myFunction.apply(null, arguments);
}
return result;
};
};
_.memoize = function (myFunction, hashFunction) {
var cache = {};
var result;
var speedyFunction = function () {
var stringyArgs;
if (hashFunction) {
var args = [].slice.call(arguments);
stringyArgs = hashFunction.apply(null, args);
} else {
stringyArgs = JSON.stringify(arguments[0]);
}
if (cache.hasOwnProperty(stringyArgs)) {
result = cache[stringyArgs];
} else {
result = myFunction.apply(null, arguments);
cache[stringyArgs] = result;
}
return result;
};
// save the cache as a property on the function
speedyFunction.cache = cache;
return speedyFunction;
};
_.delay = function (myFunc, delay, ...args) {
return setTimeout(function () {
myFunc.apply(null, args);
}, delay);
};
_.shuffle = function (arr) {
var list = arr.slice();
var shuffledList = [];
var index;
while (list.length > 0) {
index = Math.floor(Math.random() * list.length);
shuffledList.push(list[index]);
list.splice(index, 1);
}
return shuffledList;
};
_.invoke = function (list, method, ...args) {
return _.map(list, function (element) {
return element[method](...args);
});
};
_.sortBy = function (list, iteratee, context) {
list = list || [];
iteratee = iteratee || _.identity;
if (typeof iteratee === 'function') {
list = _.map(list, function (element) {
return {
original: element,
modified: iteratee.call(context, element)
};
});
// sort my modified value, but return original value
list.sort(function (a, b) {
return (a.modified < b.modified) ? -1 : (a.modified > b.modified) ? 1 : 0;
});
// return only the original values, in new order
return _.pluck(list, 'original');
} else if (typeof iteratee === 'string') {
return list.sort(function (a, b) {
return (a[iteratee] < b[iteratee]) ? -1 : (a[iteratee] > b[iteratee]) ? 1 : 0;
});
}
};
_.zip = function (...arrays) {
var maxLength = _.reduce(arrays, function (acc, element) {
return element.length > acc ? element.length : acc;
}, 0);
var result = [];
for (var i = 0; i < maxLength; i++) {
var newArr = [];
_.each(arrays, function (singleArr) {
newArr.push(singleArr[i]);
});
result.push(newArr);
}
return result;
};
_.flatten = function (list, shallow) {
var result = [];
var flattenedOneLevel = false;
function flatten (list) {
_.each(list, function (element) {
if (!Array.isArray(element)) { // NOT ARRAY
result.push(element);
} else { // ARRAY
if (flattenedOneLevel && shallow) {
result.push(element);
} else {
flattenedOneLevel = true;
flatten(element);
}
}
});
flattenedOneLevel = false;
}
flatten(list);
return result;
};
_.intersection = function (...arrays) {
var result = [];
_.each(arrays[0], function (element) {
var foundInEvery = _.every(arrays, function (array) {
return _.contains(array, element);
});
if (foundInEvery) result.push(element);
});
return result;
};
_.difference = function (array, ...others) {
var result = [];
_.each(array, function (element) {
var notInOthers = _.every(others, function (array) {
return !_.contains(array, element);
});
if (notInOthers) result.push(element);
});
return result;
};
if (typeof module !== 'undefined') {
module.exports = _;
}