Created
July 3, 2015 21:10
-
-
Save lorenzoongithub/72e1c39019c5b9231fab to your computer and use it in GitHub Desktop.
underscore.js
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
load('http://underscorejs.org/underscore-min.js'); | |
function json(x) { return JSON.stringify(x); } | |
sum = 0; | |
_.each([1, 2, 3],function(i) { sum+=i }); | |
if (sum !== 6) throw ''; | |
sum = 0; | |
_.each({one: 1, two: 2, three: 3},function(i) { sum+=i }); | |
if (sum !== 6) throw ''; | |
// map | |
if (json(_.map([1, 2, 3],function(i){ return i*3; })) !== json([3,6,9])) throw '' | |
if (json(_.map({one: 1, two: 2, three: 3}, function(num,key){ return num*3; })) !== json([3,6,9])) throw '' | |
// reduce | |
if (_.reduce([1, 2, 3], function(memo, num){ return memo + num; }, 0) != 6) throw '' | |
// list | |
list = [[0, 1], [2, 3], [4, 5]]; | |
flat = _.reduceRight(list, function(a, b) { return a.concat(b); }, []); | |
if (json(flat) !== json( [4, 5, 2, 3, 0, 1] )) throw ''; | |
// find | |
even = _.find([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; }); | |
if (even !== 2) throw ''; | |
// filter | |
evens = _.filter([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; }); | |
if (json(evens) !== json([2,4,6])) throw ''; | |
// where | |
array = [{a:1,b:1,c:11},{a:1,b:1,c:11}, {a:2,b:2,c:4}]; | |
x = _.where(array, {a:1}); | |
if (json(x) != json( [{a:1,b:1,c:11},{a:1,b:1,c:11}] )) throw ''; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment