index.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /**
  2. * Lo-Dash 2.4.1 (Custom Build) <http://lodash.com/>
  3. * Build: `lodash modularize modern exports="npm" -o ./npm/`
  4. * Copyright 2012-2013 The Dojo Foundation <http://dojofoundation.org/>
  5. * Based on Underscore.js 1.5.2 <http://underscorejs.org/LICENSE>
  6. * Copyright 2009-2013 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors
  7. * Available under MIT license <http://lodash.com/license>
  8. */
  9. var keys = require('lodash.keys'),
  10. objectTypes = require('lodash._objecttypes');
  11. /**
  12. * Assigns own enumerable properties of source object(s) to the destination
  13. * object for all destination properties that resolve to `undefined`. Once a
  14. * property is set, additional defaults of the same property will be ignored.
  15. *
  16. * @static
  17. * @memberOf _
  18. * @type Function
  19. * @category Objects
  20. * @param {Object} object The destination object.
  21. * @param {...Object} [source] The source objects.
  22. * @param- {Object} [guard] Allows working with `_.reduce` without using its
  23. * `key` and `object` arguments as sources.
  24. * @returns {Object} Returns the destination object.
  25. * @example
  26. *
  27. * var object = { 'name': 'barney' };
  28. * _.defaults(object, { 'name': 'fred', 'employer': 'slate' });
  29. * // => { 'name': 'barney', 'employer': 'slate' }
  30. */
  31. var defaults = function(object, source, guard) {
  32. var index, iterable = object, result = iterable;
  33. if (!iterable) return result;
  34. var args = arguments,
  35. argsIndex = 0,
  36. argsLength = typeof guard == 'number' ? 2 : args.length;
  37. while (++argsIndex < argsLength) {
  38. iterable = args[argsIndex];
  39. if (iterable && objectTypes[typeof iterable]) {
  40. var ownIndex = -1,
  41. ownProps = objectTypes[typeof iterable] && keys(iterable),
  42. length = ownProps ? ownProps.length : 0;
  43. while (++ownIndex < length) {
  44. index = ownProps[ownIndex];
  45. if (typeof result[index] == 'undefined') result[index] = iterable[index];
  46. }
  47. }
  48. }
  49. return result
  50. };
  51. module.exports = defaults;