diff --git a/README.md b/README.md index a7dbdf6ea..500988992 100644 --- a/README.md +++ b/README.md @@ -7551,12 +7551,12 @@ getType(new Set([1, 2, 3])); // 'set' ### is -Checks if the provided value is of the specified type (doesn't work with literals). +Checks if the provided value is of the specified type. -Use the `instanceof` operator to check if the provided value is of the specified `type`. +Ensure the value is not `undefined` or `null` using `Array.includes()`, and compare the `constructor` property on the value with `type` to check if the provided value is of the specified `type`. ```js -const is = (type, val) => val instanceof type; +const is = (type, val) => ![, null].includes(val) && val.constructor === type; ```
@@ -7570,11 +7570,11 @@ is(RegExp, /./g); // true is(Set, new Set()); // true is(WeakMap, new WeakMap()); // true is(WeakSet, new WeakSet()); // true -is(String, ''); // false +is(String, ''); // true is(String, new String('')); // true -is(Number, 1); // false +is(Number, 1); // true is(Number, new Number(1)); // true -is(Boolean, true); // false +is(Boolean, true); // true is(Boolean, new Boolean(true)); // true ``` diff --git a/docs/type.html b/docs/type.html index 2d59cf3e2..527b5d59e 100644 --- a/docs/type.html +++ b/docs/type.html @@ -82,7 +82,7 @@ }

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

 

Type

getType

Returns the native type of a value.

Returns lowercased constructor name of value, "undefined" or "null" if value is undefined or null.

const getType = v =>
   v === undefined ? 'undefined' : v === null ? 'null' : v.constructor.name.toLowerCase();
 
getType(new Set([1, 2, 3])); // 'set'
-

is

Checks if the provided value is of the specified type (doesn't work with literals).

Use the instanceof operator to check if the provided value is of the specified type.

const is = (type, val) => val instanceof type;
+

is

Checks if the provided value is of the specified type.

Ensure the value is not undefined or null using Array.includes(), and compare the constructor property on the value with type to check if the provided value is of the specified type.

const is = (type, val) => ![, null].includes(val) && val.constructor === type;
 
is(Array, [1]); // true
 is(ArrayBuffer, new ArrayBuffer()); // true
 is(Map, new Map()); // true
@@ -90,11 +90,11 @@
 is(Set, new Set()); // true
 is(WeakMap, new WeakMap()); // true
 is(WeakSet, new WeakSet()); // true
-is(String, ''); // false
+is(String, ''); // true
 is(String, new String('')); // true
-is(Number, 1); // false
+is(Number, 1); // true
 is(Number, new Number(1)); // true
-is(Boolean, true); // false
+is(Boolean, true); // true
 is(Boolean, new Boolean(true)); // true
 

isArrayLike

Checks if the provided argument is array-like (i.e. is iterable).

Use the spread operator (...) to check if the provided argument is iterable inside a try... catch block and the comma operator (,) to return the appropriate value.

const isArrayLike = val => {
   try {