Skip to content

Commit 4a83fcd

Browse files
committed
version 0.2.0 is out, with inject -> reduce, JS standard methodname aliases, a compose(), and a lastIndexOf()
1 parent 6d52832 commit 4a83fcd

File tree

8 files changed

+294
-158
lines changed

8 files changed

+294
-158
lines changed

index.html

Lines changed: 113 additions & 54 deletions
Large diffs are not rendered by default.

package.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// ServerJS package specification.
2+
{
3+
"name": "underscore",
4+
"description": "Functional programming aid for Javascript. Works well with jQuery.",
5+
"url": "http://documentcloud.github.com/underscore/",
6+
"keywords": ["util", "functional", "server", "client", "browser"],
7+
"author": "Jeremy Ashkenas <jeremy@documentcloud.org>",
8+
"maintainer": "Kris Kowal <kris.kowal@cixar.com>",
9+
"contributors": [],
10+
"dependencies": [],
11+
"lib", "."
12+
}

test/arrays.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,11 @@ $(document).ready(function() {
4949
equals(_.indexOf(numbers, 2), 1, 'can compute indexOf, even without the native function');
5050
});
5151

52+
test("arrays: lastIndexOf", function() {
53+
var numbers = [1, 0, 1, 0, 0, 1, 0, 0, 0];
54+
numbers.lastIndexOf = null;
55+
equals(_.lastIndexOf(numbers, 1), 5, 'can compute lastIndexOf, even without the native function');
56+
equals(_.lastIndexOf(numbers, 0), 8, 'lastIndexOf the other element');
57+
});
58+
5259
});

test/collections.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ $(document).ready(function() {
1414
var answers = [];
1515
_.each([1, 2, 3], function(num) { answers.push(num * this.multiplier);}, {multiplier : 5});
1616
equals(answers.join(', '), '5, 10, 15', 'context object property accessed');
17+
18+
answers = [];
19+
_.forEach([1, 2, 3], function(num){ answers.push(num); });
20+
equals(answers.join(', '), '1, 2, 3', 'aliased as "forEach"');
1721
});
1822

1923
test('collections: map', function() {
@@ -24,9 +28,12 @@ $(document).ready(function() {
2428
equals(tripled.join(', '), '3, 6, 9', 'tripled numbers with context');
2529
});
2630

27-
test('collections: inject', function() {
28-
var sum = _.inject([1,2,3], 0, function(sum, num){ return sum + num; });
31+
test('collections: reduce', function() {
32+
var sum = _.reduce([1, 2, 3], 0, function(sum, num){ return sum + num; });
2933
equals(sum, 6, 'can sum up an array');
34+
35+
sum = _.inject([1, 2, 3], 0, function(sum, num){ return sum + num; });
36+
equals(sum, 6, 'aliased as "inject"');
3037
});
3138

3239
test('collections: detect', function() {
@@ -35,12 +42,15 @@ $(document).ready(function() {
3542
});
3643

3744
test('collections: select', function() {
38-
var evens = _.select([1,2,3,4,5,6], function(num){ return num % 2 == 0; });
45+
var evens = _.select([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });
3946
equals(evens.join(', '), '2, 4, 6', 'selected each even number');
47+
48+
evens = _.filter([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });
49+
equals(evens.join(', '), '2, 4, 6', 'aliased as "filter"');
4050
});
4151

4252
test('collections: reject', function() {
43-
var odds = _.reject([1,2,3,4,5,6], function(num){ return num % 2 == 0; });
53+
var odds = _.reject([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });
4454
equals(odds.join(', '), '1, 3, 5', 'rejected each even number');
4555
});
4656

@@ -50,6 +60,7 @@ $(document).ready(function() {
5060
ok(!_.all([true, false, true]), 'one false value');
5161
ok(_.all([0, 10, 28], function(num){ return num % 2 == 0; }), 'even numbers');
5262
ok(!_.all([0, 11, 28], function(num){ return num % 2 == 0; }), 'an odd number');
63+
ok(_.every([true, true, true]), 'aliased as "every"');
5364
});
5465

5566
test('collections: any', function() {
@@ -58,6 +69,7 @@ $(document).ready(function() {
5869
ok(_.any([false, false, true]), 'one true value');
5970
ok(!_.any([1, 11, 29], function(num){ return num % 2 == 0; }), 'all odd numbers');
6071
ok(_.any([1, 10, 29], function(num){ return num % 2 == 0; }), 'an even number');
72+
ok(_.some([false, false, true]), 'aliased as "some"');
6173
});
6274

6375
test('collections: include', function() {

test/functions.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,14 @@ $(document).ready(function() {
4848
equals(backwards('moe'), 'hi: moe eom', 'wrapped the saluation function');
4949
});
5050

51+
test("functions: compose", function() {
52+
var greet = function(name){ return "hi: " + name; };
53+
var exclaim = function(sentence){ return sentence + '!'; };
54+
var composed = _.compose(exclaim, greet);
55+
equals(composed('moe'), 'hi: moe!', 'can compose a function that takes another');
56+
57+
composed = _.compose(greet, exclaim);
58+
equals(composed('moe'), 'hi: moe!', 'in this case, the functions are also commutative');
59+
});
60+
5161
});

test/utility.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ $(document).ready(function() {
55
test("utility: noConflict", function() {
66
var underscore = _.noConflict();
77
ok(underscore.isUndefined(_), "The '_' variable has been returned to its previous state.");
8+
var intersection = underscore.intersect([-1, 0, 1, 2], [1, 2, 3, 4]);
9+
equals(intersection.join(', '), '1, 2', 'but the intersection function still works');
810
window._ = underscore;
911
});
1012

underscore-min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)