Created
March 19, 2015 01:07
-
-
Save aguestuser/e21a8926efa0c5936ba3 to your computer and use it in GitHub Desktop.
Home spun tests for FP deduping thing for Tim
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
var _ = require('underscore'); | |
// getDupeList([Tweet]) -> { String : [Int] } | |
// runs in O(n+m) where n is # tweets, m is # dupes | |
function getDupeList(ts){ | |
var tts =_.chain(ts) | |
.map(function(t,i){ return _.extend(t, { i: i });}) | |
.groupBy(function(tt){ return tt.id_str;}) | |
.value(); | |
return _.chain(tts) | |
.keys() | |
.filter(function(k){ return tts[k].length > 1; }) | |
.map(function(kk){ | |
return [kk, _.map(tts[kk], function(tt){ | |
return tt.i;})];}) | |
.object() | |
.value(); | |
} | |
//test getDupeList | |
(function(){ | |
var fn = getDupeList; | |
var purpose = "getDupeList() finds the indices of dupes"; | |
testSimple(); | |
testNoDupes(); | |
testEmpty(); | |
// testSimple() -> Void | |
function testSimple(){ | |
var input = [ | |
{ _id: 1, id_str: "123"}, | |
{ _id: 2, id_str: "123"}, | |
{ _id: 3, id_str: "456"}, | |
{ _id: 4, id_str: "123"}, | |
{ _id: 5, id_str: "456"}, | |
{ _id: 6, id_str: "789"} | |
]; | |
var expected = { | |
"123": [0,1,3], | |
"456": [2,4] }; | |
test(fn(input),expected,purpose + "\n with a list that has dupes"); | |
} | |
// testSimple() -> Void | |
function testNoDupes(){ | |
var input = [ | |
{ _id: 1, id_str: "123"}, | |
{ _id: 2, id_str: "456"}, | |
{ _id: 3, id_str: "789"} | |
]; | |
var expected = {}; | |
test(fn(input),expected,purpose + "\n with a list with no dupes"); | |
} | |
// testSimple() -> Void | |
function testEmpty(){ | |
var input = [], expected = {}; | |
test(fn(input),expected,purpose + "\n with an empty list"); | |
} | |
})(); | |
// test(Any,Any,String) -> Void | |
// prints "PASSING" if actual deepEquals expected | |
// prints "FAILING" and a var dump if actual doesn't deepEqual expected | |
function test(actual,expected,purpose){ | |
if (_.isEqual(actual,expected)){ console.log("PASSING: ", purpose); } | |
else { | |
console.log("FAILED: ", purpose); | |
console.log("-- expected: ", expected); | |
console.log("-- actual: ", actual); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment