Skip to content

Commit

Permalink
version 0.2.0 is out, with inject -> reduce, JS standard methodname a…
Browse files Browse the repository at this point in the history
…liases, a compose(), and a lastIndexOf()
  • Loading branch information
jashkenas committed Oct 28, 2009
1 parent 6d52832 commit 4a83fcd
Show file tree
Hide file tree
Showing 8 changed files with 294 additions and 158 deletions.
167 changes: 113 additions & 54 deletions index.html

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// ServerJS package specification.
{
"name": "underscore",
"description": "Functional programming aid for Javascript. Works well with jQuery.",
"url": "http://documentcloud.github.com/underscore/",
"keywords": ["util", "functional", "server", "client", "browser"],
"author": "Jeremy Ashkenas <jeremy@documentcloud.org>",
"maintainer": "Kris Kowal <kris.kowal@cixar.com>",
"contributors": [],
"dependencies": [],
"lib", "."
}
7 changes: 7 additions & 0 deletions test/arrays.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});

});
20 changes: 16 additions & 4 deletions test/collections.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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() {
Expand All @@ -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');
});

Expand All @@ -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() {
Expand All @@ -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() {
Expand Down
10 changes: 10 additions & 0 deletions test/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});

});
2 changes: 2 additions & 0 deletions test/utility.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});

Expand Down
2 changes: 1 addition & 1 deletion underscore-min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 4a83fcd

Please sign in to comment.