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
| license: MIT |
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
| var myArr = [1,2,3,4,5,6,7,8,6,7,8,6]; | |
| //last with all the safeties using a for loop | |
| last = function last(arr, number){ | |
| var newArr = []; | |
| if(!Array.isArray(arr)){ | |
| return []; | |
| }else if(isNaN(number) || number === undefined ){ | |
| return arr[arr.length - 1]; | |
| }else if(number < 0){ | |
| 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
| //takes any number of objects and creates one unified object. | |
| _.extend1 = (...objects) =>{ | |
| const target = objects[0]; | |
| _.each(objects, (obj, i, objects) =>{ | |
| if(i === 0)return; | |
| _.each(obj,( val, key, obj)=>{ | |
| target[key] = val; | |
| }); | |
| }); |
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
| /*literally does everything... takes a collection, a call-back | |
| function and a starting state(this could be a number, ) | |
| */ | |
| _.reduce = function(collection, fn, seed){ | |
| var prevResult = seed; | |
| if(seed === undefined) seed = collection[0], prevResult = 1; | |
| _.each(collection, function(el, i, collection){ | |
| prevResult = fn(prevResult, el, i); | |
| }); | |
| return prevResult; |
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; | |
| } |
NewerOlder