From 148d5e0e2c531f71f60df7f934ee52c0e9b32f4d Mon Sep 17 00:00:00 2001 From: Or Schneider Date: Sun, 24 Sep 2017 21:54:34 +0300 Subject: [PATCH] tests for is.class and readme info --- index.js | 3 ++- readme.md | 4 ++++ test.js | 9 +++++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 132db41..c4251e6 100644 --- a/index.js +++ b/index.js @@ -62,6 +62,7 @@ is.symbol = x => typeof x === 'symbol'; is.array = Array.isArray; is.function = x => typeof x === 'function'; +is.class = x => typeof x === 'function' && x.toString().startsWith('class '); is.buffer = Buffer.isBuffer; is.object = x => { @@ -130,7 +131,7 @@ 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'; diff --git a/readme.md b/readme.md index ee1761d..0bc3010 100644 --- a/readme.md +++ b/readme.md @@ -64,6 +64,10 @@ All the below methods accept a value and returns a boolean for whether the value #### Built-in types ##### .array(value) +##### .class(value) + +Returns `true` for any function that initialized as an ES6 class. + ##### .function(value) ##### .buffer(value) ##### .object(value) diff --git a/test.js b/test.js index 7868637..2da4e74 100644 --- a/test.js +++ b/test.js @@ -6,6 +6,8 @@ const isNode8orHigher = Number(process.versions.node.split('.')[0]) >= 8; const PromiseSubclassFixture = class extends Promise {}; const ErrorSubclassFixture = class extends Error {}; +const FooClassFixture = class Foo {}; +const BarClassFixture = class Bar extends FooClassFixture {}; const types = new Map([ ['undefined', undefined], @@ -28,6 +30,9 @@ const types = new Map([ [1, 2], new Array(2) ]], + ['class', [ + new BarClassFixture() + ]], ['function', [ function foo() {}, // eslint-disable-line func-names function () {}, @@ -130,6 +135,10 @@ test('is.array', t => { testType(t, 'array'); }); +test('is.class', t => { + t.true(types.get('class')[0] instanceof FooClassFixture); +}); + test('is.function', t => { testType(t, 'function'); });