diff --git a/README.md b/README.md index 2c4d09bbf..e74ebf0d0 100644 --- a/README.md +++ b/README.md @@ -342,6 +342,7 @@ average(1, 2, 3); * [`isNull`](#isnull) * [`isNumber`](#isnumber) * [`isObject`](#isobject) +* [`isPlainObject`](#isplainobject) * [`isPrimitive`](#isprimitive) * [`isPromiseLike`](#ispromiselike) * [`isString`](#isstring) @@ -5632,6 +5633,29 @@ isObject(true); // false
[⬆ Back to top](#table-of-contents) +### isPlainObject + +Checks if the provided value is an bbject created by the Object constructor. + +Check if the provided value is truthy, use `typeof` to check if it is an object and `Object.constructor` to make sure the constructor is equal to `Object`. + +```js +const isPlainObject = val => !!val && typeof val === 'object' && val.constructor === Object; +``` + +
+Examples + +```js +isPlainObject({ a: 1 }); // true +isPlainObject(new Map()); // false +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + ### isPrimitive Returns a boolean determining if the passed value is primitive or not. diff --git a/docs/index.html b/docs/index.html index 3d3797a5f..431521070 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 ]
@@ -1271,6 +1271,9 @@ Foo.prototypeisObject({ a: 1 }); // true
 isObject({}); // true
 isObject(true); // false
+

isPlainObject

Checks if the provided value is an bbject created by the Object constructor.

Check if the provided value is truthy, use typeof to check if it is an object and Object.constructor to make sure the constructor is equal to Object.

const isPlainObject = val => !!val && typeof val === 'object' && val.constructor === Object;
+
isPlainObject({ a: 1 }); // true
+isPlainObject(new Map()); // false
 

isPrimitive

Returns a boolean determining if the passed value is primitive or not.

Use Array.includes() on an array of type strings which are not primitive, supplying the type using typeof. Since typeof null evaluates to 'object', it needs to be directly compared.

const isPrimitive = val => !['object', 'function'].includes(typeof val) || val === null;
 
isPrimitive(null); // true
 isPrimitive(50); // true
diff --git a/snippets/isPlainObject.md b/snippets/isPlainObject.md
index af27f16fe..c96027617 100644
--- a/snippets/isPlainObject.md
+++ b/snippets/isPlainObject.md
@@ -9,6 +9,6 @@ const isPlainObject = val => !!val && typeof val === 'object' && val.constructor
 ```
 
 ```js
-isPlainObject({ 'a': 1 }); // true
+isPlainObject({ a: 1 }); // true
 isPlainObject(new Map()); // false
 ```