version 0.2.0 is out, with inject -> reduce, JS standard methodname aliases, a compose(), and a lastIndexOf()

This commit is contained in:
Jeremy Ashkenas
2009-10-28 18:49:50 -04:00
parent 6d52832a73
commit 4a83fcdd26
8 changed files with 294 additions and 158 deletions

View File

@@ -49,4 +49,11 @@ $(document).ready(function() {
equals(_.indexOf(numbers, 2), 1, 'can compute indexOf, even without the native function');
});
test("arrays: lastIndexOf", function() {
var numbers = [1, 0, 1, 0, 0, 1, 0, 0, 0];
numbers.lastIndexOf = null;
equals(_.lastIndexOf(numbers, 1), 5, 'can compute lastIndexOf, even without the native function');
equals(_.lastIndexOf(numbers, 0), 8, 'lastIndexOf the other element');
});
});

View File

@@ -14,6 +14,10 @@ $(document).ready(function() {
var answers = [];
_.each([1, 2, 3], function(num) { answers.push(num * this.multiplier);}, {multiplier : 5});
equals(answers.join(', '), '5, 10, 15', 'context object property accessed');
answers = [];
_.forEach([1, 2, 3], function(num){ answers.push(num); });
equals(answers.join(', '), '1, 2, 3', 'aliased as "forEach"');
});
test('collections: map', function() {
@@ -24,9 +28,12 @@ $(document).ready(function() {
equals(tripled.join(', '), '3, 6, 9', 'tripled numbers with context');
});
test('collections: inject', function() {
var sum = _.inject([1,2,3], 0, function(sum, num){ return sum + num; });
test('collections: reduce', function() {
var sum = _.reduce([1, 2, 3], 0, function(sum, num){ return sum + num; });
equals(sum, 6, 'can sum up an array');
sum = _.inject([1, 2, 3], 0, function(sum, num){ return sum + num; });
equals(sum, 6, 'aliased as "inject"');
});
test('collections: detect', function() {
@@ -35,12 +42,15 @@ $(document).ready(function() {
});
test('collections: select', function() {
var evens = _.select([1,2,3,4,5,6], function(num){ return num % 2 == 0; });
var evens = _.select([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });
equals(evens.join(', '), '2, 4, 6', 'selected each even number');
evens = _.filter([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });
equals(evens.join(', '), '2, 4, 6', 'aliased as "filter"');
});
test('collections: reject', function() {
var odds = _.reject([1,2,3,4,5,6], function(num){ return num % 2 == 0; });
var odds = _.reject([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });
equals(odds.join(', '), '1, 3, 5', 'rejected each even number');
});
@@ -50,6 +60,7 @@ $(document).ready(function() {
ok(!_.all([true, false, true]), 'one false value');
ok(_.all([0, 10, 28], function(num){ return num % 2 == 0; }), 'even numbers');
ok(!_.all([0, 11, 28], function(num){ return num % 2 == 0; }), 'an odd number');
ok(_.every([true, true, true]), 'aliased as "every"');
});
test('collections: any', function() {
@@ -58,6 +69,7 @@ $(document).ready(function() {
ok(_.any([false, false, true]), 'one true value');
ok(!_.any([1, 11, 29], function(num){ return num % 2 == 0; }), 'all odd numbers');
ok(_.any([1, 10, 29], function(num){ return num % 2 == 0; }), 'an even number');
ok(_.some([false, false, true]), 'aliased as "some"');
});
test('collections: include', function() {

View File

@@ -48,4 +48,14 @@ $(document).ready(function() {
equals(backwards('moe'), 'hi: moe eom', 'wrapped the saluation function');
});
test("functions: compose", function() {
var greet = function(name){ return "hi: " + name; };
var exclaim = function(sentence){ return sentence + '!'; };
var composed = _.compose(exclaim, greet);
equals(composed('moe'), 'hi: moe!', 'can compose a function that takes another');
composed = _.compose(greet, exclaim);
equals(composed('moe'), 'hi: moe!', 'in this case, the functions are also commutative');
});
});

View File

@@ -5,6 +5,8 @@ $(document).ready(function() {
test("utility: noConflict", function() {
var underscore = _.noConflict();
ok(underscore.isUndefined(_), "The '_' variable has been returned to its previous state.");
var intersection = underscore.intersect([-1, 0, 1, 2], [1, 2, 3, 4]);
equals(intersection.join(', '), '1, 2', 'but the intersection function still works');
window._ = underscore;
});