This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //takes a collection and returns a boolean if any of the | |
| //values pass the test | |
| _.some = function some(collection, fn){ | |
| var isTrue = false; | |
| var isFalse = true; | |
| if(_.typeOf(fn) !== 'function') fn = _.identity;//why this is? | |
| _.each(collection, function(el, i, collection){ | |
| fn(el, i, collection) ? isTrue = true : isFalse = false; | |
| }); | |
| if (isTrue === false) return false; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //takes a collection and test and returns a boolean if | |
| //all values pass the test | |
| _.every = (col, test) => { | |
| if (!test) test = _.identity; | |
| let passed = true; | |
| _.each(col, (item, i, col) =>{ | |
| if(!test(item, i, col)) passed = false; | |
| }); | |
| return passed | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //takes a collection and checks if it has a value in it | |
| _.contains = function contains(collection, value){ | |
| var tOrF = false; | |
| var valIs; | |
| _.each(collection, function(el, i, collection){ | |
| tOrF = (el === value ? valIs = true : false); | |
| }); | |
| if (valIs !== undefined) return valIs; | |
| return tOrF; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //takes an array and property and returns an array of those values | |
| _.pluck = function pluck(arr, property){ | |
| var values = []; | |
| _.map(arr, function(el, i, arr){ | |
| values.push(el[property]); | |
| }); | |
| return values; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //takes a collection and a function with (el,i,col) | |
| //and returns a new collections modified | |
| _.map1 = function map(collection, fn){ | |
| var newCollection = []; | |
| _.each(collection, function(el, i, collection){ | |
| newCollection.push(fn(el,i,collection)); | |
| }); | |
| return newCollection; | |
| } | |
| //steve |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //takes an array and returns an array of unique values | |
| _.unique = function unique(list){ | |
| var unArray = []; | |
| _.each(list, function(el, i, list){ | |
| if(_.indexOf(unArray, el) === -1) unArray.push(el); | |
| }); | |
| return unArray; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //takes an array and a function and returns two arrays that | |
| //are falsey and truthy | |
| "use strict"; | |
| _.partition1 = function (list, test) { | |
| var truthy = []; | |
| var falsey = []; | |
| _.each(list, function (el, i, list) { | |
| if (test(el, i, list)) truthy.push(el); | |
| if (test(el, i, list) !== true) falsey.push(el); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* Reject works like filter but in reverse, it returns an | |
| array containing all the elements that did't pass the test. | |
| */ | |
| //version using each | |
| _.reject = function(list, test){ | |
| var newArr = []; | |
| _.each(list, function(el, i, list){ | |
| if(test(el, i, list) !== true) newArr.push(el); | |
| }); | |
| return newArr; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /*Filter takes an array and a callback function that passes | |
| a test with available (element, index, array) arguments and | |
| returns an array with all the values that pass the test. | |
| */ | |
| _.filter = function filter(list, test){ | |
| var newArr = []; | |
| _.each(list, function(el, i, list){ | |
| if (test(el, i, list)) newArr.push(el); | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //takes an array and a value and returns the first index | |
| //where that value appears. | |
| //this version uses 'each' to iterate through the array. | |
| _.indexOf = function(list, val){ | |
| let outVal = -1; | |
| _.each(list, function(el, i, list){ | |
| if(outVal === -1 && el === val) outVal = i; | |
| }); | |
| return outVal; |