-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlowbar.js
More file actions
354 lines (285 loc) · 6.74 KB
/
Copy pathlowbar.js
File metadata and controls
354 lines (285 loc) · 6.74 KB
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
let _ = {};
///////////////////////////////
_.identity = function(value) {
return value;
};
//////////////////////////
_.first = function(array, n) {
let result = [];
let i = 0;
if (arguments.length === 1) {
return array[0];
} while (i < n) {
result.push(array[i]); i++;
};
return result;
};
//////////////////////////
_.last = function(array, n) {
if(arguments.length === 1) {
let reverse = array.reverse();
return reverse[0];
};
let result = [];
for(let i = n-1; i < array.length; i++) {
result.push(array[i])
}
return result;
};
//////////////////////////
_.each = function(list, iteratee) {
if(Array.isArray(list)) {
for (let i = 0; i < list.length; i++) {
iteratee(list[i], i, list);
}
}
else {
for(let key in list) {
iteratee(list[key], key, list);
}
}
return list;
}
//////////////////////////
_.indexOf = function(array, value) {
for (let i = 0; i < array.length; i++) {
if (array[i] === value) {
return i;
}
}
if (arguments.length === 1) {
return -1;
}
return -1;
}
//////////////////////////
_.filter = function(list, func) {
let filtered = [];
if(Array.isArray(list)) {
for(let i = 0; i < list.length; i++) {
if (func(list[i])) {
filtered.push(list[i]);
}
}
} else {
for(let key in list) {
if (func(list[key])) {
filtered.push(list[key]);
}
}
}
return filtered;
}
//////////////////////////
_.reject = function(list, func) {
let rejected = [];
if (Array.isArray(list)) {
for (let i = 0; i < list.length; i++) {
if (!func(list[i])) {
rejected.push(list[i]);
}
}
} else {
for (let key in list) {
if (!func(list[key])) {
rejected.push(list[key]);
}
}
}
return rejected;
}
//////////////////////////
_.uniq = function(array) {
let uniqueArray = [];
if (arguments.length < 1) return array;
for (let i = 0; i < array.length; i++) {
if (!uniqueArray.includes(array[i])) {
uniqueArray.push(array[i]);
};
}
return uniqueArray;
};
//////////////////////////
_.map = function(list, iteratee) {
let newMap = [];
if (arguments.length < 1) return list;
if (Array.isArray(list)) {
for(let i = 0; i < list[i]; i++) {
let newValue = iteratee(list[i], i, list);
newMap.push (newValue);
}
}
else {
for (let key in list) {
let newValue = iteratee(list[key], key, list);
newMap.push(newValue);
}
}
return newMap;
};
//////////////////////////
_.pluck = function(list, propertyName) {
let newMap = [];
if (arguments.length < 1) return list;
for (let i = 0; i < list.length; i++) {
newMap.push(list[i][propertyName])
}
return newMap;
};
//////////////////////////
_.reduce = function(list, iteratee) {
let result;
if (!arguments) return list;
_.each (list, function (value) {
if (result === undefined) {
result = value;
} else {
result = iteratee(result, value);
}
});
return result;
};
//////////////////////////
_.contains = function(list, value, fromIndex) {
let bool;
if (arguments.length < 1) return list;
for (let i = fromIndex || 0; i < list.length; i++) {
if (list[i] === value) bool = true;
else bool = false;
};
return bool;
};
//////////////////////////
_.every = function(list, predicate) {
if (arguments.length < 1) return list;
for (let i = 0; i < list.length; i++) {
if (!predicate(list[i])) {
return false;
}
}
return true;
};
//////////////////////////
_.some = function(list, predicate) {
if (arguments.length < 1) return list;
for (let i = 0; i < list.length; i++) {
if (predicate(list[i])) {
return true;
}
}
return false;
};
//////////////////////////
_.extend = function(destination, sources) {
if(!sources) return destination;
return Object.assign({}, destination, sources)
};
//////////////////////////
_.defaults = function(object, defaults) {
if (!defaults) return object;
_.each(defaults, function (value, key) {
if(object[key] === undefined)
object[key] = value
})
return object;
}
//////////////////////////
_.indexOf = function(array, value) {
let min = 0;
let max = array.length - 1;
let mid;
if (!Array.isArray(array) || !value) return -1;
while (min <= max) {
mid = Math.floor((max + min) / 2);
if (array[mid] === value) return mid;
else if (array[mid] < value) min = mid + 1;
else max = mid - 1;
}
return -1;
};
//////////////////////////
_.once = function(func) {
let returnVal, called = false;
return () => {
if (!called) {
called = true;
returnVal = func.apply(this, arguments);
}
return returnVal;
};
};
//////////////////////////
_.memoize = function(func) {
const cache = {};
const fn = (key) => {
let index = JSON.stringify(key);
if (index in cache) {
return cache[index];
} else {
let memo = func.apply(null, arguments);
cache[index] = memo;
return memo;
}
};
fn.cache = cache;
return fn;
};
//////////////////////////
_.delay = function(func, wait) {
let arg = Array.prototype.slice.call(arguments, 2);
return setTimeout(function () {
return func.apply(null, arg);
}, wait);
};
//////////////////////////
_.shuffle = function(list) {
let count = list.length;
while (count > 0) {
let i = Math.floor(Math.random() * count);
count --;
let temp = list[count];
list[count] = list[i];
list[i] = temp;
}
return list;
};
//////////////////////////
_.invoke = function(list, method) {
let listCopy = list.slice();
return listCopy.map(element => {
if (typeof(method) === 'string') {
method = element[method];
}
if (method === undefined) return undefined;
return method.apply(element);
});
};
//////////////////////////
_.sortBy = function(list, iteratee) {
return list.sort((a, b) => {
return iteratee(a) - iteratee(b);
});
};
//////////////////////////
_.zip = function(arrays) {
let result = [];
let list = Array.prototype.slice.call(arguments);
for (let i = 0; i < list.length; i++) {
let newArray = [];
for (let j = 0; j < list.length; j++) {
newArray.push(list[j][i]);
}
result.push(newArray);
}
return result;
};
//////////////////////////
_.flatten = function(array) {
if (!Array.isArray(array)) return [];
return array.reduce((result, element) => {
return result.concat(element);
},[]);
};
if (typeof module !== 'undefined') {
module.exports = _;
}