Created
July 24, 2014 07:24
-
-
Save aurri/d0d22b0d303a894fb1eb to your computer and use it in GitHub Desktop.
Find items in a collection
This file contains 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
// Find items in a collection | |
var _ = module.exports = {} | |
_.find = function(c, s){ return search(c, s, {one:true}) } | |
_.findIndex = function(c, s){ return search(c, s, {one:true, index:true}) } | |
_.findLast = function(c, s){ return search(c, s, {one:true, last:true}) } | |
_.findLastIndex = function(c, s){ return search(c, s, {one:true, index:true, last:true}) } | |
_.filter = function(c, s){ return search(c, s) } | |
function search(collection, search, opt){ | |
opt = opt || {} | |
function matchProps(item){ | |
return Object.keys(search).every(function(k){ | |
return item[k] === search[k] | |
}) | |
} | |
var test = typeof search === 'function' ? search : matchProps | |
var isArray = typeof collection.length === 'number' | |
var keys = Object.keys(collection) | |
var results = [] | |
var start = 0 | |
var end = keys.length - 1 | |
// Iterate | |
for(var i = start; i <= end; i++){ | |
// Reverse | |
var ix = opt.last ? end - i : i | |
// Get key & item | |
var key = keys[ix] | |
if(isArray) key = +key | |
var item = collection[key] | |
// Test and return or collect results | |
if(test(item, key)){ | |
var res = opt.index ? key : item | |
if(opt.one) return res | |
results.push(res) | |
} | |
} | |
if(!opt.one) return results | |
} | |
///////////////////////////////////////////////// | |
//// Tests | |
/* | |
;(function test(){ | |
var arr = [ {i:0, v:1}, {i:1, v:0}, {i:2, v:0}, {i:3, v:1}] | |
var obj = {a:{i:0, v:1}, b:{i:1, v:0}, c:{i:2, v:0}, d:{i:3, v:1}} | |
var test = suite() | |
test( _.filter(arr, {v:0}), [{i:1, v:0}, {i:2, v:0}]) | |
test( _.find(arr, {v:0}), {i:1, v:0}) | |
test( _.findLast(arr, {v:0}), {i:2, v:0}) | |
test( _.findIndex(arr, {v:0}), 1) | |
test(_.findLastIndex(arr, {v:0}), 2) | |
test( _.filter(arr, {v:1}), [{i:0, v:1}, {i:3, v:1}]) | |
test( _.find(arr, {v:1}), {i:0, v:1}) | |
test( _.findLast(arr, {v:1}), {i:3, v:1}) | |
test( _.findIndex(arr, {v:1}), 0) | |
test(_.findLastIndex(arr, {v:1}), 3) | |
test( _.filter(obj, {v:1}), [{i:0, v:1}, {i:3, v:1}]) | |
test( _.find(obj, {v:1}), {i:0, v:1}) | |
test( _.findLast(obj, {v:1}), {i:3, v:1}) | |
test( _.findIndex(obj, {v:1}), 'a') | |
test(_.findLastIndex(obj, {v:1}), 'd') | |
test.end() | |
})() | |
function suite(){ | |
var line = Array(10).join('-') | |
console.log(line, 'TEST:', line) | |
var fail | |
var test = function(a, b){ | |
a = JSON.stringify(a) | |
b = JSON.stringify(b) | |
var ok = a === b | |
var v = [ok ? 'OK' : 'FAIL', a, b] | |
console[ok ? 'log' : 'error'](v.join(' -- ')) | |
if(!ok) fail = true | |
} | |
test.end = function(){ | |
fail ? console.error(line, 'DANG.', line) | |
: console.log(line, 'YEAH!', line) | |
} | |
return test | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment