From 119c41d39bfdf598a3b235bf5f88f6f7adc5d5d7 Mon Sep 17 00:00:00 2001 From: Or Schneider Date: Sun, 24 Sep 2017 22:26:15 +0300 Subject: [PATCH] Add `is.class()` (#2) --- index.js | 4 +++- readme.md | 4 ++++ test.js | 12 ++++++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 132db41..78b4368 100644 --- a/index.js +++ b/index.js @@ -130,11 +130,13 @@ is.plainObject = x => { // eslint-disable-next-line no-return-assign return getObjectType(x) === 'Object' && (prototype = Object.getPrototypeOf(x), prototype === null || - prototype === Object.getPrototypeOf({})); + prototype === Object.getPrototypeOf({})); }; is.iterable = x => !is.null(x) && !is.undefined(x) && typeof x[Symbol.iterator] === 'function'; +is.class = x => typeof x === 'function' && x.toString().startsWith('class '); + const typedArrayTypes = new Set([ 'Int8Array', 'Uint8Array', diff --git a/readme.md b/readme.md index ee1761d..45ade28 100644 --- a/readme.md +++ b/readme.md @@ -115,6 +115,10 @@ JavaScript primitives are as follows: `null`, `undefined`, `string`, `number`, ` An object is plain if it's created by either `{}`, `new Object()`, or `Object.create(null)`. ##### .iterable(value) +##### .class(value) + +Returns `true` for instances created by a ES2015 class. + ##### .typedArray(value) diff --git a/test.js b/test.js index 7868637..357aa55 100644 --- a/test.js +++ b/test.js @@ -273,6 +273,18 @@ test('is.iterable', t => { t.false(m.iterable({})); }); +test('is.class', t => { + class Foo {} + const classDeclarations = [ + Foo, + class Bar extends Foo {} + ]; + + for (const x of classDeclarations) { + t.true(m.class(x)); + } +}); + test('is.typedArray', t => { const typedArrays = [ new Int8Array(),