diff --git a/snippets/defaults.md b/snippets/defaults.md new file mode 100644 index 000000000..b34c04503 --- /dev/null +++ b/snippets/defaults.md @@ -0,0 +1,13 @@ +### defaults + +Assigns default values for all properties in an object that are `undefined`. + +Use `Object.assign()` to create a new empty object and copy the original one to maintain key order, use `Array.reverse()` and the spread operator `...` to combine the default values from left to right, finally use `obj` again to overwrite properties that originally had a value. + +```js +const defaults = (obj, ...defs) => Object.assign({}, obj, ...defs.reverse(), obj); +``` + +```js +defaults({ 'a': 1 }, { 'b': 2 }, { 'b': 6 }, { 'a': 3 }); // { a: 1, b: 2 } +``` diff --git a/tag_database b/tag_database index 86117dc08..984f905b7 100644 --- a/tag_database +++ b/tag_database @@ -28,6 +28,7 @@ currentURL:browser,url curry:function,recursion decapitalize:string,array deepFlatten:array,recursion +defaults:object defer:function detectDeviceType:browser difference:array,math