diff --git a/README.md b/README.md index af92350ed..4996266d6 100644 --- a/README.md +++ b/README.md @@ -284,6 +284,8 @@ average(1, 2, 3); * [`lowercaseKeys`](#lowercasekeys) * [`mapKeys`](#mapkeys) * [`mapValues`](#mapvalues) +* [`matches`](#matches) +* [`matchesWith`](#matcheswith) * [`merge`](#merge) * [`objectFromPairs`](#objectfrompairs) * [`objectToPairs`](#objecttopairs) @@ -4533,6 +4535,64 @@ mapValues(users, u => u.age); // { fred: 40, pebbles: 1 }
[⬆ Back to top](#table-of-contents) +### matches + +Compares two objects to determine if the first one contains equivalent property values to the second one. + +Use `Object.keys(source)` to get all the keys of the second object, then `Array.every()`, `Object.hasOwnProperty()` and strict comparison to determine if all keys exist in the first object and have the same values. + +```js +const matches = (obj, source) => + Object.keys(source).every(key => obj.hasOwnProperty(key) && obj[key] === source[key]); +``` + +
+Examples + +```js +matches({ age: 25, hair: 'long', beard: true }, { hair: 'long', beard: true }); // true +matches({ hair: 'long', beard: true }, { age: 25, hair: 'long', beard: true }); // false +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + +### matchesWith + +Compares two objects to determine if the first one contains equivalent property values to the second one, based on a provided function. + +Use `Object.keys(source)` to get all the keys of the second object, then `Array.every()`, `Object.hasOwnProperty()` and the provided function to determine if all keys exist in the first object and have equivalent values. +If no function is provided, the values will be compared using the equality operator. + +```js +const matchesWith = (obj, source, fn) => + Object.keys(source).every( + key => + obj.hasOwnProperty(key) && fn + ? fn(obj[key], source[key], key, obj, source) + : obj[key] == source[key] + ); +``` + +
+Examples + +```js +const isGreeting = val => /^h(?:i|ello)$/.test(val); +matchesWith( + { greeting: 'hello' }, + { greeting: 'hi' }, + (oV, sV) => isGreeting(oV) && isGreeting(sV) +); // true +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + ### merge Creates a new object from the combination of two or more objects. diff --git a/docs/index.html b/docs/index.html index 147944c6e..ecf29f79d 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 ]
@@ -1031,6 +1031,23 @@ Foo.prototype: { user: 'pebbles', age: 1 }
 };
 mapValues(users, u => u.age); // { fred: 40, pebbles: 1 }
+

matches

Compares two objects to determine if the first one contains equivalent property values to the second one.

Use Object.keys(source) to get all the keys of the second object, then Array.every(), Object.hasOwnProperty() and strict comparison to determine if all keys exist in the first object and have the same values.

const matches = (obj, source) =>
+  Object.keys(source).every(key => obj.hasOwnProperty(key) && obj[key] === source[key]);
+
matches({ age: 25, hair: 'long', beard: true }, { hair: 'long', beard: true }); // true
+matches({ hair: 'long', beard: true }, { age: 25, hair: 'long', beard: true }); // false
+

matchesWith

Compares two objects to determine if the first one contains equivalent property values to the second one, based on a provided function.

Use Object.keys(source) to get all the keys of the second object, then Array.every(), Object.hasOwnProperty() and the provided function to determine if all keys exist in the first object and have equivalent values. If no function is provided, the values will be compared using the equality operator.

const matchesWith = (obj, source, fn) =>
+  Object.keys(source).every(
+    key =>
+      obj.hasOwnProperty(key) && fn
+        ? fn(obj[key], source[key], key, obj, source)
+        : obj[key] == source[key]
+  );
+
const isGreeting = val => /^h(?:i|ello)$/.test(val);
+matchesWith(
+  { greeting: 'hello' },
+  { greeting: 'hi' },
+  (oV, sV) => isGreeting(oV) && isGreeting(sV)
+); // true
 

merge

Creates a new object from the combination of two or more objects.

Use Array.reduce() combined with Object.keys(obj) to iterate over all objects and keys. Use hasOwnProperty() and Array.concat() to append values for keys existing in multiple objects.

const merge = (...objs) =>
   [...objs].reduce(
     (acc, obj) =>
diff --git a/snippets/matches.md b/snippets/matches.md
index 1be130fb0..47610d3d8 100644
--- a/snippets/matches.md
+++ b/snippets/matches.md
@@ -6,12 +6,10 @@ Use `Object.keys(source)` to get all the keys of the second object, then `Array.
 
 ```js
 const matches = (obj, source) =>
-  Object.keys(source).every(
-    key => obj.hasOwnProperty(key) && obj[key] === source[key]
-  );
+  Object.keys(source).every(key => obj.hasOwnProperty(key) && obj[key] === source[key]);
 ```
 
 ```js
-matches({ age: 25, hair: 'long', beard: true }, { hair: 'long', beard: true }) // true
-matches({ hair: 'long', beard: true }, { age: 25, hair: 'long', beard: true }) // false
+matches({ age: 25, hair: 'long', beard: true }, { hair: 'long', beard: true }); // true
+matches({ hair: 'long', beard: true }, { age: 25, hair: 'long', beard: true }); // false
 ```
diff --git a/snippets/matchesWith.md b/snippets/matchesWith.md
index e3777e780..a04814191 100644
--- a/snippets/matchesWith.md
+++ b/snippets/matchesWith.md
@@ -17,5 +17,9 @@ const matchesWith = (obj, source, fn) =>
 
 ```js
 const isGreeting = val => /^h(?:i|ello)$/.test(val);
-matchesWith({ greeting: 'hello' }, { greeting: 'hi' }, (oV, sV) => isGreeting(oV) && isGreeting(sV)); // true
+matchesWith(
+  { greeting: 'hello' },
+  { greeting: 'hi' },
+  (oV, sV) => isGreeting(oV) && isGreeting(sV)
+); // true
 ```