Skip to content

Commit

Permalink
merging ratbeard's numerous improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
jashkenas committed Feb 24, 2010
2 parents 8756331 + 31f0f8c commit a2aab7c
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 68 deletions.
1 change: 1 addition & 0 deletions index.html
Expand Up @@ -4,6 +4,7 @@
<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
<meta http-equiv="X-UA-Compatible" content="chrome=1">
<title>Underscore.js</title>
<script src="underscore.js"></script>
<style>
body {
font-size: 16px;
Expand Down
7 changes: 7 additions & 0 deletions test/collections.js
Expand Up @@ -164,5 +164,12 @@ $(document).ready(function() {
test('collections: size', function() {
equals(_.size({one : 1, two : 2, three : 3}), 3, 'can compute the size of an object');
});

test('collections: buildLookup', function() {
same(_.buildLookup([1,'hi']), {1:true, 'hi':true}, 'defaults values to true');
same(_.buildLookup([1,'hi'], 1), {1:1, 'hi':1}, 'can override value');
same(_.buildLookup({5:'five'}), {five: true}, 'making a map from an object uses its values');
});


});
21 changes: 19 additions & 2 deletions test/objects.js
Expand Up @@ -11,13 +11,13 @@ $(document).ready(function() {
});

test("objects: functions", function() {
var expected = ["all", "any", "bind", "bindAll", "breakLoop", "clone", "compact",
var expected = ["alias", "all", "any", "bind", "bindAll", "breakLoop", "buildLookup", "clone", "compact",
"compose","defer", "delay", "detect", "each", "every", "extend", "filter", "first",
"flatten", "foldl", "foldr", "forEach", "functions", "head", "identity", "include",
"indexOf", "inject", "intersect", "invoke", "isArguments", "isArray", "isDate", "isElement", "isEmpty", "isEqual",
"isFunction", "isNaN", "isNull", "isNumber", "isRegExp", "isString", "isUndefined", "keys", "last", "lastIndexOf", "map", "max",
"methods", "min", "noConflict", "pluck", "range", "reduce", "reduceRight", "reject", "rest", "select",
"size", "some", "sortBy", "sortedIndex", "tail", "tap", "template", "toArray", "uniq",
"size", "some", "sortBy", "sortedIndex", "tail", "tap", "template", "times", "toArray", "uniq",
"uniqueId", "values", "without", "wrap", "zip"];
ok(_(expected).isEqual(_.methods(_)), 'provides a sorted list of functions');
var obj = {a : 'dash', b : _.map, c : (/yo/), d : _.reduce};
Expand Down Expand Up @@ -60,6 +60,8 @@ $(document).ready(function() {
ok(_.isEmpty([]), '[] is empty');
ok(!_.isEmpty({one : 1}), '{one : 1} is not empty');
ok(_.isEmpty({}), '{} is empty');
ok(_.isEmpty(null), 'null is empty');
ok(_.isEmpty(), 'undefined is empty');

var obj = {one : 1};
delete obj.one;
Expand Down Expand Up @@ -183,4 +185,19 @@ $(document).ready(function() {
value();
ok(returned == 6 && intercepted == 6, 'can use tapped objects in a chain');
});


test("objects: alias", function() {
_.alias('isEqual', 'isTheSame');
ok(_.isTheSame(9, 9), 'by default aliases methods on underscore');
delete _.isTheSame;
//
var o = { hi: function () {return 9;} };
_.alias(o, 'hi', 'ho');
equals(o.ho(), 9, 'can add an alias on another object');
_.alias(o, 'hi', 'there', 'sir');
ok(o.hi==o.sir, 'can add multiple aliases');
});


});
11 changes: 11 additions & 0 deletions test/utility.js
Expand Up @@ -29,6 +29,17 @@ $(document).ready(function() {
while(i++ < 100) ids.push(_.uniqueId());
equals(_.uniq(ids).length, ids.length, 'can generate a globally-unique stream of ids');
});

test("utility: times", function() {
var vals = [];
_.times(3, function (i) { vals.push(i); });
ok(_.isEqual(vals, [0,1,2]), "is 0 indexed");
//
vals = [];
_(3).times(function (i) { vals.push(i); });
ok(_.isEqual(vals, [0,1,2]), "works as a wrapper");
});


test("utility: template", function() {
var basicTemplate = _.template("<%= thing %> is gettin' on my noives!");
Expand Down

0 comments on commit a2aab7c

Please sign in to comment.