55 lines
1.9 KiB
JavaScript
55 lines
1.9 KiB
JavaScript
|
/**
|
||
|
* Lo-Dash 2.4.1 (Custom Build) <http://lodash.com/>
|
||
|
* Build: `lodash modularize modern exports="npm" -o ./npm/`
|
||
|
* Copyright 2012-2013 The Dojo Foundation <http://dojofoundation.org/>
|
||
|
* Based on Underscore.js 1.5.2 <http://underscorejs.org/LICENSE>
|
||
|
* Copyright 2009-2013 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors
|
||
|
* Available under MIT license <http://lodash.com/license>
|
||
|
*/
|
||
|
var keys = require('lodash.keys'),
|
||
|
objectTypes = require('lodash._objecttypes');
|
||
|
|
||
|
/**
|
||
|
* Assigns own enumerable properties of source object(s) to the destination
|
||
|
* object for all destination properties that resolve to `undefined`. Once a
|
||
|
* property is set, additional defaults of the same property will be ignored.
|
||
|
*
|
||
|
* @static
|
||
|
* @memberOf _
|
||
|
* @type Function
|
||
|
* @category Objects
|
||
|
* @param {Object} object The destination object.
|
||
|
* @param {...Object} [source] The source objects.
|
||
|
* @param- {Object} [guard] Allows working with `_.reduce` without using its
|
||
|
* `key` and `object` arguments as sources.
|
||
|
* @returns {Object} Returns the destination object.
|
||
|
* @example
|
||
|
*
|
||
|
* var object = { 'name': 'barney' };
|
||
|
* _.defaults(object, { 'name': 'fred', 'employer': 'slate' });
|
||
|
* // => { 'name': 'barney', 'employer': 'slate' }
|
||
|
*/
|
||
|
var defaults = function(object, source, guard) {
|
||
|
var index, iterable = object, result = iterable;
|
||
|
if (!iterable) return result;
|
||
|
var args = arguments,
|
||
|
argsIndex = 0,
|
||
|
argsLength = typeof guard == 'number' ? 2 : args.length;
|
||
|
while (++argsIndex < argsLength) {
|
||
|
iterable = args[argsIndex];
|
||
|
if (iterable && objectTypes[typeof iterable]) {
|
||
|
var ownIndex = -1,
|
||
|
ownProps = objectTypes[typeof iterable] && keys(iterable),
|
||
|
length = ownProps ? ownProps.length : 0;
|
||
|
|
||
|
while (++ownIndex < length) {
|
||
|
index = ownProps[ownIndex];
|
||
|
if (typeof result[index] == 'undefined') result[index] = iterable[index];
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return result
|
||
|
};
|
||
|
|
||
|
module.exports = defaults;
|