From 82e58f2bf9f16e14a70fa70053a09ef6de4d158f Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Sun, 11 Nov 2012 16:16:11 -0800 Subject: [PATCH] Make `_.defaults` follow `_.assign` and only assign own properties of `source`. Former-commit-id: 114dc47bbfa6db6a53a2fa03dd477fe86c3395cb --- build.js | 20 ++++++++++++++++++++ lodash.js | 3 +-- test/test-build.js | 6 ++++++ test/test.js | 14 ++++++++++++++ 4 files changed, 41 insertions(+), 2 deletions(-) diff --git a/build.js b/build.js index 9c6d94852..6762c34ae 100755 --- a/build.js +++ b/build.js @@ -1125,6 +1125,26 @@ ' }' ].join('\n')); + // replace `_.defaults` + source = source.replace(/^( *)var defaults *= *createIterator[\s\S]+?\);/m, [ + ' function defaults(object) {', + ' if (!object) {', + ' return object;', + ' }', + ' for (var argsIndex = 1, argsLength = arguments.length; argsIndex < argsLength; argsIndex++) {', + ' var iteratee = arguments[argsIndex];', + ' if (iteratee) {', + ' for (var key in iteratee) {', + ' if (object[key] == null) {', + ' object[key] = iteratee[key];', + ' }', + ' }', + ' }', + ' }', + ' return object;', + ' }' + ].join('\n')); + // replace `_.intersection` source = source.replace(/^( *)function intersection[\s\S]+?\n\1}/m, [ ' function intersection(array) {', diff --git a/lodash.js b/lodash.js index 708c39db9..bf77e89ec 100644 --- a/lodash.js +++ b/lodash.js @@ -977,7 +977,7 @@ } /** - * Assigns enumerable properties of the default object(s) to the `destination` + * Assigns own enumerable properties of source object(s) to the `destination` * object for all `destination` properties that resolve to `null`/`undefined`. * Once a property is set, additional defaults of the same property will be * ignored. @@ -995,7 +995,6 @@ * // => { 'flavor': 'chocolate', 'sprinkles': 'rainbow' } */ var defaults = createIterator(assignIteratorOptions, { - 'useHas': false, 'objectLoop': 'if (result[index] == null) ' + assignIteratorOptions.objectLoop }); diff --git a/test/test-build.js b/test/test-build.js index 0216667d1..d090c9b58 100644 --- a/test/test-build.js +++ b/test/test-build.js @@ -651,6 +651,12 @@ equal(lodash.contains([1, 2, 3], 1, 2), true, '_.contains should ignore `fromIndex`: ' + basename); equal(lodash.every([true, false, true]), false, '_.every: ' + basename); + function Foo() {} + Foo.prototype = { 'a': 1 }; + + deepEqual(lodash.defaults({}, new Foo), Foo.prototype, '_.defaults should assign inherited `source` properties: ' + basename); + deepEqual(lodash.extend({}, new Foo), Foo.prototype, '_.extend should assign inherited `source` properties: ' + basename); + actual = lodash.find(array, function(value) { return 'value' in value; }); diff --git a/test/test.js b/test/test.js index 6c54fd70f..a651a8662 100644 --- a/test/test.js +++ b/test/test.js @@ -421,6 +421,20 @@ /*--------------------------------------------------------------------------*/ + QUnit.module('source property checks'); + + _.each(['assign', 'defaults'], function(methodName) { + var func = _[methodName]; + + test('lodash.' + methodName + ' should not assign inherited `source` properties', function() { + function Foo() {} + Foo.prototype = { 'a': 1 }; + deepEqual(func({}, new Foo), {}); + }); + }); + + /*--------------------------------------------------------------------------*/ + QUnit.module('strict mode checks'); _.each(['assign', 'bindAll', 'defaults'], function(methodName) {