Update vendor/underscore and backbone.

Former-commit-id: 614926653b0b1669f8a5533666adb0aecbd46c03
This commit is contained in:
John-David Dalton
2012-07-19 01:32:47 -04:00
parent 9848ffb77f
commit d43ede3a11
3 changed files with 62 additions and 37 deletions

View File

@@ -253,6 +253,18 @@ $(document).ready(function() {
equal(grouped['5'].join(' '), 'three seven eight');
});
test('collections: countBy', function() {
var parity = _.countBy([1, 2, 3, 4, 5], function(num){ return num % 2 == 0; });
equal(parity['true'], 2);
equal(parity['false'], 3);
var list = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"];
var grouped = _.countBy(list, 'length');
equal(grouped['3'], 4);
equal(grouped['4'], 3);
equal(grouped['5'], 3);
});
test('collections: sortedIndex', function() {
var numbers = [10, 20, 30, 40, 50], num = 35;
var indexForNum = _.sortedIndex(numbers, num);

View File

@@ -267,7 +267,7 @@
// Sort the object's values by a criterion produced by an iterator.
_.sortBy = function(obj, val, context) {
var iterator = _.isFunction(val) ? val : function(obj) { return obj[val]; };
var iterator = lookupIterator(obj, val);
return _.pluck(_.map(obj, function(value, index, list) {
return {
value : value,
@@ -281,16 +281,38 @@
}), 'value');
};
// An internal function to generate lookup iterators.
var lookupIterator = function(obj, val) {
return _.isFunction(val) ? val : function(obj) { return obj[val]; };
};
// An internal function used for aggregate "group by" operations.
var group = function(obj, val, behavior) {
var result = {};
var iterator = lookupIterator(obj, val);
each(obj, function(value, index) {
var key = iterator(value, index);
behavior(result, key, value);
});
return result;
};
// Groups the object's values by a criterion. Pass either a string attribute
// to group by, or a function that returns the criterion.
_.groupBy = function(obj, val) {
var result = {};
var iterator = _.isFunction(val) ? val : function(obj) { return obj[val]; };
each(obj, function(value, index) {
var key = iterator(value, index);
return group(obj, val, function(result, key, value) {
(result[key] || (result[key] = [])).push(value);
});
return result;
};
// Counts instances of an object that group by a certain criterion. Pass
// either a string attribute to count by, or a function that returns the
// criterion.
_.countBy = function(obj, val) {
return group(obj, val, function(result, key, value) {
result[key] || (result[key] = 0);
result[key]++;
});
};
// Use a comparator function to figure out the smallest index at which
@@ -709,7 +731,7 @@
};
// Internal recursive comparison function for `isEqual`.
function eq(a, b, stack) {
var eq = function(a, b, stack) {
// Identical objects are equal. `0 === -0`, but they aren't identical.
// See the Harmony `egal` proposal: http://wiki.ecmascript.org/doku.php?id=harmony:egal.
if (a === b) return a !== 0 || 1 / a == 1 / b;
@@ -794,7 +816,7 @@
// Remove the first object from the stack of traversed objects.
stack.pop();
return result;
}
};
// Perform a deep comparison to check if two objects are equal.
_.isEqual = function(a, b) {
@@ -832,7 +854,7 @@
return toString.call(obj) == '[object ' + name + ']';
};
});
// Define a fallback version of the method in browsers (ahem, IE), where
// there isn't any inspectable "Arguments" type.
if (!_.isArguments(arguments)) {