Last active
May 11, 2016 08:10
-
-
Save unti1x/493ca09e60009abe5c540e4252d3e22e to your computer and use it in GitHub Desktop.
Set default values recoursive
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
_mergeDefaults = (item, defaultValues...)-> | |
for defaultValue in defaultValues | |
for key, value of defaultValue | |
if not item[key] | |
item[key] = value | |
else if typeof value is 'object' and typeof value.length is 'undefined' | |
item[key] = _mergeDefaults(item[key], value) | |
return item | |
# call | |
_mergeDefaults {a: 1, b: {c: 2, d:3}}, {a: 5, b: {t: 8}}, {d: 1} # => a: 1, b: {c: 2, d: 3, t: 8}, d: 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment