is-any-type
Version:
is-any-type simple functionality alternative to check data type references such as typeof and instanceof
71 lines (70 loc) • 3.15 kB
JavaScript
import { Compare } from '../compare';
export class Type extends Compare {
constructor() {
super();
this.isTypeData = ['array', 'buffer', 'promise', 'function', 'object', 'null', 'undefined', 'string', 'number', 'boolean'];
}
isArray(data) {
this.isArrayCheck = Array.isArray(data) && 'array';
this.isMatch = this.isTypeData.indexOf(this.isArrayCheck);
this.isResult = this.isTypeData[this.isMatch];
return this.isResult === 'array' ? true : false;
}
isBuffer(data) {
this.isBufferCheck = Buffer.isBuffer(data) && 'buffer';
this.isMatch = this.isTypeData.indexOf(this.isBufferCheck);
this.isResult = this.isTypeData[this.isMatch];
return this.isResult === 'buffer' ? true : false;
}
isPromise(data) {
this.isPromiseCheck = data instanceof Promise && 'promise';
this.isMatch = this.isTypeData.indexOf(this.isPromiseCheck);
this.isResult = this.isTypeData[this.isMatch];
return this.isResult === 'promise' ? true : false;
}
isFunction(data) {
this.isFunctionCheck = data instanceof Function && 'function';
this.isMatch = this.isTypeData.indexOf(this.isFunctionCheck);
this.isResult = this.isTypeData[this.isMatch];
return this.isResult === 'function' ? true : false;
}
isObject(data) {
this.isObjectCheck =
Object.getOwnPropertyNames(data)[0] !== (undefined || null || 'length') || Object.create(data) === Object.create({})
? 'object'
: 'undefined';
this.isMatch = this.isTypeData.indexOf(this.isObjectCheck);
this.isResult = this.isTypeData[this.isMatch];
return this.isResult === 'object' ? true : false;
}
isNull(data) {
this.isNullCheck = data === null && 'null';
this.isMatch = this.isTypeData.indexOf(this.isNullCheck);
this.isResult = this.isTypeData[this.isMatch];
return this.isResult === 'null' ? true : false;
}
isUndefined(data) {
this.isUndefinedCheck = data === undefined && 'undefined';
this.isMatch = this.isTypeData.indexOf(this.isUndefinedCheck);
this.isResult = this.isTypeData[this.isMatch];
return this.isResult === 'undefined' ? true : false;
}
isString(data) {
this.isStringCheck = typeof data === 'string' && 'string';
this.isMatch = this.isTypeData.indexOf(this.isStringCheck);
this.isResult = this.isTypeData[this.isMatch];
return this.isResult === 'string' ? true : false;
}
isNumber(data) {
this.isNumberCheck = typeof data === 'number' && 'number';
this.isMatch = this.isTypeData.indexOf(this.isNumberCheck);
this.isResult = this.isTypeData[this.isMatch];
return this.isResult === 'number' ? true : false;
}
isBoolean(data) {
this.isBooleanCheck = typeof data === 'boolean' && 'boolean';
this.isMatch = this.isTypeData.indexOf(this.isBooleanCheck);
this.isResult = this.isTypeData[this.isMatch];
return this.isResult === 'boolean' ? true : false;
}
}