diff --git a/README.md b/README.md index fd9d07be5..2c4d09bbf 100644 --- a/README.md +++ b/README.md @@ -271,6 +271,7 @@ average(1, 2, 3);
View contents +* [`defaults`](#defaults) * [`equals`](#equals-) * [`forOwn`](#forown) * [`forOwnRight`](#forownright) @@ -4135,6 +4136,28 @@ UUIDGeneratorNode(); // '79c7c136-60ee-40a2-beb2-856f1feabefc' --- ## 🗃️ Object +### 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); +``` + +
+Examples + +```js +defaults({ a: 1 }, { b: 2 }, { b: 6 }, { a: 3 }); // { a: 1, b: 2 } +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + ### equals ![advanced](/advanced.svg) Performs a deep comparison between two values to determine if they are equivalent. diff --git a/docs/index.html b/docs/index.html index b294be08d..3d3797a5f 100644 --- a/docs/index.html +++ b/docs/index.html @@ -50,7 +50,7 @@ scrollToTop(); } }, false); - }

logo 30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.

 

Adapter

call

Given a key and a set of arguments, call them when given a context. Primarily useful in composition.

Use a closure to call a stored key with stored arguments.

const call = (key, ...args) => context => context[key](...args);
+      }

logo 30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.

 

Adapter

call

Given a key and a set of arguments, call them when given a context. Primarily useful in composition.

Use a closure to call a stored key with stored arguments.

const call = (key, ...args) => context => context[key](...args);
 
Promise.resolve([1, 2, 3])
   .then(call('map', x => 2 * x))
   .then(console.log); //[ 2, 4, 6 ]
@@ -913,7 +913,9 @@ console.log<
     (c ^ (crypto.randomBytes(1)[0] & (15 >> (c / 4)))).toString(16)
   );
 
UUIDGeneratorNode(); // '79c7c136-60ee-40a2-beb2-856f1feabefc'
-

Object

equalsadvanced

Performs a deep comparison between two values to determine if they are equivalent.

Check if the two values are identical, if they are both Date objects with the same time, using Date.getTime() or if they are both non-object values with an equivalent value (strict comparison). Check if only one value is null or undefined or if their prototypes differ. If none of the above conditions are met, use Object.keys() to check if both values have the same number of keys, then use Array.every() to check if every key in the first value exists in the second one and if they are equivalent by calling this method recursively.

const equals = (a, b) => {
+

Object

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.

const defaults = (obj, ...defs) => Object.assign({}, obj, ...defs.reverse(), obj);
+
defaults({ a: 1 }, { b: 2 }, { b: 6 }, { a: 3 }); // { a: 1, b: 2 }
+

equalsadvanced

Performs a deep comparison between two values to determine if they are equivalent.

Check if the two values are identical, if they are both Date objects with the same time, using Date.getTime() or if they are both non-object values with an equivalent value (strict comparison). Check if only one value is null or undefined or if their prototypes differ. If none of the above conditions are met, use Object.keys() to check if both values have the same number of keys, then use Array.every() to check if every key in the first value exists in the second one and if they are equivalent by calling this method recursively.

const equals = (a, b) => {
   if (a === b) return true;
   if (a instanceof Date && b instanceof Date) return a.getTime() === b.getTime();
   if (!a || !b || (typeof a != 'object' && typeof b !== 'object')) return a === b;
diff --git a/snippets/defaults.md b/snippets/defaults.md
index b34c04503..d023b4744 100644
--- a/snippets/defaults.md
+++ b/snippets/defaults.md
@@ -9,5 +9,5 @@ const defaults = (obj, ...defs) => Object.assign({}, obj, ...defs.reverse(), obj
 ```
 
 ```js
-defaults({ 'a': 1 }, { 'b': 2 }, { 'b': 6 }, { 'a': 3 }); // { a: 1, b: 2 }
+defaults({ a: 1 }, { b: 2 }, { b: 6 }, { a: 3 }); // { a: 1, b: 2 }
 ```