Make _.extend and alias of _.assign and make _.assign iterate only own enumerable source props to align with ES6.

Former-commit-id: 37ba7c3066c1ea70210346a9bf598e8587e907db
This commit is contained in:
John-David Dalton
2012-11-11 15:52:43 -08:00
parent ec6a709393
commit 797a87ca26
4 changed files with 91 additions and 93 deletions

View File

@@ -18,11 +18,11 @@
var aliasToRealMap = {
'all': 'every',
'any': 'some',
'assign': 'extend',
'collect': 'map',
'detect': 'find',
'drop': 'rest',
'each': 'forEach',
'extend': 'assign',
'foldl': 'reduce',
'foldr': 'reduceRight',
'head': 'first',
@@ -37,9 +37,9 @@
/** Used to associate real names with their aliases */
var realToAliasMap = {
'assign': ['extend'],
'contains': ['include'],
'every': ['all'],
'extend': ['assign'],
'filter': ['select'],
'find': ['detect'],
'first': ['head', 'take'],
@@ -242,7 +242,6 @@
/** List of methods used by Underscore */
var underscoreMethods = _.without.apply(_, [allMethods].concat([
'assign',
'forIn',
'forOwn',
'isPlainObject',

View File

@@ -163,6 +163,42 @@
/*--------------------------------------------------------------------------*/
QUnit.module('lodash.assign');
(function() {
test('should not error on `null` or `undefined` sources (test in IE < 9)', function() {
try {
deepEqual(_.assign({}, null, undefined, { 'a': 1 }), { 'a': 1 });
} catch(e) {
ok(false);
}
});
test('skips the prototype property of functions (test in Firefox < 3.6, Opera > 9.50 - Opera < 11.60, and Safari < 5.1)', function() {
function Foo() {}
Foo.prototype.c = 3;
Foo.a = 1;
Foo.b = 2;
var expected = { 'a': 1, 'b': 2 };
deepEqual(_.assign({}, Foo), expected);
Foo.prototype = { 'c': 3 };
deepEqual(_.assign({}, Foo), expected);
});
test('should work with `_.reduce`', function() {
var actual = { 'a': 1},
array = [{ 'b': 2 }, { 'c': 3 }];
_.reduce(array, _.assign, actual);
deepEqual(actual, { 'a': 1, 'b': 2, 'c': 3});
});
}());
/*--------------------------------------------------------------------------*/
QUnit.module('lodash.bind');
(function() {
@@ -385,45 +421,9 @@
/*--------------------------------------------------------------------------*/
QUnit.module('lodash.extend');
(function() {
test('should not error on `null` or `undefined` sources (test in IE < 9)', function() {
try {
deepEqual(_.extend({}, null, undefined, { 'a': 1 }), { 'a': 1 });
} catch(e) {
ok(false);
}
});
test('skips the prototype property of functions (test in Firefox < 3.6, Opera > 9.50 - Opera < 11.60, and Safari < 5.1)', function() {
function Foo() {}
Foo.prototype.c = 3;
Foo.a = 1;
Foo.b = 2;
var expected = { 'a': 1, 'b': 2 };
deepEqual(_.extend({}, Foo), expected);
Foo.prototype = { 'c': 3 };
deepEqual(_.extend({}, Foo), expected);
});
test('should work with `_.reduce`', function() {
var actual = { 'a': 1},
array = [{ 'b': 2 }, { 'c': 3 }];
_.reduce(array, _.extend, actual);
deepEqual(actual, { 'a': 1, 'b': 2, 'c': 3});
});
}());
/*--------------------------------------------------------------------------*/
QUnit.module('strict mode checks');
_.each(['bindAll', 'defaults', 'extend'], function(methodName) {
_.each(['assign', 'bindAll', 'defaults'], function(methodName) {
var func = _[methodName];
test('lodash.' + methodName + ' should not throw strict mode errors', function() {