forked from OperationSpark/lodown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
546 lines (489 loc) · 17.3 KB
/
index.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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
'use strict';
// YOU KNOW WHAT TO DO //
/**
* each: Designed to loop over a collection, Array or Object, and applies the
* action Function to each value in the collection.
*
* @param {Array or Object} collection: The collection over which to iterate.
*
* @param {Function} action: The Function to be applied to each value in the
* collection.
*
* Usage:
* let array = ["a","b","c"];
* each(array, function(e,i,a){ console.log(e) }); // -> "a"
* // -> "b"
* // -> "c"
*/
function each(collection, action) {
if(Array.isArray(collection)) {
for(var i = 0; i < collection.length; i++) {
action(collection[i], i, collection);
}
} else {
for (var key in collection) {
action(collection[key], key, collection);
}
}
}
module.exports.each = each;
/**
* identity: Takes a value, and returns it unchanged.
*
* @param {Any Datatype} value: Can be any data type / value.
*
* @return {Any Datatype}: Will be the same value as the input value.
*
* Usage:
* console.log(identity(5)); // -) 5
* console.log(identity({a: "b"})); // -> {a: "b"}
*/
function identity(value) {
return value;
}
module.exports.identity = identity;
/**
* tyepOf: Takes a value, and returns a string indicating the data type of the
* value.
*
* @param {Any Datatype} value: Can be any data type / value.
*
* @return {String}: Will be the data type of the input value as a string.
* Data type will be one of:
* - "string"
* - "array"
* - "object"
* - "undefined"
* - "number"
* - "boolean"
* - "null"
* - "function"
*
* Usage:
* console.log(typeOf(134)); // -> "number"
* console.log(typeOf("javascript")); // -> "string"
* console.log(typeOf([1,2,3])); // -> "array"
*/
function typeOf(value) {
if (value === null) {
return 'null';
} else if (Array.isArray(value)) {
return 'array';
} else {
return typeof value;
}
}
module.exports.typeOf = typeOf;
/**
* first: Takes an array and a number, and returns the number of elements
* specified by the number from the beginning of the array.
*
* @param {Array} array: Can be an array containing any data types / values.
*
* @param {Number} number: Can be any whole number.
*
* @return {Array or Array Element}: Will be a subarray from the beginning of
* the array. If the array argument is not
* an array or the number is negative, will
* return an empty array literal. If the
* number is not provided or is not a number,
* will return first element of the array.
*
* Usage:
* let arr = ["a", "b", "c"];
* console.log(first("ponies", 1)); // -> []
* console.log(first(arr, "ponies")); // -> "a"
* console.log(first(arr, 1)); // -> "a"
* console.log(first(arr, 2)); // -> ["a", "b"]
*/
function first(array, number) {
if (!Array.isArray(array) || number < 0) {
return [];
}
if (typeof number !== 'number') {
return array[0];
}
return array.slice(0, number);
}
module.exports.first = first;
/**
* last: Takes an array and a number, and returns the number of elements
* specified by the number from the end of the array.
*
* @param {Array} array: Can be an array containing any data types / values.
*
* @param {Number} number: Can be any whole number.
*
* @return {Array or Array Element}: Will be a subarray from the end of the
* array. If the array argument is not an
* array or the number is negative, will
* return an empty array literal. If the
* number is not provided or is not a number,
* will return the last element of the array.
* If number is greater than the length of
* the array, will return the entire array.
*
* Usage:
* let arr = ["a", "b", "c"];
* console.log(last("ponies", 2)); // -> []
* console.log(last(arr, "ponies")); // -> "c"
* console.log(last(arr, 1)); // -> "c"
* console.log(last(arr, 2)); // -> ["b", "c"]
*/
function last(array, number) {
if (!Array.isArray(array) || number < 0) {
return [];
}
if (typeof number !== 'number') {
return array[array.length - 1];
}
if (number > array.length) {
return array;
} else {
return array.slice(array.length - number , array.length);
}
}
module.exports.last = last;
/**
* indexOf: Takes an array and a value, and returns the index of the array
* that is the first occurrence of the value.
*
* @param {Array} array: Can be an array containing any data types / values.
*
* @param {Any Datatype} value: Can be any data type / value.
*
* @return {Number}: Will be the index of the first occurence of the input
* value in the array. If the input array argument is not
* an array or the input value is not found in the array,
* will return -1.
*
* Usage:
* let arr = ["a","b","c"];
* console.log(indexOf(arr, "c")); // -> 2
* console.log(indexOf(arr, "d")); // -> -1
*/
function indexOf(array, value) {
if (!Array.isArray(array)) {
return -1;
}
for (var i = 0; i < array.length; i++) {
if (array[i] === value) {
return i;
}
}
return -1;
}
module.exports.indexOf = indexOf;
/**
* contains: Takes an array and a value, and returns a boolean determining if
* the array includes the value.
*
* @param {Array} array: Can be an array containing any data types / values.
*
* @param {Any Datatype} value: Can be any data type / value.
*
* @return {Boolean}: Will be true if the array contains the input value;
* otherwise, will be false. If no value is provided, will
* return false.
*
* Usage:
* console.log(contains([1,"two", 3.14], "two")); // -> true
*/
function contains(array, value) {
if (value === undefined || value === null) {
return false;
}
return (array.includes(value) ? true : false);
}
module.exports.contains = contains;
/**
* unique: Takes an array, and returns an array of all elements from the
* array with duplicates removed.
*
* @param {Array} array: Can be an array containing any data types / values.
*
* @return {Array}: Will be an array with duplicates removed.
*
* Usage:
* let arr = [1,2,2,4,5,6,5,2];
* console.log(unique(arr)); // -> [1,2,4,5,6]
*/
function unique(array) {
return array.filter((item, index) => array.indexOf(item) === index);
}
module.exports.unique = unique;
/**
* filter: Takes an array and a function, and returns an array of elements for
* which calling the function returned true.
*
* @param {Array} array: Can be an array containing any data types / values.
*
* @param {Function} test: The Function to be applied to each value in the
* array.
*
* @return {Array}: Will be an array containing the elements which calling test
* function returned true.
*
* Usage:
* let arr = [1,2,3,4,5];
* console.log(filter(arr, function(x){return x%2 === 0})); // -> [2,4]
*/
function filter(array, test) {
let filtered = [];
each(array, (element, index, collection) => {
if (test(element, index, collection)) {
filtered.push(element);
};
});
return filtered;
}
module.exports.filter = filter;
/**
* reject: Takes an array and a function, and returns an array of elements for
* which calling the function returned false.
*
* @param {Array} array: Can be an array containing any data types / values.
*
* @param {Function} test: The Function to be applied to each value in the
* array.
*
* @return {Array}: Will be an array containing the elements which calling test
* function returned false.
*
* Usage:
* let arr = [1,2,3,4,5];
* console.log(reject(arr, function(e){return e%2 === 0})); // -> [1,3,5]
*/
function reject(array, test) {
return array.filter((element, index, collection) => !test(element, index, collection));
}
module.exports.reject = reject;
/**
* partition: Takes an array and a function, and returns an array that consists
* of two arrays: one containing array elements that evaluated to
* true when calling the function and one containing array elements
* that evaluated to false when calling the function.
*
* @param {Array} array: Can be an array containing any data types / values.
*
* @param {Function} test: The Function to be applied to each value in the
* array.
*
* @return {Array}: Will be an array made of of 2 sub arrays:
* - At position 0: An array that contains the array
* values for which calling the test
* function returned true.
* - At position 1: An array that contains the array
* values for which calling the test
* function returned false.
*
* Usage:
* let arr = [1,2,3,4,5];
* console.log(partition(arr, function(element,index,arr){return element % 2 === 0})); // -> [[2,4],[1,3,5]]
*/
function partition(array, test) {
let truthy = [];
let falsy = [];
for (let i = 0; i < array.length; i++) {
if (test(array[i], i, array)) {
truthy.push(array[i]);
} else {
falsy.push(array[i]);
}
}
return [truthy, falsy];
}
module.exports.partition = partition;
/**
* map: Takes a collection and a function, and returns an array of values
* resulting from calling the function.
*
* @param {Array or Object} collection: The collection over which to iterate.
*
* @param {Function} test: The Function to be applied to each value in the
* collection.
*
* @return {Array}: An array with the resulting values from calling the test
* function.
*
* Usage:
* let arr = [1,2,3,4];
* console.log(map(arr, function(e){return e * 2})); // -> [2,4,6,8])
*/
function map(collection, test) {
let result = [];
if (Array.isArray(collection)) {
for (let i = 0; i < collection.length; i++) {
result.push(test(collection[i], i, collection));
}
} else if (typeof collection === 'object') {
for (let key in collection) {
result.push(test(collection[key], key, collection));
}
}
return result;
}
module.exports.map = map;
/**
* pluck: Takes an array of objects and a property, and returns an array
* containing the value of the property for every object in the array.
*
* @param {Array} array: An array of objects.
*
* @param {String} property: A string representing an object property.
*
* @return {Array}: An array containing the corresponding property value.
*
* Usage:
* let arr = [{a: "one"}, {a: "two"}];
* console.log(pluck(arr, "a")); // -> ["one", "two"]
*/
function pluck(array, property) {
return map(array, (element, index, list) => element[property]);
}
module.exports.pluck = pluck;
/**
* every: Takes a collection and a function, and returns true if all elements of
* collection return true when calling the function.
*
* @param {Array or Object} collection: The collection over which to iterate.
*
* @param {Function} test: The Function to be applied to each value in the
* collection
*
* @return {Boolean}: Will be true if all elements of collection have a value
* of true when calling the function. Will be false if just
* one element results in false when calling the function.
* If the function is not provided, return true if every
* element in the collection has a true value; otherwise,
* return false.
*
* Usage:
* let arr = [2,4,6];
* console.log(every(arr, function(e){return e % 2 === 0})); // -> true
* arr = [1,2,3];
* console.log(every(arr, function(e){return e % 2 === 0})); // -> false
*/
function every(collection, test) {
if (typeof test != 'function') {
for (let element of collection) {
if (!element) {
return false;
}
}
return true;
}
let array = map(collection, (element, position, collectiton) => test(element, position, collection));
for (let element of array) {
if (!element) {
return false
}
}
return true;
}
module.exports.every = every;
/**
* some: Takes a collection and a function, and returns true if at least one
* element of the collection returns true when calling the function.
*
* @param {Array or Object} collection: The collection over which to iterate.
*
* @param {Function} test: The Function to be applied to each value in the
* collection.
*
* @return {Boolean}: Will be true if at least one element of collection has a
* value of true when calling the function. Will be false
* if all elements result in false when calling the function.
* If the function is not provided, return true if one
* element in the collection has a true value; otherwise,
* return false.
*
* Usage:
* let arr = [1,3,5];
* console.log(some(arr, function(e){return e % 2 === 0})); // -> false
* arr = [1,2,3];
* console.log(some(arr, function(e){return e % 2 === 0})); // -> true
*/
function some(collection, test) {
if (typeof test != 'function') {
for (let element of collection) {
if (element) {
return true;
}
}
return false;
}
let array = map(collection, (element, position, collectiton) => test(element, position, collection));
for (let element of array) {
if (element) {
return true;
}
}
return false;
}
module.exports.some = some;
/**
* reduce: Takes an array, a function, and a seed. It returns resulting value
* from calling the function for each element in the array.
*
* @param {Array} array: An array of numbers.
*
* @param {Function} test: The Function to be applied to each value in the
* array.
*
* @param {Any Datatype} seed: The value to be used in the first iteration of
* the array. If no seed is provided, the first
* element of the array will be used as the seed
* for the next iteration.
*
* @return {Any Datatype}: The resulting value from calling the input function.
*
* Usage:
* let arr = [1,2,3];
* console.log(reduce(arr, function(previousSum, currentValue, currentIndex){ return previousSum + currentValue }, 0)); // -> 6
*/
function reduce(array, test, seed) {
let previous;
let start;
if (seed === undefined && array.length - 1 > 0) {
previous = array[0];
start = 1;
} else {
previous = seed;
start = 0;
}
for (let i = start; i < array.length; i++) {
previous = test(previous, array[i], i);
}
return previous;
}
module.exports.reduce = reduce;
/**
* extend: Takes at least two objects, and returns only the first object
* updated with all subsequent object properties and values.
*
* @param {Object1, Object2 [, Object3, . . ., ObjectN ]}: Can be at least two
* objects, but there
* may be more objects.
*
* @return {Object}: Will be the first Object updated with properties and values
* from second object at the very minimum. If more objects
* are passed in, their properties and values will also be
* used to update the first object with subsequent object
* properties and values in the order that they are passed in.
*
* Usage:
* var data = {a:"one"};
* console.log(extend(data, {b:"two"})); // -> {a:"one",b:"two"}
* console.log(extend(data, {a:"two"})); // -> {a:"two"}
*/
function extend() {
let args = arguments;
for (let i = 1; i < args.length; i++) {
for (let key in args[i]) {
args[0][key] = args[i][key]
}
}
return args[0];
}
module.exports.extend = extend;