diff --git a/index.js b/index.js index 716421b..2019ec7 100644 --- a/index.js +++ b/index.js @@ -134,11 +134,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 c1cdac8..e63a2e6 100644 --- a/readme.md +++ b/readme.md @@ -118,6 +118,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 e290e8c..0b9e486 100644 --- a/test.js +++ b/test.js @@ -287,6 +287,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(),