| 1 |
var baseRest = require('./_baseRest'), |
| 2 |
eq = require('./eq'), |
| 3 |
isIterateeCall = require('./_isIterateeCall'), |
| 4 |
keysIn = require('./keysIn'); |
| 5 |
|
| 6 |
/** Used for built-in method references. */ |
| 7 |
var objectProto = Object.prototype; |
| 8 |
|
| 9 |
/** Used to check objects for own properties. */ |
| 10 |
var hasOwnProperty = objectProto.hasOwnProperty; |
| 11 |
|
| 12 |
/** |
| 13 |
* Assigns own and inherited enumerable string keyed properties of source |
| 14 |
* objects to the destination object for all destination properties that |
| 15 |
* resolve to `undefined`. Source objects are applied from left to right. |
| 16 |
* Once a property is set, additional values of the same property are ignored. |
| 17 |
* |
| 18 |
* **Note:** This method mutates `object`. |
| 19 |
* |
| 20 |
* @static |
| 21 |
* @since 0.1.0 |
| 22 |
* @memberOf _ |
| 23 |
* @category Object |
| 24 |
* @param {Object} object The destination object. |
| 25 |
* @param {...Object} [sources] The source objects. |
| 26 |
* @returns {Object} Returns `object`. |
| 27 |
* @see _.defaultsDeep |
| 28 |
* @example |
| 29 |
* |
| 30 |
* _.defaults({ 'a': 1 }, { 'b': 2 }, { 'a': 3 }); |
| 31 |
* // => { 'a': 1, 'b': 2 } |
| 32 |
*/ |
| 33 |
var defaults = baseRest(function(object, sources) { |
| 34 |
object = Object(object); |
| 35 |
|
| 36 |
var index = -1; |
| 37 |
var length = sources.length; |
| 38 |
var guard = length > 2 ? sources[2] : undefined; |
| 39 |
|
| 40 |
if (guard && isIterateeCall(sources[0], sources[1], guard)) { |
| 41 |
length = 1; |
| 42 |
} |
| 43 |
|
| 44 |
while (++index < length) { |
| 45 |
var source = sources[index]; |
| 46 |
var props = keysIn(source); |
| 47 |
var propsIndex = -1; |
| 48 |
var propsLength = props.length; |
| 49 |
|
| 50 |
while (++propsIndex < propsLength) { |
| 51 |
var key = props[propsIndex]; |
| 52 |
var value = object[key]; |
| 53 |
|
| 54 |
if (value === undefined || |
| 55 |
(eq(value, objectProto[key]) && !hasOwnProperty.call(object, key))) { |
| 56 |
object[key] = source[key]; |
| 57 |
} |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
return object; |
| 62 |
}); |
| 63 |
|
| 64 |
module.exports = defaults; |