From b5d6f5a995187b2ac5ee871995bcf5655b162fd3 Mon Sep 17 00:00:00 2001 From: "kebler.net" Date: Sat, 29 May 2021 08:18:17 -0700 Subject: [PATCH] 0.6.4 add isEmiiter and isAysnc --- package.json | 2 +- src/check.js | 15 ++++++++++++++- test/check.test.js | 12 +++++++++++- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 5fbf89b..ea6e852 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@uci-utils/type", - "version": "0.6.3", + "version": "0.6.4", "description": "A variable type check and casting utility - extension of typechecker package plus type casting", "main": "src/index.js", "type": "module", diff --git a/src/check.js b/src/check.js index 6bfe4f1..5d876ed 100644 --- a/src/check.js +++ b/src/check.js @@ -21,13 +21,26 @@ customTypeChecker.isSymbol = function isSymbol(x) { customTypeChecker.isObservable = observable +customTypeChecker.isEmitter = function isEmitter(emitter) { + if (!emitter) return false + let check = ['on', 'emit'] + return check.reduce((acc, fn) => acc && typeof emitter[fn] === 'function', true) +} + +customTypeChecker.isAsync = function isAsync(fn) { + if (typeof fn !== 'function') return false + return (customTypeChecker.isPromise(fn) || fn.constructor.name === 'AsyncFunction') +} + // Add custom types to typeMap let customTypeMap = { buffer: customTypeChecker.isBuffer, promise: customTypeChecker.isPromise, symbol: customTypeChecker.isSymbol, - observable: customTypeChecker.isObservable + observable: customTypeChecker.isObservable, + emitter: customTypeChecker.isEmitter, + async: customTypeChecker.isAsync } customTypeMap = Object.assign(customTypeMap, customTypeChecker.typeMap) diff --git a/test/check.test.js b/test/check.test.js index ecd614a..d26f75d 100644 --- a/test/check.test.js +++ b/test/check.test.js @@ -1,17 +1,26 @@ import { expect } from 'chai' import u, { typeOf } from '../src/index.js' import { of } from 'rxjs' +import EE from 'events' console.log(of) describe('Type Check Library - ', function () { - it('Should include custom types', function () { + it('Should include custom types: buffer,promise,observable,emitter,async', function () { + const emitter = new EE() + const asyncfunc = async function () { } + expect(u.isBuffer(Buffer.from('this is a test'))).to.equal(true) expect(u.getType(Buffer.from('this is a test'))).to.equal('buffer') expect(u.isPromise(Promise.resolve(2))).to.equal(true) expect(u.getType(Promise.resolve(2))).to.equal('promise') expect(u.isObservable(of(1, 2, 3))).to.equal(true) expect(u.getType(of(1, 2, 3))).to.equal('observable') + expect(u.isEmitter(emitter)).to.equal(true) + expect(u.getType(emitter)).to.equal('emitter') + expect(u.isAsync(asyncfunc)).to.equal(true) + expect(u.getType(asyncfunc)).to.equal('async') + }) it('Should export typeOf for .getType', function () { @@ -22,6 +31,7 @@ describe('Type Check Library - ', function () { it('Should include base typechecker methods', function () { expect(u.isPlainObject([1])).to.equal(false) + expect(u.isPlainObject({ test: 'test' })).to.equal(true) expect(u.getType(true)).to.equal('boolean') expect(u.getObjectType('a string')).to.equal('[object String]') expect(u.getType('this is a test')).to.equal('string')