mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-08 02:17:48 +00:00
finished off the complete test suite for underscore -- let's polish off the corners and provide some docs
This commit is contained in:
45
test/arrays.js
Normal file
45
test/arrays.js
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
$(document).ready(function() {
|
||||||
|
|
||||||
|
module("Array-only functions (last, compact, uniq, and so on...)");
|
||||||
|
|
||||||
|
test("arrays: first", function() {
|
||||||
|
equals(_.first([1,2,3]), 1, 'can pull out the first element of an array');
|
||||||
|
});
|
||||||
|
|
||||||
|
test("arrays: last", function() {
|
||||||
|
equals(_.last([1,2,3]), 3, 'can pull out the last element of an array');
|
||||||
|
});
|
||||||
|
|
||||||
|
test("arrays: compact", function() {
|
||||||
|
equals(_.compact([0, 1, false, 2, false, 3]).length, 3, 'can trim out all falsy values');
|
||||||
|
});
|
||||||
|
|
||||||
|
test("arrays: flatten", function() {
|
||||||
|
var list = [1, [2], [3, [[[4]]]]];
|
||||||
|
equals(_.flatten(list).join(', '), '1, 2, 3, 4', 'can flatten nested arrays');
|
||||||
|
});
|
||||||
|
|
||||||
|
test("arrays: without", function() {
|
||||||
|
var list = [1, 2, 1, 0, 3, 1, 4];
|
||||||
|
equals(_.without(list, 0, 1).join(', '), '2, 3, 4', 'can remove all instances of an object');
|
||||||
|
});
|
||||||
|
|
||||||
|
test("arrays: uniq", function() {
|
||||||
|
var list = [1, 2, 1, 3, 1, 4];
|
||||||
|
equals(_.uniq(list).join(', '), '1, 2, 3, 4', 'can find the unique values of an unsorted array');
|
||||||
|
var list = [1, 1, 1, 2, 2, 3];
|
||||||
|
equals(_.uniq(list, true).join(', '), '1, 2, 3', 'can find the unique values of a sorted array faster');
|
||||||
|
});
|
||||||
|
|
||||||
|
test("arrays: intersect", function() {
|
||||||
|
var stooges = ['moe', 'curly', 'larry'], leaders = ['moe', 'groucho'];
|
||||||
|
equals(_.intersect(stooges, leaders).join(''), 'moe', 'can take the set intersection of two arrays');
|
||||||
|
});
|
||||||
|
|
||||||
|
test("arrays: indexOf", function() {
|
||||||
|
var numbers = [1, 2, 3];
|
||||||
|
numbers.indexOf = null;
|
||||||
|
equals(_.indexOf(numbers, 2), 1, 'can compute indexOf, even without the native function');
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
123
test/collections.js
Normal file
123
test/collections.js
Normal file
@@ -0,0 +1,123 @@
|
|||||||
|
$(document).ready(function() {
|
||||||
|
|
||||||
|
module("Collection functions (each, any, select, and so on...)");
|
||||||
|
|
||||||
|
test("collections: each", function() {
|
||||||
|
_.each([1, 2, 3], function(num, i){
|
||||||
|
equals(num, i + 1, 'each iterators provide value and iteration count');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test('collections: each and throwing "__break__"', function() {
|
||||||
|
var answer = null;
|
||||||
|
_.each([1, 2, 3], function(num){
|
||||||
|
if ((answer = num) == 2) throw '__break__';
|
||||||
|
});
|
||||||
|
equals(answer, 2, 'the loop broke in the middle');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('collections: each can take a context object', 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');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('collections: all', function() {
|
||||||
|
ok(_.all([]), 'the empty set');
|
||||||
|
ok(_.all([true, true, true]), 'all true values');
|
||||||
|
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');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('collections: any', function() {
|
||||||
|
ok(!_.any([]), 'the empty set');
|
||||||
|
ok(!_.any([false, false, false]), 'all false values');
|
||||||
|
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');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('collections: map', function() {
|
||||||
|
var doubled = _.map([1, 2, 3], function(num){ return num * 2; });
|
||||||
|
equals(doubled.join(', '), '2, 4, 6', 'doubled numbers');
|
||||||
|
var tripled = _.map([1, 2, 3], function(num){ return num * this.multiplier; }, {multiplier : 3});
|
||||||
|
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; });
|
||||||
|
equals(sum, 6, 'can sum up an array');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('collections: detect', function() {
|
||||||
|
var result = _.detect([1, 2, 3], function(num){ return num * 2 == 4; });
|
||||||
|
equals(result, 2, 'found the first "2" and broke the loop');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('collections: select', function() {
|
||||||
|
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');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('collections: reject', function() {
|
||||||
|
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');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('collections: include', function() {
|
||||||
|
ok(_.include([1,2,3], 2), 'two is in the array');
|
||||||
|
ok(!_.include([1,3,9], 2), 'two is not in the array');
|
||||||
|
ok(_.include({moe:1, larry:3, curly:9}, 3), '_.include on objects checks their values');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('collections: invoke', function() {
|
||||||
|
var list = [[5, 1, 7], [3, 2, 1]];
|
||||||
|
var result = _.invoke(list, 'sort');
|
||||||
|
equals(result[0].join(', '), '1, 5, 7', 'first array sorted');
|
||||||
|
equals(result[1].join(', '), '1, 2, 3', 'second array sorted');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('collections: pluck', function() {
|
||||||
|
var people = [{name : 'moe', age : 30}, {name : 'curly', age : 50}];
|
||||||
|
equals(_.pluck(people, 'name').join(', '), 'moe, curly', 'pulls names out of objects');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('collections: max', function() {
|
||||||
|
equals(3, _.max([1, 2, 3]), 'can perform a regular Math.max');
|
||||||
|
var neg = _.max([1, 2, 3], function(num){ return -num; });
|
||||||
|
equals(neg, 1, 'can perform a computation-based max');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('collections: min', function() {
|
||||||
|
equals(1, _.min([1, 2, 3]), 'can perform a regular Math.min');
|
||||||
|
var neg = _.min([1, 2, 3], function(num){ return -num; });
|
||||||
|
equals(neg, 3, 'can perform a computation-based min');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('collections: sortBy', function() {
|
||||||
|
var people = [{name : 'curly', age : 50}, {name : 'moe', age : 30}];
|
||||||
|
people = _.sortBy(people, function(person){ return person.age; });
|
||||||
|
equals(_.pluck(people, 'name').join(', '), 'moe, curly', 'stooges sorted by age');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('collections: sortedIndex', function() {
|
||||||
|
var numbers = [10, 20, 30, 40, 50], num = 35;
|
||||||
|
var index = _.sortedIndex(numbers, function(a, b) { return a < b ? -1 : a > b ? 1 : 0; }, num);
|
||||||
|
equals(index, 3, '35 should be inserted at index 3');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('collections: toArray', function() {
|
||||||
|
ok(!_.isArray(arguments), 'arguments object is not an array');
|
||||||
|
ok(_.isArray(_.toArray(arguments)), 'arguments object converted into array');
|
||||||
|
var numbers = _.toArray({one : 1, two : 2, three : 3});
|
||||||
|
equals(_.pluck(numbers, '0').join(', '), 'one, two, three', 'object flattened into array');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('collections: size', function() {
|
||||||
|
equals(_.size({one : 1, two : 2, three : 3}), 3, 'can compute the size of an object');
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
@@ -1,29 +0,0 @@
|
|||||||
$(document).ready(function(){
|
|
||||||
|
|
||||||
test("a basic test example", function() {
|
|
||||||
ok( true, "this test is fine" );
|
|
||||||
var value = "hello";
|
|
||||||
equals( "hello", value, "We expect value to be hello" );
|
|
||||||
});
|
|
||||||
|
|
||||||
module("Module A");
|
|
||||||
|
|
||||||
test("first test within module", function() {
|
|
||||||
ok( true, "all pass" );
|
|
||||||
});
|
|
||||||
|
|
||||||
test("second test within module", function() {
|
|
||||||
ok( true, "all pass" );
|
|
||||||
});
|
|
||||||
|
|
||||||
module("Module B");
|
|
||||||
|
|
||||||
test("some other test", function() {
|
|
||||||
expect(2);
|
|
||||||
equals( true, false, "failing test" );
|
|
||||||
equals( true, true, "passing test" );
|
|
||||||
});
|
|
||||||
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
25
test/functions.js
Normal file
25
test/functions.js
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
$(document).ready(function() {
|
||||||
|
|
||||||
|
module("Function functions (bind, bindAll, and so on...)");
|
||||||
|
|
||||||
|
test("functions: bind", function() {
|
||||||
|
var context = {name : 'moe'};
|
||||||
|
var func = function() { return "name: " + this.name; };
|
||||||
|
var bound = _.bind(func, context);
|
||||||
|
equals(bound(), 'name: moe', 'can bind a function to a context');
|
||||||
|
});
|
||||||
|
|
||||||
|
test("functions: bindAll", function() {
|
||||||
|
var curly = {name : 'curly'}, moe = {
|
||||||
|
name : 'moe',
|
||||||
|
getName : function() { return 'name: ' + this.name; },
|
||||||
|
sayHi : function() { return 'hi: ' + this.name; }
|
||||||
|
};
|
||||||
|
curly.getName = moe.getName;
|
||||||
|
_.bindAll('getName', 'sayHi', moe);
|
||||||
|
curly.sayHi = moe.sayHi;
|
||||||
|
equals(curly.getName(), 'name: curly', 'unbound function is bound to current object');
|
||||||
|
equals(curly.sayHi(), 'hi: moe', 'bound function is still bound to original object');
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
64
test/objects.js
Normal file
64
test/objects.js
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
$(document).ready(function() {
|
||||||
|
|
||||||
|
module("Object functions (values, extend, isEqual, and so on...)");
|
||||||
|
|
||||||
|
test("objects: keys", function() {
|
||||||
|
equals(_.keys({one : 1, two : 2}).join(', '), 'one, two', 'can extract the keys from an object');
|
||||||
|
});
|
||||||
|
|
||||||
|
test("objects: values", function() {
|
||||||
|
equals(_.values({one : 1, two : 2}).join(', '), '1, 2', 'can extract the values from an object');
|
||||||
|
});
|
||||||
|
|
||||||
|
test("objects: extend", function() {
|
||||||
|
var source = {name : 'moe'}, dest = {age : 50};
|
||||||
|
_.extend(dest, source);
|
||||||
|
equals(dest.name, 'moe', 'can extend an object with the attributes of another');
|
||||||
|
});
|
||||||
|
|
||||||
|
test("objects: clone", function() {
|
||||||
|
var moe = {name : 'moe', lucky : [13, 27, 34]};
|
||||||
|
var clone = _.clone(moe);
|
||||||
|
equals(clone.name, 'moe', 'the clone as the attributes of the original');
|
||||||
|
clone.name = 'curly';
|
||||||
|
ok(clone.name == 'curly' && moe.name == 'moe', 'clones can change shallow attributes without affecting the original');
|
||||||
|
clone.lucky.push(101);
|
||||||
|
equals(_.last(moe.lucky), 101, 'changes to deep attributes are shared with the original');
|
||||||
|
});
|
||||||
|
|
||||||
|
test("objects: isEqual", function() {
|
||||||
|
var moe = {name : 'moe', lucky : [13, 27, 34]};
|
||||||
|
var clone = {name : 'moe', lucky : [13, 27, 34]};
|
||||||
|
ok(moe != clone, 'basic equality between objects is false');
|
||||||
|
ok(_.isEqual(moe, clone), 'deep equality is true');
|
||||||
|
});
|
||||||
|
|
||||||
|
test("objects: isElement", function() {
|
||||||
|
ok(!_.isElement('div'), 'strings are not dom elements');
|
||||||
|
ok(_.isElement($('html')[0]), 'the html tag is a DOM element');
|
||||||
|
});
|
||||||
|
|
||||||
|
test("objects: isArray", function() {
|
||||||
|
ok(!_.isArray(arguments), 'the arguments object is not an array');
|
||||||
|
ok(_.isArray([1, 2, 3]), 'but arrays are');
|
||||||
|
});
|
||||||
|
|
||||||
|
test("objects: isFunction", function() {
|
||||||
|
ok(!_.isFunction([1, 2, 3]), 'arrays are not functions');
|
||||||
|
ok(!_.isFunction('moe'), 'strings are not functions');
|
||||||
|
ok(_.isFunction(_.isFunction), 'but functions are');
|
||||||
|
});
|
||||||
|
|
||||||
|
test("objects: isUndefined", function() {
|
||||||
|
ok(!_.isUndefined(1), 'numbers are defined');
|
||||||
|
ok(!_.isUndefined(null), 'null is defined');
|
||||||
|
ok(!_.isUndefined(false), 'false is defined');
|
||||||
|
ok(_.isUndefined(), 'nothing is undefined');
|
||||||
|
ok(_.isUndefined(undefined), 'undefined is undefined');
|
||||||
|
});
|
||||||
|
|
||||||
|
test("objects: toString", function() {
|
||||||
|
equals(_.toString([1, 2, 3]), '1,2,3', 'object can be converted to printable strings');
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
@@ -6,7 +6,11 @@
|
|||||||
<script type="text/javascript" src="vendor/jquery.js"></script>
|
<script type="text/javascript" src="vendor/jquery.js"></script>
|
||||||
<script type="text/javascript" src="vendor/qunit.js"></script>
|
<script type="text/javascript" src="vendor/qunit.js"></script>
|
||||||
<script type="text/javascript" src="../underscore.js"></script>
|
<script type="text/javascript" src="../underscore.js"></script>
|
||||||
<script type="text/javascript" src="test.js"></script>
|
<script type="text/javascript" src="collections.js"></script>
|
||||||
|
<script type="text/javascript" src="arrays.js"></script>
|
||||||
|
<script type="text/javascript" src="functions.js"></script>
|
||||||
|
<script type="text/javascript" src="objects.js"></script>
|
||||||
|
<script type="text/javascript" src="utility.js"></script>
|
||||||
|
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
|
|||||||
59
test/test.js
59
test/test.js
@@ -1,59 +0,0 @@
|
|||||||
$(document).ready(function() {
|
|
||||||
|
|
||||||
module("Collection functions (each, any, select, and so on...)");
|
|
||||||
|
|
||||||
test("collections: _.each", function() {
|
|
||||||
_.each(numbers, function(num, i){
|
|
||||||
equals(num, i + 1, 'each iterators provide value and iteration count');
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
test('collections: _.each and throwing "__break__"', function() {
|
|
||||||
var answer = null;
|
|
||||||
_.each(numbers, function(num){
|
|
||||||
if ((answer = num) == 2) throw '__break__';
|
|
||||||
});
|
|
||||||
equals(2, answer, 'the loop broke in the middle');
|
|
||||||
});
|
|
||||||
|
|
||||||
test('collections: _.each can take a context object', function() {
|
|
||||||
var answers = [];
|
|
||||||
_.each(numbers, function(num) {
|
|
||||||
answers.push(num * this.multiplier);
|
|
||||||
}, {multiplier : 5});
|
|
||||||
equals('5, 10, 15', answers.join(', '), 'context object property accessed');
|
|
||||||
});
|
|
||||||
|
|
||||||
test('collections: _.all', function() {
|
|
||||||
ok(_.all([]), 'the empty set');
|
|
||||||
ok(_.all([true, true, true]), 'all true values');
|
|
||||||
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');
|
|
||||||
});
|
|
||||||
|
|
||||||
test('collections: _.any', function() {
|
|
||||||
ok(!_.any([]), 'the empty set');
|
|
||||||
ok(!_.any([false, false, false]), 'all false values');
|
|
||||||
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');
|
|
||||||
});
|
|
||||||
|
|
||||||
test('collections: _.map', function() {
|
|
||||||
var doubled = _.map(numbers, function(num){ return num * 2; });
|
|
||||||
equals('2, 4, 6', doubled.join(', '), 'doubled numbers');
|
|
||||||
var tripled = _.map(numbers, function(num){ return num * this.multiplier; }, {multiplier : 3});
|
|
||||||
equals('3, 6, 9', tripled.join(', '), 'tripled numbers with context');
|
|
||||||
});
|
|
||||||
|
|
||||||
test('collections: _.detect', function() {
|
|
||||||
var result = _.detect(numbers, function(num){ return num * 2 == 4; });
|
|
||||||
equals(2, result, 'found the first "2" and broke the loop');
|
|
||||||
});
|
|
||||||
|
|
||||||
test('collections: _.select', function() {
|
|
||||||
var evens = _.select(function([1,2,3,4,5,6], ))
|
|
||||||
});
|
|
||||||
|
|
||||||
});
|
|
||||||
20
test/utility.js
Normal file
20
test/utility.js
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
$(document).ready(function() {
|
||||||
|
|
||||||
|
module("Utility functions (uniqueId, template)");
|
||||||
|
|
||||||
|
test("utility: uniqueId", function() {
|
||||||
|
var ids = [], i = 0;
|
||||||
|
while(i++ < 100) ids.push(_.uniqueId());
|
||||||
|
equals(_.uniq(ids).length, ids.length, 'can generate a globally-unique stream of ids');
|
||||||
|
});
|
||||||
|
|
||||||
|
test("utility: template", function() {
|
||||||
|
var basicTemplate = _.template("<%= thing %> is gettin' on my noives!");
|
||||||
|
var result = basicTemplate({thing : 'This'});
|
||||||
|
equals(result, "This is gettin' on my noives!", 'can do basic attribute interpolation');
|
||||||
|
var fancyTemplate = _.template("<% for (key in people) { %><li><%= people[key] %></li><% } %>");
|
||||||
|
result = fancyTemplate({people : {moe : "Moe", larry : "Larry", curly : "Curly"}});
|
||||||
|
equals(result, "<li>Moe</li><li>Larry</li><li>Curly</li>", 'can run arbitrary javascript in templates');
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
202
underscore.js
202
underscore.js
@@ -10,13 +10,9 @@ window._ = {
|
|||||||
if (obj.forEach) {
|
if (obj.forEach) {
|
||||||
obj.forEach(iterator, context);
|
obj.forEach(iterator, context);
|
||||||
} else if (obj.length) {
|
} else if (obj.length) {
|
||||||
for (var i=0; i<obj.length; i++) {
|
for (var i=0; i<obj.length; i++) iterator.call(context, obj[i], i);
|
||||||
iterator.call(context, obj[i], i);
|
} else if (obj.each) {
|
||||||
}
|
obj.each(function(value) { iterator.call(context, value, index++); });
|
||||||
} else if (obj._each) {
|
|
||||||
obj._each(function(value) {
|
|
||||||
iterator.call(context, value, index++);
|
|
||||||
});
|
|
||||||
} else {
|
} else {
|
||||||
var i = 0;
|
var i = 0;
|
||||||
for (var key in obj) {
|
for (var key in obj) {
|
||||||
@@ -68,6 +64,14 @@ window._ = {
|
|||||||
return results;
|
return results;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
// Aka reduce. Inject builds up a single result from a list of values.
|
||||||
|
inject : function(obj, memo, iterator, context) {
|
||||||
|
_.each(obj, function(value, index) {
|
||||||
|
memo = iterator.call(context, memo, value, index);
|
||||||
|
});
|
||||||
|
return memo;
|
||||||
|
},
|
||||||
|
|
||||||
// Return the first value which passes a truth test.
|
// Return the first value which passes a truth test.
|
||||||
detect : function(obj, iterator, context) {
|
detect : function(obj, iterator, context) {
|
||||||
var result;
|
var result;
|
||||||
@@ -91,12 +95,22 @@ window._ = {
|
|||||||
return results;
|
return results;
|
||||||
},
|
},
|
||||||
|
|
||||||
// Determine if a given value is included in the object, based on '=='.
|
// Return all the elements for which a truth test fails.
|
||||||
|
reject : function(obj, iterator, context) {
|
||||||
|
var results = [];
|
||||||
|
_.each(obj, function(value, index) {
|
||||||
|
if (!iterator.call(context, value, index)) results.push(value);
|
||||||
|
});
|
||||||
|
return results;
|
||||||
|
},
|
||||||
|
|
||||||
|
// Determine if a given value is included in the array or object,
|
||||||
|
// based on '=='.
|
||||||
include : function(obj, target) {
|
include : function(obj, target) {
|
||||||
if (_.isArray(obj)) if (obj.indexOf(target) != -1) return true;
|
if (_.isArray(obj)) return _.indexOf(obj, target) != -1;
|
||||||
var found = false;
|
var found = false;
|
||||||
_.each(obj, function(value) {
|
_.each(obj, function(pair) {
|
||||||
if (value == target) {
|
if (pair.value == target) {
|
||||||
found = true;
|
found = true;
|
||||||
throw '__break__';
|
throw '__break__';
|
||||||
}
|
}
|
||||||
@@ -104,14 +118,6 @@ window._ = {
|
|||||||
return found;
|
return found;
|
||||||
},
|
},
|
||||||
|
|
||||||
// Aka reduce. Inject builds up a single result from a list of values.
|
|
||||||
inject : function(obj, memo, iterator, context) {
|
|
||||||
_.each(obj, function(value, index) {
|
|
||||||
memo = iterator.call(context, memo, value, index);
|
|
||||||
});
|
|
||||||
return memo;
|
|
||||||
},
|
|
||||||
|
|
||||||
// Invoke a method with arguments on every item in a collection.
|
// Invoke a method with arguments on every item in a collection.
|
||||||
invoke : function(obj, method) {
|
invoke : function(obj, method) {
|
||||||
var args = _.toArray(arguments).slice(2);
|
var args = _.toArray(arguments).slice(2);
|
||||||
@@ -120,26 +126,6 @@ window._ = {
|
|||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
// Return the maximum item or (item-based computation).
|
|
||||||
max : function(obj, iterator, context) {
|
|
||||||
var result;
|
|
||||||
_.each(obj, function(value, index) {
|
|
||||||
value = iterator ? iterator.call(context, value, index) : value;
|
|
||||||
if (result == null || value >= result) result = value;
|
|
||||||
});
|
|
||||||
return result;
|
|
||||||
},
|
|
||||||
|
|
||||||
// Return the minimum element (or element-based computation).
|
|
||||||
min : function(obj, iterator, context) {
|
|
||||||
var result;
|
|
||||||
_.each(obj, function(value, index) {
|
|
||||||
value = iterator ? iterator.call(context, value, index) : value;
|
|
||||||
if (result == null || value < result) result = value;
|
|
||||||
});
|
|
||||||
return result;
|
|
||||||
},
|
|
||||||
|
|
||||||
// Optimized version of a common use case of map: fetching a property.
|
// Optimized version of a common use case of map: fetching a property.
|
||||||
pluck : function(obj, key) {
|
pluck : function(obj, key) {
|
||||||
var results = [];
|
var results = [];
|
||||||
@@ -147,13 +133,26 @@ window._ = {
|
|||||||
return results;
|
return results;
|
||||||
},
|
},
|
||||||
|
|
||||||
// Return all the elements for which a truth test fails.
|
// Return the maximum item or (item-based computation).
|
||||||
reject : function(obj, iterator, context) {
|
max : function(obj, iterator, context) {
|
||||||
var results = [];
|
if (!iterator && _.isArray(obj)) return Math.max.apply(Math, obj);
|
||||||
|
var result;
|
||||||
_.each(obj, function(value, index) {
|
_.each(obj, function(value, index) {
|
||||||
if (!iterator.call(context, value, index)) results.push(value);
|
var computed = iterator ? iterator.call(context, value, index) : value;
|
||||||
|
if (result == null || computed >= result.computed) result = {value : value, computed : computed};
|
||||||
});
|
});
|
||||||
return results;
|
return result.value;
|
||||||
|
},
|
||||||
|
|
||||||
|
// Return the minimum element (or element-based computation).
|
||||||
|
min : function(obj, iterator, context) {
|
||||||
|
if (!iterator && _.isArray(obj)) return Math.min.apply(Math, obj);
|
||||||
|
var result;
|
||||||
|
_.each(obj, function(value, index) {
|
||||||
|
var computed = iterator ? iterator.call(context, value, index) : value;
|
||||||
|
if (result == null || computed < result.computed) result = {value : value, computed : computed};
|
||||||
|
});
|
||||||
|
return result.value;
|
||||||
},
|
},
|
||||||
|
|
||||||
// Sort the object's values by a criteria produced by an iterator.
|
// Sort the object's values by a criteria produced by an iterator.
|
||||||
@@ -189,7 +188,7 @@ window._ = {
|
|||||||
|
|
||||||
// Return the number of elements in an object.
|
// Return the number of elements in an object.
|
||||||
size : function(obj) {
|
size : function(obj) {
|
||||||
_.toArray(obj).length;
|
return _.toArray(obj).length;
|
||||||
},
|
},
|
||||||
|
|
||||||
//------------- The following methods only apply to arrays. -----------------
|
//------------- The following methods only apply to arrays. -----------------
|
||||||
@@ -218,10 +217,10 @@ window._ = {
|
|||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
// Return a version of the array that does not contain the specified value.
|
// Return a version of the array that does not contain the specified value(s).
|
||||||
without : function(array) {
|
without : function(array) {
|
||||||
var values = array.slice.call(arguments, 0);
|
var values = array.slice.call(arguments, 0);
|
||||||
return _.select(function(value){ return !_.include(values, value); });
|
return _.select(array, function(value){ return !_.include(values, value); });
|
||||||
},
|
},
|
||||||
|
|
||||||
// Produce a duplicate-free version of the array. If the array has already
|
// Produce a duplicate-free version of the array. If the array has already
|
||||||
@@ -240,14 +239,38 @@ window._ = {
|
|||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
// If the browser doesn't supply us with indexOf, we might need this function.
|
// If the browser doesn't supply us with indexOf (I'm looking at you, MSIE),
|
||||||
// Return the position of the first occurence of an item in an array,
|
// we need this function. Return the position of the first occurence of an
|
||||||
// or -1 if the item is not included in the array.
|
// item in an array, or -1 if the item is not included in the array.
|
||||||
// indexOf : function(array, item) {
|
indexOf : function(array, item) {
|
||||||
// var length = array.length;
|
if (array.indexOf) return array.indexOf(item);
|
||||||
// for (i=0; i<length; i++) if (array[i] === item) return i;
|
var length = array.length;
|
||||||
// return -1;
|
for (i=0; i<length; i++) if (array[i] === item) return i;
|
||||||
// }
|
return -1;
|
||||||
|
},
|
||||||
|
|
||||||
|
/* -------------- The following methods apply to functions -----------------*/
|
||||||
|
|
||||||
|
// Create a function bound to a given object (assigning 'this', and arguments,
|
||||||
|
// optionally).
|
||||||
|
bind : function(func, context) {
|
||||||
|
if (!context) return func;
|
||||||
|
var args = _.toArray(arguments).slice(2);
|
||||||
|
return function() {
|
||||||
|
var a = args.concat(_.toArray(arguments));
|
||||||
|
return func.apply(context, a);
|
||||||
|
};
|
||||||
|
},
|
||||||
|
|
||||||
|
// Bind all of an object's methods to that object. Useful for ensuring that
|
||||||
|
// all callbacks defined on an object belong to it.
|
||||||
|
bindAll : function() {
|
||||||
|
var args = _.toArray(arguments);
|
||||||
|
var context = args.pop();
|
||||||
|
_.each(args, function(methodName) {
|
||||||
|
context[methodName] = _.bind(context[methodName], context);
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
/* ---------------- The following methods apply to objects ---------------- */
|
/* ---------------- The following methods apply to objects ---------------- */
|
||||||
|
|
||||||
@@ -272,6 +295,30 @@ window._ = {
|
|||||||
return _.extend({}, obj);
|
return _.extend({}, obj);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
// Perform a deep comparison to check if two objects are equal.
|
||||||
|
isEqual : function(a, b) {
|
||||||
|
// Check object identity.
|
||||||
|
if (a === b) return true;
|
||||||
|
// Different types?
|
||||||
|
var atype = typeof(a), btype = typeof(b);
|
||||||
|
if (atype != btype) return false;
|
||||||
|
// Basic equality test (watch out for coercions).
|
||||||
|
if (a == b) return true;
|
||||||
|
// One of them implements an isEqual()?
|
||||||
|
if (a.isEqual) return a.isEqual(b);
|
||||||
|
// Nothing else worked, deep compare the contents.
|
||||||
|
return atype === 'object' && _._isEqualContents(a, b);
|
||||||
|
},
|
||||||
|
|
||||||
|
// Objects have equal contents if they have the same keys, and all the values
|
||||||
|
// are equal (as defined by _.isEqual).
|
||||||
|
_isEqualContents : function(a, b) {
|
||||||
|
var aKeys = _.keys(a), bKeys = _.keys(b);
|
||||||
|
if (aKeys.length != bKeys.length) return false;
|
||||||
|
for (var key in a) if (!_.isEqual(a[key], b[key])) return false;
|
||||||
|
return true;
|
||||||
|
},
|
||||||
|
|
||||||
// Is a given value a DOM element?
|
// Is a given value a DOM element?
|
||||||
isElement : function(obj) {
|
isElement : function(obj) {
|
||||||
return !!(obj && obj.nodeType == 1);
|
return !!(obj && obj.nodeType == 1);
|
||||||
@@ -297,26 +344,7 @@ window._ = {
|
|||||||
return obj == null ? '' : String(obj);
|
return obj == null ? '' : String(obj);
|
||||||
},
|
},
|
||||||
|
|
||||||
// Create a function bound to a given object (assigning 'this', and arguments,
|
/* -------------- The following methods are utility methods --------------- */
|
||||||
// optionally).
|
|
||||||
bind : function(func, context) {
|
|
||||||
if (!context) return func;
|
|
||||||
var args = _.toArray(arguments).slice(2);
|
|
||||||
return function() {
|
|
||||||
var a = args.concat(_.toArray(arguments));
|
|
||||||
return func.apply(context, a);
|
|
||||||
};
|
|
||||||
},
|
|
||||||
|
|
||||||
// Bind all of an object's methods to that object. Useful for ensuring that
|
|
||||||
// all callbacks defined on an object belong to it.
|
|
||||||
bindAll : function() {
|
|
||||||
var args = _.toArray(arguments);
|
|
||||||
var context = args.pop();
|
|
||||||
_.each(args, function(methodName) {
|
|
||||||
context[methodName] = _.bind(context[methodName], context);
|
|
||||||
});
|
|
||||||
},
|
|
||||||
|
|
||||||
// Generate a unique integer id (unique within the entire client session).
|
// Generate a unique integer id (unique within the entire client session).
|
||||||
// Useful for temporary DOM ids.
|
// Useful for temporary DOM ids.
|
||||||
@@ -325,30 +353,6 @@ window._ = {
|
|||||||
return prefix ? prefix + id : id;
|
return prefix ? prefix + id : id;
|
||||||
},
|
},
|
||||||
|
|
||||||
// Perform a deep comparison to check if two objects are equal.
|
|
||||||
isEqual : function(a, b) {
|
|
||||||
// Check object identity.
|
|
||||||
if (a === b) return true;
|
|
||||||
// Different types?
|
|
||||||
var at = typeof(a), bt = typeof(b);
|
|
||||||
if (at != bt) return false;
|
|
||||||
// Basic equality test (watch out for coercions).
|
|
||||||
if (a == b) return true;
|
|
||||||
// One of them implements an isEqual()?
|
|
||||||
if (a.isEqual) return a.isEqual(b);
|
|
||||||
// Nothing else worked, deep compare the contents.
|
|
||||||
return at === 'object' && _.isEqualContents(a, b);
|
|
||||||
},
|
|
||||||
|
|
||||||
// Objects have equal contents if they have the same keys, and all the values
|
|
||||||
// are equal (as defined by _.isEqual).
|
|
||||||
_isEqualContents : function(a, b) {
|
|
||||||
var aKeys = _.keys(a), bKeys = _.keys(b);
|
|
||||||
if (aKeys.length != bKeys.length) return false;
|
|
||||||
for (var key in a) if (!_.isEqual(a[key], b[key])) return false;
|
|
||||||
return true;
|
|
||||||
},
|
|
||||||
|
|
||||||
// Javascript templating a-la ERB, pilfered from John Resig's
|
// Javascript templating a-la ERB, pilfered from John Resig's
|
||||||
// "Secrets of the Javascript Ninja", page 83.
|
// "Secrets of the Javascript Ninja", page 83.
|
||||||
template : function(str, data) {
|
template : function(str, data) {
|
||||||
|
|||||||
Reference in New Issue
Block a user