diff --git a/test/arrays.js b/test/arrays.js index 1c910355f..7e2c7328b 100644 --- a/test/arrays.js +++ b/test/arrays.js @@ -127,9 +127,12 @@ $(document).ready(function() { }); test("arrays: indexOf", function() { - var numbers = [1, 2, 3]; + var numbers = [1]; + numbers[2] = 3; numbers.indexOf = null; - equals(_.indexOf(numbers, 2), 1, 'can compute indexOf, even without the native function'); + equals(_.indexOf(numbers, 3), 2, 'can compute indexOf, even without the native function'); + equals(_.indexOf(numbers, void 0), -1, 'handles sparse arrays correctly'); + var result = (function(){ return _.indexOf(arguments, 2); })(1, 2, 3); equals(result, 1, 'works on an arguments object'); equals(_.indexOf(null, 2), -1, 'handles nulls properly'); @@ -148,10 +151,13 @@ $(document).ready(function() { }); test("arrays: lastIndexOf", function() { - var numbers = [1, 0, 1, 0, 0, 1, 0, 0, 0]; + var numbers = [1, 0, 1, 0, 0, 1, 0]; + numbers[8] = 0; numbers.lastIndexOf = null; equals(_.lastIndexOf(numbers, 1), 5, 'can compute lastIndexOf, even without the native function'); equals(_.lastIndexOf(numbers, 0), 8, 'lastIndexOf the other element'); + equals(_.lastIndexOf(numbers, void 0), -1, 'handles sparse arrays correctly'); + var result = (function(){ return _.lastIndexOf(arguments, 1); })(1, 0, 1, 0, 0, 1, 0, 0, 0); equals(result, 5, 'works on an arguments object'); equals(_.indexOf(null, 2), -1, 'handles nulls properly'); diff --git a/test/collections.js b/test/collections.js index 1726629eb..b05bed8b6 100644 --- a/test/collections.js +++ b/test/collections.js @@ -16,11 +16,11 @@ $(document).ready(function() { equals(answers.join(', '), '1, 2, 3', 'aliased as "forEach"'); answers = []; - var obj = {one : 1, two : 2, three : 3}; + var obj = {one: 1, two: 2, three: 3, toString: 1}; obj.constructor.prototype.four = 4; _.each(obj, function(value, key){ answers.push(key); }); - equals(answers.join(", "), 'one, two, three', 'iterating over objects works, and ignores the object prototype.'); delete obj.constructor.prototype.four; + equals(answers.sort().join(', '), 'one, three, toString, two', 'iterating over objects works, and ignores the object prototype.'); answer = null; _.each([1, 2, 3], function(num, index, arr){ if (_.include(arr, num)) answer = true; }); @@ -233,7 +233,7 @@ $(document).ready(function() { equals(_.toArray(a).join(', '), '1, 2, 3', 'cloned array contains same elements'); var numbers = _.toArray({one : 1, two : 2, three : 3}); - equals(numbers.join(', '), '1, 2, 3', 'object flattened into array'); + equals(numbers.sort().join(', '), '1, 2, 3', 'object flattened into array'); }); test('collections: size', function() { diff --git a/test/objects.js b/test/objects.js index 6d432033a..42bea76a8 100644 --- a/test/objects.js +++ b/test/objects.js @@ -4,8 +4,30 @@ $(document).ready(function() { test("objects: keys", function() { var exception = /object/; - equals(_.keys({one : 1, two : 2}).join(', '), 'one, two', 'can extract the keys from an object'); - // the test above is not safe because it relies on for-in enumeration order + equals( + _.keys({one: 1, two: 2}).sort().join(', '), + 'one, two', + 'can extract the keys from an object' + ); + + equals( + _.keys({ + constructor: 'a', + hasOwnProperty: 'b', + isPrototypeOf: 'c', + propertyIsEnumerable: 'd', + toLocaleString: 'e', + toString: 'f', + valueOf: 'g' + }).sort().join(', '), + 'constructor, hasOwnProperty, isPrototypeOf, propertyIsEnumerable, toLocaleString, toString, valueOf', + 'keys of shadowing properties' + ); + + var fn = function(){}; + fn.x = fn.y = fn.z = fn.prototype.a = 1; + equals(_.keys(fn).sort().join(', '), 'x, y, z', 'keys of functions works, and ignores the prototype property'); + var a = []; a[1] = 0; equals(_.keys(a).join(', '), '1', 'is not fooled by sparse arrays; see issue #95'); raises(function() { _.keys(null); }, exception, 'throws an error for `null` values'); @@ -16,7 +38,7 @@ $(document).ready(function() { }); test("objects: values", function() { - equals(_.values({one : 1, two : 2}).join(', '), '1, 2', 'can extract the values from an object'); + equals(_.values({one : 1, two : 2}).sort().join(', '), '1, 2', 'can extract the values from an object'); }); test("objects: functions", function() { @@ -30,20 +52,38 @@ $(document).ready(function() { test("objects: extend", function() { var result; + var expected = { + constructor: 'a', + hasOwnProperty: 'b', + isPrototypeOf: 'c', + propertyIsEnumerable: 'd', + toLocaleString: 'e', + toString: 'f', + valueOf: 'g' + }; + equals(_.extend({}, {a:'b'}).a, 'b', 'can extend an object with the attributes of another'); equals(_.extend({a:'x'}, {a:'b'}).a, 'b', 'properties in source override destination'); equals(_.extend({x:'x'}, {a:'b'}).x, 'x', 'properties not in source dont get overriden'); + + result = _.extend({}, expected); + ok(_.isEqual(result, expected), 'extend with shadow properties'); + result = _.extend({x:'x'}, {a:'a'}, {b:'b'}); ok(_.isEqual(result, {x:'x', a:'a', b:'b'}), 'can extend from multiple source objects'); + 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'); + result = _.extend({}, {a: void 0, b: null}); equals(_.keys(result).join(''), 'b', 'extend does not copy undefined values'); }); test("objects: defaults", function() { var result; - var options = {zero: 0, one: 1, empty: "", nan: NaN, string: "string"}; + var Options = function() { this.empty = ''; this.string = 'string'; this.zero = 0; }; + _.extend(Options.prototype, {nan: NaN, one:1}); + var options = new Options; _.defaults(options, {zero: 1, one: 10, twenty: 20}); equals(options.zero, 0, 'value exists'); @@ -57,9 +97,14 @@ $(document).ready(function() { }); test("objects: clone", function() { - var moe = {name : 'moe', lucky : [13, 27, 34]}; + var toString = function() { return this.name; }; + var valueOf = function() { return this.name; }; + var moe = {name : 'moe', lucky : [13, 27, 34], toString: toString, valueOf: valueOf}; + var clone = _.clone(moe); equals(clone.name, 'moe', 'the clone as the attributes of the original'); + equals(clone.toString, toString, 'cloned own toString method'); + equals(clone.valueOf, valueOf, 'cloned own valueOf method'); clone.name = 'curly'; ok(clone.name == 'curly' && moe.name == 'moe', 'clones can change shallow attributes without affecting the original'); @@ -82,6 +127,16 @@ $(document).ready(function() { } Second.prototype.value = 2; + var obj = { + constructor: 'a', + hasOwnProperty: 'b', + isPrototypeOf: 'c', + propertyIsEnumerable: 'd', + toLocaleString: 'e', + toString: 'f', + valueOf: 'g' + }; + // Basic equality and identity comparisons. ok(_.isEqual(null, null), "`null` is equal to `null`"); ok(_.isEqual(), "`undefined` is equal to `undefined`"); @@ -208,6 +263,10 @@ $(document).ready(function() { ok(!_.isEqual({a: 1}, {a: 1, b: 2}), "Commutative equality is implemented for objects"); ok(!_.isEqual({x: 1, y: undefined}, {x: 1, z: 2}), "Objects with identical keys and different values are not equivalent"); + // Objects with shadowing properties + ok(!_.isEqual({}, {toString: 1}), "Object with custom toString is not equal to {}"); + ok(_.isEqual({toString: 1, valueOf: 2}, {toString: 1, valueOf: 2}), "Objects with equivalent shadow properties"); + // `A` contains nested objects and arrays. a = { name: new String("Moe Howard"), @@ -358,7 +417,7 @@ $(document).ready(function() { test("objects: isEmpty", function() { ok(!_([1]).isEmpty(), '[1] is not empty'); ok(_.isEmpty([]), '[] is empty'); - ok(!_.isEmpty({one : 1}), '{one : 1} is not empty'); + ok(!_.isEmpty({valueOf: 1}), '{valueOf: 1} is not empty'); ok(_.isEmpty({}), '{} is empty'); ok(_.isEmpty(new RegExp('')), 'objects with prototype properties are empty'); ok(_.isEmpty(null), 'null is empty'); @@ -388,7 +447,7 @@ $(document).ready(function() { parent.iNaN = NaN;\ parent.iNull = null;\ parent.iBoolean = new Boolean(false);\ - parent.iUndefined = undefined;\ + parent.iUndefined = void 0;\ " ); iDoc.close(); diff --git a/underscore.js b/underscore.js index e535966b3..ef515d167 100644 --- a/underscore.js +++ b/underscore.js @@ -84,6 +84,8 @@ var fn = iterator; var i = -1; var l = obj.length; + + // We optimized for common use by only binding a context when it's passed if (context) { iterator = function() { return fn.call(context, obj[i], i, obj); }; } @@ -98,7 +100,7 @@ } }; - // A simple each, for when we know we're dealing with an array. + // A simple each, for dealing with non-sparse arrays and arguments objects var simpleEach = function(obj, iterator, index) { index || (index = 0); for (var l = obj.length; index < l; index++) { @@ -112,15 +114,15 @@ 'toLocaleString', 'toString', 'valueOf' ]; - // IE < 9 skips enumerable properties shadowing non-enumerable ones. + // IE < 9 makes properties, shadowing non-enumerable ones, non-enumerable too var forShadowed = !{valueOf:0}.propertyIsEnumerable('valueOf') && function(obj, iterator) { - // because IE < 9 can't set the `[[Enumerable]]` attribute of an existing + // Because IE < 9 can't set the `[[Enumerable]]` attribute of an existing // property and the `constructor` property of a prototype defaults to // non-enumerable, we manually skip the `constructor` property when we // think we are iterating over a `prototype` object. var ctor = obj.constructor; - var skipCtor = ctor && ctor.prototype && ctor.prototype.constructor == ctor; + var skipCtor = ctor && ctor.prototype && ctor.prototype.constructor === ctor; for (var key, i = 0; key = shadowed[i]; i++) { if (!(skipCtor && key == 'constructor') && hasOwnProperty.call(obj, key) && @@ -131,24 +133,25 @@ }; // Iterates over an object's properties, executing the `callback` for each. - var forProps = function(obj, iterator, ownOnly, context) { + var forProps = function(obj, iterator, ownOnly) { var done = !obj; var skipProto = typeof obj == 'function'; for (var key in obj) { - // Opera < 12 and Safari < 5.1 (if the prototype or a property on the prototype has been set) + // Firefox < 3.6, Opera > 9.50 - Opera < 12, and Safari < 5.1 + // (if the prototype or a property on the prototype has been set) // incorrectly set a function's `prototype` property [[Enumerable]] value // to true. Because of this we standardize on skipping the the `prototype` // property of functions regardless of their [[Enumerable]] value. if (done = !(skipProto && key == 'prototype') && (!ownOnly || ownOnly && hasOwnProperty.call(obj, key)) && - iterator.call(context, obj[key], key, obj) === breaker) { + iterator(obj[key], key, obj) === breaker) { break; } } if (!done && forShadowed) { - forShadowed(obj, iterator, context); + forShadowed(obj, iterator); } }; @@ -1016,9 +1019,8 @@ // A method to easily add functions to the OOP wrapper. var addToWrapper = function(name, func) { wrapper.prototype[name] = function() { - var args = slice.call(arguments); - unshift.call(args, this._wrapped); - return result(func.apply(_, args), this._chain); + unshift.call(arguments, this._wrapped); + return result(func.apply(_, arguments), this._chain); }; };