-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLowbar.js
397 lines (356 loc) · 8.65 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
385
386
387
388
389
390
391
392
393
394
395
396
397
const _ = {};
_.identity = input => {
return input;
};
_.first = (input, n) => {
if (typeof input === "string") {
input = input.split("");
}
if (Array.isArray(input)) {
if (n) return input.slice(0, n);
return input[0];
}
return undefined;
};
_.last = (input, n) => {
if (typeof input === "string") {
input = input.split("");
}
if (Array.isArray(input)) {
if (n) return input.slice(input.length - n, input.length);
return input[input.length - 1];
}
return undefined;
};
_.each = (list, iteratee = _.identity, context) => {
if (!context) context = this;
if (Array.isArray(list) || typeof list === "string") {
for (let i = 0; i < list.length; i++) {
iteratee.call(context, list[i], i, list);
}
} else {
for (let key in list) {
iteratee.call(context, list[key], key, list);
}
}
};
_.indexOf = (array, value, boolean) => {
if (!Array.isArray(array)) return -1;
for (let i = 0; i < array.length; i++) {
if (array[i] === value) return i;
}
return -1;
};
_.filter = (list, predicate = _.identity, context) => {
if (!context) context = this;
let results = [];
if (Array.isArray(list)) {
for (let i = 0; i < list.length; i++) {
if (predicate.call(context, list[i])) {
results.push(list[i]);
}
}
return results;
}
if (typeof list === "object") {
let result = {};
for (let key in list) {
if (predicate.call(context, list[key])) {
result[key] = list[key];
}
return result;
}
} else return [];
};
_.reject = (list, predicate = _.identity, context) => {
if (!context) context = this;
if (!Array.isArray(list)) return [];
let results = [];
for (let i = 0; i < list.length; i++) {
if (!predicate.call(context, list[i])) {
results.push(list[i]);
}
}
return results;
};
_.uniq = array => {
let result = [];
for (let i = 0; i < array.length; i++) {
if (!result.includes(array[i])) {
result.push(array[i]);
}
}
return result;
};
_.map = (input, fn = _.identity, context) => {
let result = [];
if (!context) context = this;
if (typeof input === "object" && !Array.isArray(input)) {
for (let key in input) {
result.push(fn.call(context, input[key]));
}
}
for (let i = 0; i < input.length; i++) {
result.push(fn.call(context, input[i]));
}
return result;
};
_.contains = (input, value) => {
if (typeof input === "object" && !Array.isArray(input)) {
for (let key in input) {
if (input[key] === value) return true;
}
}
for (let i = 0; i < input.length; i++) {
if (input[i] === value) return true;
}
return false;
};
_.pluck = (list, property) => {
let result = [];
if (Array.isArray(list)) {
for (let i = 0; i < list.length; i++) {
for (let key in list[i]) {
if (key === property) {
result.push(list[i][key]);
}
}
}
}
return result;
};
_.reduce = (list, iteratee = _.identity, memo, context) => {
if (!context) context = this;
_.each(list, function (item) {
if (memo === undefined) {
return (memo = list[0]);
} else {
return (memo = iteratee.call(context, memo, item));
}
});
return memo;
};
_.once = fn => {
if (typeof fn !== "function") return fn;
let alreadyCalled = false;
let returnValue;
if (alreadyCalled) return returnValue;
return () => {
if (!alreadyCalled) {
alreadyCalled = true;
returnValue = fn.apply(null, arguments);
}
return returnValue;
};
};
_.shuffle = input => {
let result = [];
if (!Array.isArray(input) && typeof input === "object") {
for (let key in input) {
result.push(input[key]);
}
input = result;
}
if (typeof input === "string") {
input = input.split("");
}
if (Array.isArray(input)) {
for (let i = 0; i < input.length; i++) {
let j = Math.floor(Math.random() * (i + 1));
[input[i], input[j]] = [input[j], input[i]];
}
return input;
}
return result;
};
_.invoke = (list, methodName, ...args) => {
const results = [];
const cb = value => {
results.push(value[methodName].apply(value, args));
};
_.each(list, cb);
return results;
};
_.sortBy = (list, iteratee, context) => {
if (!context) context = this;
if (typeof iteratee === "function") {
return list.sort((a, b) => {
return iteratee.call(context, a) - iteratee.call(context, b);
});
} else {
return list.sort((a, b) => {
if (a[iteratee] < b[iteratee]) return -1;
if (a[iteratee] > b[iteratee]) return 1;
return 0;
});
}
};
_.zip = function () {
const results = [];
if (Array.isArray(arguments[0])) {
for (var i = 0; i < arguments[0].length; i++) {
var resultsPrior = [];
for (var j = 0; j < arguments.length; j++) {
resultsPrior.push(arguments[j][i]);
}
results.push(resultsPrior);
resultsPrior = [];
}
}
return results;
};
_.sortedIndex = (list, value, iteratee, context) => {
if (!context) context = this;
if (!Array.isArray(list) && typeof list !== "object") return list;
if (iteratee) {
return binarySearch(_.sortBy(list, iteratee), value);
} else {
return binarySearch(list, value);
}
};
_.flatten = arr => {
return arr.reduce((flat, toFlatten) => {
return flat.concat(
Array.isArray(toFlatten) ? _.flatten(toFlatten) : toFlatten
);
}, []);
};
_.intersection = (...arrays) => {
let result = [];
let i, j;
for (i = 0; i < arrays[0].length; i++) {
let item = arrays[0][i];
if (_.contains(result, item)) continue;
for (j = 1; j < arrays.length; j++) {
if (!_.contains(arrays[j], item)) break;
}
if (j === arrays.length) result.push(item);
}
return result;
};
_.difference = (array, list) => {
let arrays = _.flatten(list, true);
return array.filter(val => {
return !_.contains(arrays, val);
});
};
_.memoize = passedFunc => {
var cache = {};
return x => {
if (x in cache) return cache[x];
return (cache[x] = passedFunc(x));
};
};
_.negate = predicate => {
return x => {
return !predicate(x);
};
};
_.values = obj => {
let result = [];
if (Array.isArray(obj)) return obj;
if (typeof obj === "object") {
for (let key in obj) {
result.push(obj[key]);
}
}
return result;
};
_.every = (list, predicate, context) => {
if (!context) context = this;
if (typeof predicate === "function") {
for (let i = 0; i < list.length; i++) {
if (predicate.call(context, list[i]) !== true) return false;
}
}
return true;
};
_.some = (list, predicate, context) => {
if (!context) context = this;
if (typeof predicate === "function" && Array.isArray(list)) {
for (let i = 0; i < list.length; i++) {
if (predicate.call(context, list[i])) return true;
}
}
if (typeof predicate === "function" && typeof list === "object") {
for (let key in list) {
if (predicate.call(context, list[key])) return true;
}
}
return false;
};
_.partial = function (func, args) {
args = [].slice.call(arguments, 1);
return function () {
let position = 0;
let length = args.length;
let newArgs = Array(length);
for (let i = 0; i < length; i++) {
if (args[i] === _) {
newArgs[i] = arguments[position++];
} else {
newArgs[i] = args[i];
}
}
while (position < arguments.length) newArgs.push(arguments[position++]);
return func(...newArgs);
};
};
_.delay = function (func, ms, args) {
args = [].slice.call(arguments, 2);
return setTimeout(() => {
func.apply(null, args);
}, ms);
};
_.where = (array, properties) => {
return array.filter(values => {
let count = 0;
for (let key in properties) {
if (values[key] === properties[key]) {
count++;
}
}
if (count === Object.keys(properties).length) return values;
});
};
_.extend = (object, sources) => {
return Object.assign({}, object, sources);
};
_.defaults = (object, defaults) => {
const objKeys = Object.keys(object);
for (let key in defaults) {
if (!_.contains(objKeys, key)) {
object[key] = defaults[key];
}
}
return object;
};
_.throttle = (fn, wait) => {
let callFunc = true;
return function () {
if (callFunc) {
let result = fn.apply(this, arguments)
callFunc = false;
setTimeout(() => callFunc = true, wait);
return result;
}
}
}
module.exports = _;
function binarySearch(list, name) {
let start = 0;
let end = list.length - 1;
for (let i = 0; i < 10; i++) {
var mid = Math.floor((end + start) / 2);
if (list[mid] === name) {
return mid;
}
if (name < list[mid]) {
end = mid - 1;
}
if (name > list[mid]) {
start = mid + 1;
}
}
return mid + 1;
}