From 85f9fab1be6d063bea6b81feffb07e540c25c641 Mon Sep 17 00:00:00 2001 From: atomiks Date: Thu, 12 Apr 2018 00:29:40 +1000 Subject: [PATCH 1/2] fix: use constructor property instead of instanceof --- snippets/is.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/snippets/is.md b/snippets/is.md index bd7bb70b5..71c824930 100644 --- a/snippets/is.md +++ b/snippets/is.md @@ -1,11 +1,11 @@ ### 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` 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; ``` ```js @@ -16,10 +16,10 @@ 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 ``` From 272d65bd2d9514db9b5cac80b023a59f29193ed4 Mon Sep 17 00:00:00 2001 From: atomiks Date: Thu, 12 Apr 2018 15:20:50 +1000 Subject: [PATCH 2/2] Update is.md --- snippets/is.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/is.md b/snippets/is.md index 71c824930..7dc5dba30 100644 --- a/snippets/is.md +++ b/snippets/is.md @@ -2,7 +2,7 @@ Checks if the provided value is of the specified type. -Ensure the value is not `undefined` or `null` and compare the `constructor` property on the value with `type` 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) => ![, null].includes(val) && val.constructor === type;