-
Notifications
You must be signed in to change notification settings - Fork 1
/
noobjs_funk.js
660 lines (589 loc) · 22.5 KB
/
noobjs_funk.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
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
module.exports = (function() {
var funk = {};
funk.VERSION = '0.0.1';
/*
---------------Collection Methods-------------------------
*/
// Iterate through a collection and invoke the callback on each
// element if it's an array or value if it's an object.
// The callback is called with (element, index, list) if it's
// an array or string, and (value, key, list) if it's an object
var each = funk.each = funk.forEach = function( collection, callback ) {
var length, i, key;
// Check for null and undefined, and false and 0
if( !(isArray(collection) || isObject(collection) || (typeof collection === 'string') ) ) {
return collection;
}
// Check to see if collection is an array or string
// and perform the callback. If it has the length, it will
// return a number and this will fire. Otherwise, it will return
// 0, which would return the collection, or undefined, which when
// converted to a number would be NaN
length = collection.length;
if( length === +length) {
for( i = 0; i < length; i++ ) {
// callback( element, index, list )
callback( collection[i], i, collection );
}
} else {
// Otherwise, iterate through the collection as if it's an
// object and call the callback on each value.
for( key in collection ) {
// callback( value, key, list )
callback( collection[key], key, collection );
}
}
// Return the collection for chaining methods
return collection;
};
// Iterate through a collection and invoke the callback on each
// element if it's an array or value if it's an object.
// The callback is called with (element, index, list) if it's
// an array or string, and (value, key, list) if it's an object
funk.map = function( collection, callback ) {
// Save the results that the callback returns
var results = [];
// Iterate through the collection and pass the element, index,
// and list into the callback, and push the returned values
// to the results array.
each( collection, function( value, index, list ) {
results.push( callback( value, index, list ));
});
// If the collection was not actually a collection, the
// each method should have return it as is, which would
// be the first element in the array Otherwise, return
// the results array.
if( results[0] === collection ) {
return [];
}
return results;
};
// Iterate through a collection and invoke the callback on each
// element if it's an array or value if it's an object, and
// it turns a list of values into one value, returning it.
// The callback is called with ( memory, value, index, list )
funk.reduce = function( collection, callback, initial ) {
var memory = initial;
funk.each( collection, function( value, index, list ) {
memory = callback( memory, value, index, list );
});
// If collection wasn't really a collection, return the
// initial value. Otherwise, return the memory.
if( memory === collection ) {
return initial;
}
return memory;
};
// pluck takes an array of objects as it's first argument,
// and a property name as it's second argument, and goes through
// the objects and returns an array of the values for the property name.
funk.pluck = function( list, property ) {
var results = [],
i;
// Iterate through the list of objects passed into pluck
each( list, function( object ) {
// push the value of the matching property in the object
results.push( object[property] );
});
return results;
};
// find takes a collection and a callback. It goes through each
// value until if finds a value that causes the callback to return
// true. If no match is found, it returns undefined.
funk.find = function( collection, callback ) {
// Iterate through the collection to look for a match
for( var key in collection ) {
// If the callback returns true, return the value and call it a day!
if( callback( collection[key] )) {
return collection[key];
}
}
// If no match was found, return undefined
return undefined;
};
// filter takes a collection and a truthy test (predicate), and returns
// all values into an array that pass the test. It should return an empty
// array if no values match
funk.filter = function( collection, truther ) {
// store the matches
var results = [];
// Iterate through the collection
each( collection, function( value ) {
// If the value passes the truther test
if( truther( value ) ) {
// push the value into the results array
results.push( value );
}
// Otherwise, do nothing
});
// return the results
return results;
};
// where looks through each value in a collection, and returns
// an array of each key-value pair that matches the list properties.
// i.e. funk.where( listOfPlayers, { team: 'Packers', year: 1967 });
// -> [ { team: 'Packers', year: 1967, name: 'Bart Starr' },
// { team: 'Packers', year: 1967, name: 'Willie Davis'}, ...]
funk.where = function( collection, properties ) {
// Save the matches in an array
var results = [];
// Iterate through each item in the collection and test them
each( collection, function( object ) {
// Innocent until proven guilty
// Iterate through the properites and see if we have
// pairs that don't match
for( var key in properties ) {
// If we find one that doesn't match, return undefined
if( object[key] !== properties[key] ) {
return;
}
}
// If all properties were found in the object and
// we didn't return undefined, we have a match
return results.push( object );
});
return results;
};
// findWhere looks through a list and finds the first value
// that matches keys and values passed in, and returns it.
// funk.findWhere( myList, { color: 'green', size: 'S' });
// -> { color: 'green', size: 'S', name: 'My t-shirt' }
funk.findWhere = function( collection, properties ) {
var i, key, object, match;
// Iterate through each item until you find a match
for( i = 0; i < collection.length; i++ ) {
// Save a shortcut to the current object
object = collection[i];
// Set match to true
match = true;
// Check each key/value pair in the properties
for( key in properties ) {
// If we find one that doesn't match, set match to false
// and break the for in loop
if( object[key] !== properties[key] ) {
match = false;
break;
}
}
// If all properties matched an object, return it.
if( match ) {
return object;
}
}
// No matches were found. Return undefined.
return undefined;
};
// reject returns a list without the elements that
// passed the truth test (predicate). The opposite of filter.
funk.reject = function( collection, truther ) {
// store the rejects
var results = [];
// Iterate through the collection
each( collection, function( value ) {
// If the value fails the truther test
if( !truther( value ) ) {
// push the value into the results array
results.push( value );
}
// Otherwise, do nothing
});
// return the results
return results;
};
// every returns true if all items in a list pass the
// truthy test, and false if any items fail. However,
// it returns true if not pass a list.
funk.every = function( collection, truther ) {
// Because result is set to true, if collection is
// never iterated through because it's not a collection,
// every will return true. This seems usual behavior,
// but we're keeping it to match Underscore.
var result = true;
// Check that collection is not null or undefined
if( collection == null ) {
return result;
}
/******************************
Write this into a function as it's in many methods
*******************************/
// Iterate through the collection
each( collection, function( value ) {
// If the value fails the truther test
if( !truther( value ) ) {
// Set result to false
return result = false;
}
});
// Return result, which would still be true if
// no items failed the truther test
return result;
};
// Returns true if any of the values in a collection
// pass the predicate truth test. If no predicate is
// provided it will just check if any of the values are truthy.
funk.some = function( collection, predicate ) {
// If collection is not actually a collection, return false.
if( !collection ) {
return false;
}
// If a predicate was not passed into the arguments,
// set the predicate to return the value for a straight
// truthy test.
if( !predicate ) {
predicate = function( value ) {
return value;
}
}
// Iterate through the collection
for( var key in collection ) {
// And run the values through the predicate to test truthiness
if( predicate( collection[key] ) ) {
// And return true if any values pass the predicate test
return true;
}
}
// If no values passed the predicate test, return false.
return false;
};
// Returns true if any values in the collection strictly
// equal the value passed in. Use indexOf if passed an array.
funk.contains = function( collection, value ) {
// Check if the collection is an array
if( isArray( collection )) {
// Use indexOf to see if the array contains the value
if( indexOf( collection, value ) !== -1 ) {
// Return true if it return an index that's not -1
return true;
}
// Otherwise, return false.
return false;
}
// Iterate through the collection
for( var key in collection ) {
// See if the value in collection matches the value argument
if( collection[key] === value ) {
// Return true if there's a match
return true;
}
}
// Otherwise, return false
return false;
};
/*
-------------------Array Methods------------------
*/
// indexOf finds the index of the first instance a value
// appears in an array, or -1 if it is not present.
// This is tests with ===, so it returns exact matches only.
// Returns -1 if an array isn't passed in
var indexOf = funk.indexOf = function( list, value ) {
// Make sure that list is an array or string, otherwise return -1
if( list == null || list.length !== +list.length ) {
return -1;
}
// Iterate through the array to find the first instance of the value
var length = list.length;
for( var i = 0; i < length; i++ ) {
if( list[i] === value ) {
// Return the index if the value is found
return i;
}
}
// Return -1 if the value isn't found.
return -1;
};
// union computes the union of passed in array or values, which is the
// unique set of values from one or more arrays, in order.
// i.e. funk.union( [ 1, 2, 4 ], [ 0, 1, 2, 3, 6 ], -1 );
// -> returns [ 1, 2, 4, 0, 3, 6, -1 ]
funk.union = function( ) {
// Access the arguments passed into the function
var args = arguments,
length = args.length,
// Using an object to store element as keys to find uniques
unique = {},
// store unique arrays to differentiate from string
uniqueArrays = {},
// If a value is unique, push it to the results array
results = [],
// store hold the value to save typing
temp,
i, j;
// Iterate through the arguments to find all of the unique values
for( i = 0; i < length; i++ ) {
temp = args[i];
if( !funk.isArray(temp) ) {
if( !unique[temp] ) {
unique[temp] = true;
results.push( temp );
}
} else {
for(j = 0; j < temp.length; j++ ) {
// When trying to use an array as a key to an object, it will
// be converted to a string first. Fortunately, it will also
// be converted to a string when you test the key. However,
// if you have '1,2,3' and [ 1, 2, 3 ] in your set, the object
// test will think they're the same object. Thus, the extra code.
if( funk.isArray( temp[j] )) {
if( !uniqueArrays[temp[j]] ) {
uniqueArrays[temp[j]] = true;
results.push(temp[j]);
}
}
// If it's not an array, check for uniqueness
else if( !unique[temp[j]] ) {
unique[temp[j]] = true;
results.push( temp[j] );
}
}
}
}
return results;
};
// Deep union -> like union put digs down
// zip merges together the values of each of the arrays with the values
// at the corresponding position.
// i.e. funk.zip( [1, 2], ['a', 'b'])
// -> [[ 1, 'a' ], [ 2, 'b' ]]
funk.zip = function() {
var max_length = 0,
results = [],
i, j;
// Check to see if more than one argument was passed into the method
if( arguments.length === 1 ) {
for( i = 0; i < arguments[0].length; i++ ) {
results[i] = arguments[0][i];
}
return results;
}
// Find the longest array passed in and save that as it will
// be the length of the result
for( i = 0; i < arguments.length; i++ ) {
if( arguments[i].length > max_length ) {
max_length = arguments[i].length;
}
}
// Iterate through all of the arrays in order and add
// the elements into the results array at the corresponding
// position
for( i = 0; i < arguments.length; i++ ) {
for( j = 0; j < arguments[i].length; j++ ) {
if( results[j] === undefined ) {
results[j] = [];
}
results[j].push( arguments[i][j] );
}
}
return results;
};
// Flatten flattens a nested array of any depth. Takes an optional
// parameter of shallow. If true, the array will only be flattened
// a single level.
// i.e. funk.flatten( [[[[1]]], [2], 3]) -> [ 1, 2, 3 ]
// or... funk.flatten( [[[1]], [2], 3]) -> [[1], 2, 3 ]
var flatten = funk.flatten = function( array, shallow ) {
var results = [];
// Iterate through the array to check if it's an array
each( array, function( value ) {
if( isArray( value )) {
// If value is an array, pass it into the _flattener function
// which will flatten the array and push the elements to the
// results array.
_flattener( value, shallow, function( value ) {
results.push( value );
});
}
else {
// If value is not an array, push it to the results array
results.push( value );
}
});
return results;
};
// Helper function that takes an array, iterates through it,
// and if the element is an array, it calls itself recursively
// if shallow is not true, or it sends the element into the callback
var _flattener = function( value, shallow, callback ) {
// Check that the value is an array
if( isArray( value ) ) {
// Iterate through the array
each( value, function( val ) {
// If shallow is false and val is an array,
// call flattener recursively
if( !shallow && isArray( val )) {
_flattener( val, false, callback );
}
// Otherwise, send val into the callback
else {
callback( val );
}
});
}
};
/*
-------------Object Methods---------------
*/
// NaN is the only value in JavaScript that doesn't equal itself.
// As little sense as that makes, it means that it's really easy to test
funk.isNaN = function( value ) {
return value !== value;
};
// Checks to see if value is an instance of array
var isArray = funk.isArray = function( value ) {
return value instanceof Array;
};
// Checks to see if value is an object
var isObject = funk.isObject = function( value ) {
// See if value still equals itself if you turn it into an object
return value === Object(value);
};
/*
Another method to see if a value is an object would be the
typeof operator. However, the typeof operator will return
'function' for Object and Function, which are actually objects.
*/
// Values pulls all of the values out of an object
funk.values = function( object ) {
var results = [];
// Iterate through the object
for( var key in object ) {
// If object is an object or array, it will push the
// values to the results array. Otherwise, this will
// not be called and an empty array will be returned
results.push( object[key] );
}
return results;
};
// Keys pulls out all of the keys from an object
funk.keys = function( object ) {
var results = [];
for( var key in object ) {
// If object is an object or array, it will push the keys
// to the results array. Otherwise, this will not be called
// and an empty array will be returned;
results.push( key );
}
return results;
};
// Invert takes an object and switches the values and the keys.
// The values should be unique, or invert will overwrite them
// when it gets to the next matching value.
// i.e. { a: 1, b: 2 } -> { 1: 'a', 2: 'b' }
funk.invert = function( object ) {
var results = {};
// Iterate through the object to flip keys and values
for( var key in object ) {
// If object is an object or array, it will set the key in
// results to be the value of object, and the value in results
// to be the key in object. Otherwise, it will skip this
// and return an empty object.
results[ object[key] ] = key;
}
return results;
};
// pick takes an object as it's first argument, and keys or
// arrays of keys as the subsequent arguments, and returns
// an object with only those keys.
funk.pick = function( object ) {
var keys = [],
results = {},
i, key;
// Iterate through the arguments, skipping the first, which
// should be object, and push all of the keys to the keys
// array.
for( i = 1; i < arguments.length; i++ ) {
// If the argument exists, continue
if( arguments[i] != null ) {
// If it's an array, go through each element and add
// it to the keys array.
if( isArray( arguments[i] )) {
each( arguments[i], function( key ) {
// The object's keys will be strings, so in order for
// the indexOf function to identify matches, it will
// need to compare it with strings.
keys.push( String(key) );
});
}
else {
// If the argument is not an array, convert it to a string
// and push it to the keys array.
keys.push( String(arguments[i]) );
}
}
}
// Iterate through the object to find all of the matching keys.
// If it's a match, add the key and value to the results object.
// Otherwise, ignore it.
for( key in object ) {
if( indexOf( keys, key ) !== -1 ) {
results[key] = object[key];
}
}
// Underscore will fire an error if pick is not passed an
// array or object. This, however, returns an empty object
// at the moment.
return results;
};
// Extend copies all properties in the source arguments into the
// destination object, and returns the destination object. It's
// in order, so if any properties are named the same thing, the
// last one will be the one in the object
// i.e. funk.extend( {a: 1}, {b: 2, c: 3}, { d: 4 } )
// -> {a: 1, b: 2, c: 3, d: 4}
// or... funk.extend( {a: 1}, {b: 2}, {b: 3} )
// -> { a: 1, b: 3 }
funk.extend = function( destination ) {
// Iterate through the arguments, skipping the first,
// to get all of the source objects
for( var i = 1; i < arguments.length; i++ ) {
// Iterate through all of the properties in the argument
for( var key in arguments[i] ) {
// and add it to the destination object
destination[key] = arguments[i][key];
}
}
// return the modified object
return destination;
};
/*
---------------Other Methods---------------
*/
// Method to get a little funky and print 'Funky Town' to console
funk.getFunky = function() {
funk.each( funkyTown, function( value ) {
console.log(value);
});
return true;
};
var funkyTown = [
'',
'FFFFFFFFFFFFFFFFFFF FFFF',
'FFFFFFFFFFFFFFFFFFF FFFF',
'FFFF FFFF',
'FFFF FFFF FFFF',
'FFFFFFFFFFFFFF FFFF FFFF FFFF FFFFFFFF FFFF FFFF FFFF FFFF',
'FFFFFFFFFFFFFF FFFF FFFF FFFFFFFFFFFFFF FFFF FFFF FFFF FFFF',
'FFFF FFFF FFFF FFFFFF FFFF FFFF FFFF FFFF FFFF',
'FFFF FFFF FFFF FFFF FFFF FFFFFFFFFF FFFF FFFF',
'FFFF FFFF FFFF FFFF FFFF FFFFFF FFFF FFFF FFFF',
'FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFFFFFF ',
'FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFFFF',
'FFFF FFFFFFFFFF FFFF FFFF FFFF FFFF FFFF ',
' FFFF',
' FFFF',
'FFFFFFFFFFFFFFFFFFFF FFFF',
'FFFFFFFFFFFFFFFFFFFF FFFF',
' FFFF ',
' FFFF FFFFFFF ',
' FFFF FFFFFFFFFFF FFFF FFFF ',
' FFFF FFFF FFFF FFFF FFFF FFFF FFFFFFFF',
' FFFF FFFF FFFF FFFF FFFF FFFF FFFFFFFFFFFFFF',
' FFFF FFFF FFFF FFFF FFFFFF FFFF FFFFFF FFFF',
' FFFF FFFF FFFF FFFF FFFFFFFF FFFF FFFF FFFF',
' FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF',
' FFFF FFFF FFFF FFFFFFFF FFFFFFFF FFFF FFFF',
' FFFF FFFFFFFFFFF FFFFFF FFFFFF FFFF FFFF',
' FFFF FFFFFFF FFFF FFFF FFFF FFFF'
];
return funk;
})();