From 5fe15a160d7f0aec76a33d1ac25b260441d20d3b Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Wed, 8 Feb 2017 11:05:23 -0800 Subject: [PATCH] Simplify `defaults`. [closes #2983] --- defaults.js | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/defaults.js b/defaults.js index 78eabac51..76ad0e184 100644 --- a/defaults.js +++ b/defaults.js @@ -1,6 +1,11 @@ -import apply from './.internal/apply.js' -import assignInWith from './assignInWith.js' -import customDefaultsAssignIn from './.internal/customDefaultsAssignIn.js' +import eq from './eq.js' +import keysIn from './keysIn.js' + +/** Used for built-in method references. */ +const objectProto = Object.prototype + +/** Used to check objects for own properties. */ +const hasOwnProperty = objectProto.hasOwnProperty /** * Assigns own and inherited enumerable string keyed properties of source @@ -21,9 +26,25 @@ import customDefaultsAssignIn from './.internal/customDefaultsAssignIn.js' * defaults({ 'a': 1 }, { 'b': 2 }, { 'a': 3 }) * // => { 'a': 1, 'b': 2 } */ -function defaults(...args) { - args.push(undefined, customDefaultsAssignIn) - return apply(assignInWith, undefined, args) +function defaults(object, ...sources) { + object = Object(object) + let length = sources.length + while (length--) { + const source = sources[length] + const props = keysIn(source) + let index = -1 + const propsLength = props.length + + while (++index < propsLength) { + const key = props[index] + const value = object[key] + if (value === undefined || + (eq(value, objectProto[key]) && !hasOwnProperty.call(object, key))) { + object[key] = source[key] + } + } + } + return object } export default defaults