Update vendor/underscore to 1.5.2.

This commit is contained in:
John-David Dalton
2013-09-07 14:38:53 -05:00
parent e078f584eb
commit eeabb47fb6
7 changed files with 143 additions and 76 deletions

View File

@@ -1,5 +1,5 @@
/** vim: et:ts=4:sw=4:sts=4 /** vim: et:ts=4:sw=4:sts=4
* @license RequireJS 2.1.8+ Copyright (c) 2010-2012, The Dojo Foundation All Rights Reserved. * @license RequireJS 2.1.8 Copyright (c) 2010-2012, The Dojo Foundation All Rights Reserved.
* Available via the MIT or new BSD license. * Available via the MIT or new BSD license.
* see: http://github.com/jrburke/requirejs for details * see: http://github.com/jrburke/requirejs for details
*/ */
@@ -12,7 +12,7 @@ var requirejs, require, define;
(function (global) { (function (global) {
var req, s, head, baseElement, dataMain, src, var req, s, head, baseElement, dataMain, src,
interactiveScript, currentlyAddingScript, mainScript, subPath, interactiveScript, currentlyAddingScript, mainScript, subPath,
version = '2.1.8+', version = '2.1.8',
commentRegExp = /(\/\*([\s\S]*?)\*\/|([^:]|^)\/\/(.*)$)/mg, commentRegExp = /(\/\*([\s\S]*?)\*\/|([^:]|^)\/\/(.*)$)/mg,
cjsRequireRegExp = /[^.]\s*require\s*\(\s*["']([^'"\s]+)["']\s*\)/g, cjsRequireRegExp = /[^.]\s*require\s*\(\s*["']([^'"\s]+)["']\s*\)/g,
jsSuffixRegExp = /\.js$/, jsSuffixRegExp = /\.js$/,
@@ -1609,7 +1609,7 @@ var requirejs, require, define;
//Join the path parts together, then figure out if baseUrl is needed. //Join the path parts together, then figure out if baseUrl is needed.
url = syms.join('/'); url = syms.join('/');
url += (ext || (/^data\:|\?/.test(url) || skipExt ? '' : '.js')); url += (ext || (/\?/.test(url) || skipExt ? '' : '.js'));
url = (url.charAt(0) === '/' || url.match(/^[\w\+\.\-]+:/) ? '' : config.baseUrl) + url; url = (url.charAt(0) === '/' || url.match(/^[\w\+\.\-]+:/) ? '' : config.baseUrl) + url;
} }

View File

@@ -274,6 +274,12 @@ $(document).ready(function() {
deepEqual(result, {a: 1, b: 2}); deepEqual(result, {a: 1, b: 2});
result = _.findWhere(list, {b: 4}); result = _.findWhere(list, {b: 4});
deepEqual(result, {a: 1, b: 4}); deepEqual(result, {a: 1, b: 4});
result = _.findWhere(list, {c:1})
ok(_.isUndefined(result), "undefined when not found");
result = _.findWhere([], {c:1});
ok(_.isUndefined(result), "undefined when searching empty list");
}); });
test('max', function() { test('max', function() {
@@ -379,6 +385,24 @@ $(document).ready(function() {
deepEqual(_.groupBy(matrix, 1), {2: [[1,2]], 3: [[1,3], [2,3]]}) deepEqual(_.groupBy(matrix, 1), {2: [[1,2]], 3: [[1,3], [2,3]]})
}); });
test('indexBy', function() {
var parity = _.indexBy([1, 2, 3, 4, 5], function(num){ return num % 2 == 0; });
equal(parity['true'], 4);
equal(parity['false'], 5);
var list = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"];
var grouped = _.indexBy(list, 'length');
equal(grouped['3'], 'ten');
equal(grouped['4'], 'nine');
equal(grouped['5'], 'eight');
var array = [1, 2, 1, 2, 3];
var grouped = _.indexBy(array);
equal(grouped['1'], 1);
equal(grouped['2'], 2);
equal(grouped['3'], 3);
});
test('countBy', function() { test('countBy', function() {
var parity = _.countBy([1, 2, 3, 4, 5], function(num){ return num % 2 == 0; }); var parity = _.countBy([1, 2, 3, 4, 5], function(num){ return num % 2 == 0; });
equal(parity['true'], 2); equal(parity['true'], 2);
@@ -433,6 +457,19 @@ $(document).ready(function() {
equal(shuffled.join(','), numbers.join(','), 'contains the same members before and after shuffle'); equal(shuffled.join(','), numbers.join(','), 'contains the same members before and after shuffle');
}); });
test('sample', function() {
var numbers = _.range(10);
var all_sampled = _.sample(numbers, 10).sort();
equal(all_sampled.join(','), numbers.join(','), 'contains the same members before and after sample');
all_sampled = _.sample(numbers, 20).sort();
equal(all_sampled.join(','), numbers.join(','), 'also works when sampling more objects than are present');
ok(_.contains(numbers, _.sample(numbers)), 'sampling a single element returns something from the array');
strictEqual(_.sample([]), undefined, 'sampling empty array with no number returns undefined');
notStrictEqual(_.sample([], 5), [], 'sampling empty array with a number returns an empty array');
notStrictEqual(_.sample([1, 2, 3], 0), [], 'sampling an array with 0 picks returns an empty array');
deepEqual(_.sample([1, 2], -1), [], 'sampling a negative number of picks returns an empty array');
});
test('toArray', function() { test('toArray', function() {
ok(!_.isArray(arguments), 'arguments object is not an array'); ok(!_.isArray(arguments), 'arguments object is not an array');
ok(_.isArray(_.toArray(arguments)), 'arguments object converted into array'); ok(_.isArray(_.toArray(arguments)), 'arguments object converted into array');

View File

@@ -159,14 +159,14 @@ $(document).ready(function() {
asyncTest("throttle repeatedly with results", 6, function() { asyncTest("throttle repeatedly with results", 6, function() {
var counter = 0; var counter = 0;
var incr = function(){ return ++counter; }; var incr = function(){ return ++counter; };
var throttledIncr = _.throttle(incr, 64); var throttledIncr = _.throttle(incr, 100);
var results = []; var results = [];
var saveResult = function() { results.push(throttledIncr()); }; var saveResult = function() { results.push(throttledIncr()); };
saveResult(); saveResult(); saveResult(); saveResult();
_.delay(saveResult, 32); _.delay(saveResult, 50);
_.delay(saveResult, 80); _.delay(saveResult, 150);
_.delay(saveResult, 96); _.delay(saveResult, 160);
_.delay(saveResult, 144); _.delay(saveResult, 230);
_.delay(function() { _.delay(function() {
equal(results[0], 1, "incr was called once"); equal(results[0], 1, "incr was called once");
equal(results[1], 1, "incr was throttled"); equal(results[1], 1, "incr was throttled");
@@ -175,7 +175,7 @@ $(document).ready(function() {
equal(results[4], 2, "incr was throttled"); equal(results[4], 2, "incr was throttled");
equal(results[5], 3, "incr was called trailing"); equal(results[5], 3, "incr was called trailing");
start(); start();
}, 192); }, 300);
}); });
asyncTest("throttle triggers trailing call when invoked repeatedly", 2, function() { asyncTest("throttle triggers trailing call when invoked repeatedly", 2, function() {
@@ -239,10 +239,10 @@ $(document).ready(function() {
var time = new Date; var time = new Date;
while (new Date - time < 350) throttledIncr(); while (new Date - time < 350) throttledIncr();
ok(counter === 3); ok(counter <= 3);
_.delay(function() { _.delay(function() {
equal(counter, 4); ok(counter <= 4);
start(); start();
}, 200); }, 200);
}); });

View File

@@ -52,7 +52,7 @@ $(document).ready(function() {
result = _.extend({x:'x'}, {a:'a', x:2}, {a:'b'}); result = _.extend({x:'x'}, {a:'a', x:2}, {a:'b'});
ok(_.isEqual(result, {x:2, a:'b'}), 'extending from multiple source objects last property trumps'); ok(_.isEqual(result, {x:2, a:'b'}), 'extending from multiple source objects last property trumps');
result = _.extend({}, {a: void 0, b: null}); result = _.extend({}, {a: void 0, b: null});
equal(_.keys(result).join(''), 'ab', 'extend does not copy undefined values'); equal(_.keys(result).join(''), 'ab', 'extend copies undefined values');
try { try {
result = {}; result = {};

View File

@@ -73,7 +73,7 @@ $(document).ready(function() {
test("_.escape", function() { test("_.escape", function() {
equal(_.escape("Curly & Moe"), "Curly &amp; Moe"); equal(_.escape("Curly & Moe"), "Curly &amp; Moe");
equal(_.escape('<a href="http://moe.com">Curly & Moe\'s</a>'), '&lt;a href=&quot;http:&#x2F;&#x2F;moe.com&quot;&gt;Curly &amp; Moe&#x27;s&lt;&#x2F;a&gt;'); equal(_.escape('<a href="http://moe.com">Curly & Moe\'s</a>'), '&lt;a href=&quot;http://moe.com&quot;&gt;Curly &amp; Moe&#x27;s&lt;/a&gt;');
equal(_.escape("Curly &amp; Moe"), "Curly &amp;amp; Moe"); equal(_.escape("Curly &amp; Moe"), "Curly &amp;amp; Moe");
equal(_.escape(null), ''); equal(_.escape(null), '');
}); });
@@ -81,7 +81,7 @@ $(document).ready(function() {
test("_.unescape", function() { test("_.unescape", function() {
var string = "Curly & Moe"; var string = "Curly & Moe";
equal(_.unescape("Curly &amp; Moe"), string); equal(_.unescape("Curly &amp; Moe"), string);
equal(_.unescape('&lt;a href=&quot;http:&#x2F;&#x2F;moe.com&quot;&gt;Curly &amp; Moe&#x27;s&lt;&#x2F;a&gt;'), '<a href="http://moe.com">Curly & Moe\'s</a>'); equal(_.unescape('&lt;a href=&quot;http://moe.com&quot;&gt;Curly &amp; Moe&#x27;s&lt;/a&gt;'), '<a href="http://moe.com">Curly & Moe\'s</a>');
equal(_.unescape("Curly &amp;amp; Moe"), "Curly &amp; Moe"); equal(_.unescape("Curly &amp;amp; Moe"), "Curly &amp; Moe");
equal(_.unescape(null), ''); equal(_.unescape(null), '');
equal(_.unescape(_.escape(string)), string); equal(_.unescape(_.escape(string)), string);

File diff suppressed because one or more lines are too long

View File

@@ -1,4 +1,4 @@
// Underscore.js 1.5.1 // Underscore.js 1.5.2
// http://underscorejs.org // http://underscorejs.org
// (c) 2009-2013 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors // (c) 2009-2013 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors
// Underscore may be freely distributed under the MIT license. // Underscore may be freely distributed under the MIT license.
@@ -8,7 +8,7 @@
// Baseline setup // Baseline setup
// -------------- // --------------
// Establish the root object, `window` in the browser, or `global` on the server. // Establish the root object, `window` in the browser, or `exports` on the server.
var root = this; var root = this;
// Save the previous value of the `_` variable. // Save the previous value of the `_` variable.
@@ -65,7 +65,7 @@
} }
// Current version. // Current version.
_.VERSION = '1.5.1'; _.VERSION = '1.5.2';
// Collection Functions // Collection Functions
// -------------------- // --------------------
@@ -78,14 +78,13 @@
if (nativeForEach && obj.forEach === nativeForEach) { if (nativeForEach && obj.forEach === nativeForEach) {
obj.forEach(iterator, context); obj.forEach(iterator, context);
} else if (obj.length === +obj.length) { } else if (obj.length === +obj.length) {
for (var i = 0, l = obj.length; i < l; i++) { for (var i = 0, length = obj.length; i < length; i++) {
if (iterator.call(context, obj[i], i, obj) === breaker) return; if (iterator.call(context, obj[i], i, obj) === breaker) return;
} }
} else { } else {
for (var key in obj) { var keys = _.keys(obj);
if (_.has(obj, key)) { for (var i = 0, length = keys.length; i < length; i++) {
if (iterator.call(context, obj[key], key, obj) === breaker) return; if (iterator.call(context, obj[keys[i]], keys[i], obj) === breaker) return;
}
} }
} }
}; };
@@ -284,7 +283,8 @@
return result.value; return result.value;
}; };
// Shuffle an array. // Shuffle an array, using the modern version of the
// [Fisher-Yates shuffle](http://en.wikipedia.org/wiki/FisherYates_shuffle).
_.shuffle = function(obj) { _.shuffle = function(obj) {
var rand; var rand;
var index = 0; var index = 0;
@@ -297,6 +297,16 @@
return shuffled; return shuffled;
}; };
// Sample **n** random values from an array.
// If **n** is not specified, returns a single random element from the array.
// The internal `guard` argument allows it to work with `map`.
_.sample = function(obj, n, guard) {
if (arguments.length < 2 || guard) {
return obj[_.random(obj.length - 1)];
}
return _.shuffle(obj).slice(0, Math.max(0, n));
};
// An internal function to generate lookup iterators. // An internal function to generate lookup iterators.
var lookupIterator = function(value) { var lookupIterator = function(value) {
return _.isFunction(value) ? value : function(obj){ return obj[value]; }; return _.isFunction(value) ? value : function(obj){ return obj[value]; };
@@ -307,9 +317,9 @@
var iterator = lookupIterator(value); var iterator = lookupIterator(value);
return _.pluck(_.map(obj, function(value, index, list) { return _.pluck(_.map(obj, function(value, index, list) {
return { return {
value : value, value: value,
index : index, index: index,
criteria : iterator.call(context, value, index, list) criteria: iterator.call(context, value, index, list)
}; };
}).sort(function(left, right) { }).sort(function(left, right) {
var a = left.criteria; var a = left.criteria;
@@ -318,38 +328,41 @@
if (a > b || a === void 0) return 1; if (a > b || a === void 0) return 1;
if (a < b || b === void 0) return -1; if (a < b || b === void 0) return -1;
} }
return left.index < right.index ? -1 : 1; return left.index - right.index;
}), 'value'); }), 'value');
}; };
// An internal function used for aggregate "group by" operations. // An internal function used for aggregate "group by" operations.
var group = function(obj, value, context, behavior) { var group = function(behavior) {
var result = {}; return function(obj, value, context) {
var iterator = lookupIterator(value == null ? _.identity : value); var result = {};
each(obj, function(value, index) { var iterator = value == null ? _.identity : lookupIterator(value);
var key = iterator.call(context, value, index, obj); each(obj, function(value, index) {
behavior(result, key, value); var key = iterator.call(context, value, index, obj);
}); behavior(result, key, value);
return result; });
return result;
};
}; };
// Groups the object's values by a criterion. Pass either a string attribute // Groups the object's values by a criterion. Pass either a string attribute
// to group by, or a function that returns the criterion. // to group by, or a function that returns the criterion.
_.groupBy = function(obj, value, context) { _.groupBy = group(function(result, key, value) {
return group(obj, value, context, function(result, key, value) { (_.has(result, key) ? result[key] : (result[key] = [])).push(value);
(_.has(result, key) ? result[key] : (result[key] = [])).push(value); });
});
}; // Indexes the object's values by a criterion, similar to `groupBy`, but for
// when you know that your index values will be unique.
_.indexBy = group(function(result, key, value) {
result[key] = value;
});
// Counts instances of an object that group by a certain criterion. Pass // 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 // either a string attribute to count by, or a function that returns the
// criterion. // criterion.
_.countBy = function(obj, value, context) { _.countBy = group(function(result, key) {
return group(obj, value, context, function(result, key) { _.has(result, key) ? result[key]++ : result[key] = 1;
if (!_.has(result, key)) result[key] = 0; });
result[key]++;
});
};
// Use a comparator function to figure out the smallest index at which // Use a comparator function to figure out the smallest index at which
// an object should be inserted so as to maintain order. Uses binary search. // an object should be inserted so as to maintain order. Uses binary search.
@@ -386,7 +399,7 @@
// allows it to work with `_.map`. // allows it to work with `_.map`.
_.first = _.head = _.take = function(array, n, guard) { _.first = _.head = _.take = function(array, n, guard) {
if (array == null) return void 0; if (array == null) return void 0;
return (n != null) && !guard ? slice.call(array, 0, n) : array[0]; return (n == null) || guard ? array[0] : slice.call(array, 0, n);
}; };
// Returns everything but the last entry of the array. Especially useful on // Returns everything but the last entry of the array. Especially useful on
@@ -401,10 +414,10 @@
// values in the array. The **guard** check allows it to work with `_.map`. // values in the array. The **guard** check allows it to work with `_.map`.
_.last = function(array, n, guard) { _.last = function(array, n, guard) {
if (array == null) return void 0; if (array == null) return void 0;
if ((n != null) && !guard) { if ((n == null) || guard) {
return slice.call(array, Math.max(array.length - n, 0));
} else {
return array[array.length - 1]; return array[array.length - 1];
} else {
return slice.call(array, Math.max(array.length - n, 0));
} }
}; };
@@ -436,7 +449,7 @@
return output; return output;
}; };
// Return a completely flattened version of an array. // Flatten out an array, either recursively (by default), or just one level.
_.flatten = function(array, shallow) { _.flatten = function(array, shallow) {
return flatten(array, shallow, []); return flatten(array, shallow, []);
}; };
@@ -508,7 +521,7 @@
_.object = function(list, values) { _.object = function(list, values) {
if (list == null) return {}; if (list == null) return {};
var result = {}; var result = {};
for (var i = 0, l = list.length; i < l; i++) { for (var i = 0, length = list.length; i < length; i++) {
if (values) { if (values) {
result[list[i]] = values[i]; result[list[i]] = values[i];
} else { } else {
@@ -526,17 +539,17 @@
// for **isSorted** to use binary search. // for **isSorted** to use binary search.
_.indexOf = function(array, item, isSorted) { _.indexOf = function(array, item, isSorted) {
if (array == null) return -1; if (array == null) return -1;
var i = 0, l = array.length; var i = 0, length = array.length;
if (isSorted) { if (isSorted) {
if (typeof isSorted == 'number') { if (typeof isSorted == 'number') {
i = (isSorted < 0 ? Math.max(0, l + isSorted) : isSorted); i = (isSorted < 0 ? Math.max(0, length + isSorted) : isSorted);
} else { } else {
i = _.sortedIndex(array, item); i = _.sortedIndex(array, item);
return array[i] === item ? i : -1; return array[i] === item ? i : -1;
} }
} }
if (nativeIndexOf && array.indexOf === nativeIndexOf) return array.indexOf(item, isSorted); if (nativeIndexOf && array.indexOf === nativeIndexOf) return array.indexOf(item, isSorted);
for (; i < l; i++) if (array[i] === item) return i; for (; i < length; i++) if (array[i] === item) return i;
return -1; return -1;
}; };
@@ -562,11 +575,11 @@
} }
step = arguments[2] || 1; step = arguments[2] || 1;
var len = Math.max(Math.ceil((stop - start) / step), 0); var length = Math.max(Math.ceil((stop - start) / step), 0);
var idx = 0; var idx = 0;
var range = new Array(len); var range = new Array(length);
while(idx < len) { while(idx < length) {
range[idx++] = start; range[idx++] = start;
start += step; start += step;
} }
@@ -678,17 +691,24 @@
// N milliseconds. If `immediate` is passed, trigger the function on the // N milliseconds. If `immediate` is passed, trigger the function on the
// leading edge, instead of the trailing. // leading edge, instead of the trailing.
_.debounce = function(func, wait, immediate) { _.debounce = function(func, wait, immediate) {
var result; var timeout, args, context, timestamp, result;
var timeout = null;
return function() { return function() {
var context = this, args = arguments; context = this;
args = arguments;
timestamp = new Date();
var later = function() { var later = function() {
timeout = null; var last = (new Date()) - timestamp;
if (!immediate) result = func.apply(context, args); if (last < wait) {
timeout = setTimeout(later, wait - last);
} else {
timeout = null;
if (!immediate) result = func.apply(context, args);
}
}; };
var callNow = immediate && !timeout; var callNow = immediate && !timeout;
clearTimeout(timeout); if (!timeout) {
timeout = setTimeout(later, wait); timeout = setTimeout(later, wait);
}
if (callNow) result = func.apply(context, args); if (callNow) result = func.apply(context, args);
return result; return result;
}; };
@@ -754,22 +774,33 @@
// Retrieve the values of an object's properties. // Retrieve the values of an object's properties.
_.values = function(obj) { _.values = function(obj) {
var values = []; var keys = _.keys(obj);
for (var key in obj) if (_.has(obj, key)) values.push(obj[key]); var length = keys.length;
var values = new Array(length);
for (var i = 0; i < length; i++) {
values[i] = obj[keys[i]];
}
return values; return values;
}; };
// Convert an object into a list of `[key, value]` pairs. // Convert an object into a list of `[key, value]` pairs.
_.pairs = function(obj) { _.pairs = function(obj) {
var pairs = []; var keys = _.keys(obj);
for (var key in obj) if (_.has(obj, key)) pairs.push([key, obj[key]]); var length = keys.length;
var pairs = new Array(length);
for (var i = 0; i < length; i++) {
pairs[i] = [keys[i], obj[keys[i]]];
}
return pairs; return pairs;
}; };
// Invert the keys and values of an object. The values must be serializable. // Invert the keys and values of an object. The values must be serializable.
_.invert = function(obj) { _.invert = function(obj) {
var result = {}; var result = {};
for (var key in obj) if (_.has(obj, key)) result[obj[key]] = key; var keys = _.keys(obj);
for (var i = 0, length = keys.length; i < length; i++) {
result[obj[keys[i]]] = keys[i];
}
return result; return result;
}; };
@@ -1053,8 +1084,7 @@
'<': '&lt;', '<': '&lt;',
'>': '&gt;', '>': '&gt;',
'"': '&quot;', '"': '&quot;',
"'": '&#x27;', "'": '&#x27;'
'/': '&#x2F;'
} }
}; };
entityMap.unescape = _.invert(entityMap.escape); entityMap.unescape = _.invert(entityMap.escape);
@@ -1085,7 +1115,7 @@
// Add your own custom functions to the Underscore object. // Add your own custom functions to the Underscore object.
_.mixin = function(obj) { _.mixin = function(obj) {
each(_.functions(obj), function(name){ each(_.functions(obj), function(name) {
var func = _[name] = obj[name]; var func = _[name] = obj[name];
_.prototype[name] = function() { _.prototype[name] = function() {
var args = [this._wrapped]; var args = [this._wrapped];