Skip to content

Instantly share code, notes, and snippets.

View jwgoedert's full-sized avatar

James Wesley Goedert jwgoedert

View GitHub Profile
@jwgoedert
jwgoedert / jsbin.hoyale.js
Created January 30, 2017 21:02 — forked from anonymous/index.html
Some!
//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;
@jwgoedert
jwgoedert / jsbin.towaceq.js
Created January 30, 2017 21:01 — forked from anonymous/index.html
Every!
//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
};
@jwgoedert
jwgoedert / jsbin.fodigu.js
Created January 30, 2017 21:00 — forked from anonymous/index.html
Contains!
//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;
}
@jwgoedert
jwgoedert / jsbin.zecaci.js
Created January 30, 2017 21:00 — forked from anonymous/index.html
Pluck!
//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;
}
@jwgoedert
jwgoedert / jsbin.vabazi.js
Last active January 30, 2017 20:59 — forked from anonymous/index.html
Map!
//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
@jwgoedert
jwgoedert / jsbin.qeyeho.js
Created January 30, 2017 20:58 — forked from anonymous/index.html
Unique!
//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;
}
@jwgoedert
jwgoedert / jsbin.xotecaw.js
Created January 30, 2017 20:58 — forked from anonymous/index.html
Partition!
//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);
@jwgoedert
jwgoedert / jsbin.covabax.js
Created January 30, 2017 20:58 — forked from anonymous/index.html
Reject!
/* 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;
@jwgoedert
jwgoedert / jsbin.zozuror.js
Created January 30, 2017 20:57 — forked from anonymous/index.html
Filter!
/*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);
});
@jwgoedert
jwgoedert / jsbin.metayif.js
Created January 30, 2017 20:57 — forked from anonymous/index.html
IndexOf!
//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;