forked from OperationSpark/lodown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
401 lines (298 loc) · 10.9 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
'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
*/
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);
}
}
}
/*
identity: Designed to return the value of the input parameter unchanged
@param {value} value: the value being returned.
*/
function identity(value){
return value;
};
/*
typeOf: Designed to return the datatype of an input value
@param {array, null, string, object, undefined, number, boolean, function} value: the value that is being inspected.
*/
function typeOf(value){
if (Array.isArray(value)) {
return "array";
}
if (value === null) {
return "null";
}
if (typeof(value) === 'string') {
return "string";
}
if (typeof(value) === 'object') {
return "object";
}
if (value === undefined) {
return "undefined";
}
if (typeof(value) === 'number') {
return "number";
}
if (typeof(value) === 'boolean') {
return "boolean";
}
if (typeof(value) === 'function') {
return "function";
}
};
/*
first: Designed to take an array and a number a return that number of items from the array starting from the 0 index of the array. if no array is given it returns an
empty array. if the number given is not a number, the function only returns the first item from the array.
@param {array} array: the array to reference.
@param {number} number: the number of items that will fill the output array.
*/
function first(array, number){
let firstElement = [];
if (!Array.isArray(array)) {
return [];
}
if (number === isNaN || number === undefined) {
return array[0];
}
if (number > array.length) {
return array;
}
for (let i = 0; i < array.length; i++) {
if (firstElement.length < number) {
firstElement.push(array[i]);
}
else return firstElement;
}
};
/*
last: Designed to take an array and a number and return a new array that contains the input number of items from the original array starting from the last index of the input array.
if the input array is not an array, an empty array is returned. if the input number is not a number, only the last index is returned.
@param {array} array: an array to reference
@param {number} number: the number of items being put into a new array and returned
*/
function last(array, number) {
let lastElement = [];
if (!Array.isArray(array)) {
return [];
}
if (number === isNaN || number === undefined) {
return array[array.length - 1];
}
if (number > array.length) {
return array;
}
for (let i = array.length - 1; i >= 0; i--) {
if (lastElement.length < number) {
lastElement.unshift(array[i]);
}
else return lastElement;
}
}
/*
indexOf: Designed to take an array and a value and return the index of the first occurence of the value in the array.
@param {array} array: an array to search through.
@param {value} value: a value being searched for.
*/
function indexOf(array, value) {
for (let i = 0; i < array.length; i++) {
if (array[i] === value) {
return i
}
}
return -1
}
/*
contains: Designed to take an array and a value and return a boolean based on if the value is found inside the array.
@param {array} array: the array to search through.
@param {value} value: the value being searched for.
*/
function contains(array, value) {
return (array.includes(value)) ? true : false
}
/*
Unique: designed to take an array and return an array with the same values but without any identical values.
@param {array} array: an array to search through.
*/
function unique(array) {
let unique = [...new Set(array)]
return unique
}
/*
filter: designed to take an array and a function. it will call the function for each element in the array that passes the arguments: the element, it's index, array. it will return a new
array of elements that pass.
@param {array} array: an array to iterate through
@param {function} function: a function to call on elements in the array
*/
function filter(array, func) {
let filtered = []
for (let i = 0; i < array.length; i++) {
if (func(array[i], i, array)) {
filtered.push(array[i])
}
}
return filtered
}
/*
reject: designed to take an array and a function and return a new array. the function is called for each element in the array passin the arguments: the element, it's index, array
@param {array} array: an array to iterat through
@param {function} function: a function to call on elements in the array
*/
function reject(array, func) {
let rejected = []
for (let i = 0; i < array.length; i++) {
if (!func(array[i], i, array)) {
rejected.push(array[i])
}
}
return rejected
}
/*
partition: is designed to take an array and a function. it will return an array with two sub-arrays. one sub-array for elements in the array that test false when
the function is called on it, and another of when they test true.
@param {array} array: an array to iterat through
@param {function} function: a function to call on elements in the array
*/
function partition(array, func) {
let truthy = []
let falsey = []
let part = [truthy, falsey]
for (let i = 0; i < array.length; i++) {
if (func(array[i], i, array)) {
truthy.push(array[i])
}
else if (!func(array[i], i, array)) {
falsey.push(array[i])
}
}
return part
}
/*
map: designed to take a collection and a function and return an array. it calls the function for each element in the array that passes the argument if <collection> is an array:
the element, it's index, <collection>; if <collection> is an object:the value, it's key, <collection>; and saves the value into a new array and returns it.
@param {collection} array: an array to loop through.
@param {collection} object: an object to loop through.
@param {function} function: a function to call on the elements that pass.
*/
function map(collection, func) {
let arr = []
each(collection, function(ele, i, col) {
arr.push(func(ele, i, col))
})
return arr
}
/*
pluck: is designed to take an array of objects and a property then return an array containing the value of <property> for every element in <array>.
@param {objArr} array: an array with objects in it.
@param {prop} property: a key name that will be searched for in the objects array.
*/
function pluck(objArr, prop) {
let propArr = []
for (let i = 0; i < objArr.length; i++) {
if (objArr[i].hasOwnProperty(prop)) {
propArr.push(objArr[i][prop])
}
}
return propArr
}
/*
every: is designed to take a collecition and a funciton and return a boolean. it calls the function for every element of the collection with the parameter:
if <collection> is an array: current element, it's index, <collection>; if <collection> is an object: current value, current key, <collection>.
if every element tests true it returns true. if even one element tests false it will return false.
@param {collection} array: an array to loop through.
@param {collection} object: an object to loop through.
@param {function} function: a function to call on the elements
*/
function every(collection, iterator) {
iterator = iterator || identity;
return !!reduce(collection, function(a, b) {
return a && iterator(b);
}, true)
}
/*
some: is designed to take a collection and a function and return a boolean. it calls the function for every element of the collection with the parameter:
if <collection> is an array: current element, it's index, <collection>; if <collection> is an object: current value, current key, <collection>.
if even one element tests true it returns true. if none test true it returns false.
@param {collection} array: an array to loop through.
@param {collection} object: an object to loop through.
@param {function} function: a function to call on the elements
*/
function some(collection, func){
let bool = false
if (!func) {
if (Array.isArray(collection)) {
for (let i = 0; i < collection.length; i++) {
if (collection[i]) bool = true;
}
}
else {
for (let key in collection) {
if (collection[key]) bool = true;
}
}
}
else if (typeof func === 'function') {
if (Array.isArray(collection)) {
for (let i = 0; i < collection.length; i++) {
if (func(collection[i], i, collection)) {
bool = true;
}
}
}
else {
for (let key in collection) {
if (func(collection[key], key, collection)) {
bool = true;
}
}
}
}
return bool
}
/*
reduce: is designed to take an array a function and a seed then return the seed. the seed take the value it is given. if no seed is given it returns the return value of the final function call
@param {array} array: an array to loop through
@param {function} function: a function to call for every element in the array passing the arguments: previous result, element, index.
@param {seed} value: a seed that is returned after it has been altered by the function.
*/
function reduce(array, func, seed) {
if (seed !== undefined) {
for (let i = 0; i < array.length; i++) {
seed = func(seed, array[i], i, array);
}
return seed
}
else {
seed = array[0]
for (let i = 1; i < array.length; i++) {
seed = func(seed, array[i], i, array)
}
return seed
}
}
/*
extend: is designed to take at least two objects but can have more and return a new object with all the key value pairs of all the objects into one.
@param {obj} object: an object
@param {obj} object: an object
@param {...objects} objects: more objects.
*/
function extend(obj1, obj2, ...objects){
let extended = Object.assign(obj1, obj2, ...objects)
return extended
}
module.exports.each = each;