Created
October 26, 2011 08:49
-
-
Save bamthomas/1315814 to your computer and use it in GitHub Desktop.
groupBy javascript
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
function groupBy(jsonArray, fun) { | |
return jsonArray.reduce(function(accumulator, object) { | |
var value = fun(object); | |
if (Array.isArray(accumulator[value])) { | |
accumulator[value].push(object); | |
} else { | |
accumulator[value] = [object]; | |
} | |
return accumulator; | |
}, {}); | |
} | |
TestCase("Group by", { | |
"test group by": function () { | |
assertEquals({3: ["foo", "bar", "baz"]}, groupBy(["foo", "bar", "baz"], function(string) { return string.length;})); | |
assertEquals({"b": ["bar", "baz"], "f": ["foo"]}, groupBy(["foo", "bar", "baz"], function(string) { return string[0];})); | |
} | |
}); |
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
server: http://localhost:9876 | |
load: | |
- groupBy.js |
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
java -jar JsTestDriver-1.3.2.jar --port 9876 --tests all --config jsTestDriver.conf --browser /usr/bin/firefox |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment