@wessberg/typedetector
Advanced tools
Comparing version 1.0.4 to 1.0.5
@@ -1,2 +0,2 @@ | ||
export declare type TypeOf = 'string' | 'object' | 'regexp' | 'number' | 'date' | 'symbol' | 'function' | 'boolean' | 'null' | 'undefined' | 'array' | 'set' | 'map' | 'weakset' | 'weakmap'; | ||
export declare type TypeOf = "string" | "object" | "regexp" | "number" | "date" | "symbol" | "function" | "boolean" | "null" | "undefined" | "array" | "set" | "map" | "weakset" | "weakmap" | "class" | "constructor"; | ||
export interface IArbitraryObject<T> { | ||
@@ -7,2 +7,4 @@ [key: string]: T; | ||
export interface ITypeDetector { | ||
isClassConstructor(item: any): item is Function; | ||
isClassInstance<T>(item: any): item is T; | ||
isBoolean(item: any): item is boolean; | ||
@@ -9,0 +11,0 @@ isString(item: any): item is string; |
@@ -1,2 +0,2 @@ | ||
import { IArbitraryObject, ITypeDetector, TypeOf } from './interface/ITypeDetector'; | ||
import { IArbitraryObject, ITypeDetector, TypeOf } from "./interface/ITypeDetector"; | ||
/** | ||
@@ -8,2 +8,15 @@ * A class that holds a set of predicate methods that detects the native type of the input. | ||
/** | ||
* Returns true if the given input is a class constructor.. | ||
* @param {?} item | ||
* @returns {Function} | ||
*/ | ||
isClassConstructor(item: any): item is new (args: any[]) => Function; | ||
/** | ||
* Returns true if the given input is a class. | ||
* @template T | ||
* @param {?} item | ||
* @returns {T} | ||
*/ | ||
isClassInstance<T>(item: any): item is T; | ||
/** | ||
* Returns true f the given item is a boolean. | ||
@@ -10,0 +23,0 @@ * @param {*} item |
@@ -9,2 +9,31 @@ "use strict"; | ||
/** | ||
* Returns true if the given input is a class constructor.. | ||
* @param {?} item | ||
* @returns {Function} | ||
*/ | ||
/* tslint:disable */ | ||
isClassConstructor(item) { | ||
/* tslint:enable */ | ||
if (!item || typeof item !== "function") | ||
return false; | ||
return item.toString().substring(0, 5) === "class"; | ||
} | ||
/** | ||
* Returns true if the given input is a class. | ||
* @template T | ||
* @param {?} item | ||
* @returns {T} | ||
*/ | ||
/* tslint:disable */ | ||
isClassInstance(item) { | ||
/* tslint:enable */ | ||
if (!item || this.isClassConstructor(item)) | ||
return false; | ||
const isCtorClass = item.constructor && item.constructor.toString().substring(0, 5) === "class"; | ||
if (item.prototype === undefined) | ||
return isCtorClass; | ||
const isPrototypeCtorClass = item.prototype.constructor && item.prototype.constructor.toString && item.prototype.constructor.toString().substring(0, 5) === "class"; | ||
return isCtorClass || isPrototypeCtorClass; | ||
} | ||
/** | ||
* Returns true f the given item is a boolean. | ||
@@ -48,2 +77,6 @@ * @param {*} item | ||
/* tslint:enable */ | ||
if (!item) | ||
return false; | ||
if (this.isClassInstance(item) || this.isClassConstructor(item)) | ||
return false; | ||
return item ? item.constructor === {}.constructor : false; | ||
@@ -79,2 +112,4 @@ } | ||
/* tslint:enable */ | ||
if (this.isClassInstance(item) || this.isClassConstructor(item)) | ||
return false; | ||
return !!(item && item.constructor && item.call && item.apply); | ||
@@ -135,2 +170,6 @@ } | ||
return "array"; | ||
if (this.isClassInstance(data)) | ||
return "class"; | ||
if (this.isClassConstructor(data)) | ||
return "constructor"; | ||
if (this.isObject(data)) | ||
@@ -137,0 +176,0 @@ return "object"; |
@@ -1,2 +0,2 @@ | ||
export declare type TypeOf = 'string' | 'object' | 'regexp' | 'number' | 'date' | 'symbol' | 'function' | 'boolean' | 'null' | 'undefined' | 'array' | 'set' | 'map' | 'weakset' | 'weakmap'; | ||
export declare type TypeOf = "string" | "object" | "regexp" | "number" | "date" | "symbol" | "function" | "boolean" | "null" | "undefined" | "array" | "set" | "map" | "weakset" | "weakmap" | "class" | "constructor"; | ||
export interface IArbitraryObject<T> { | ||
@@ -7,2 +7,4 @@ [key: string]: T; | ||
export interface ITypeDetector { | ||
isClassConstructor(item: any): item is Function; | ||
isClassInstance<T>(item: any): item is T; | ||
isBoolean(item: any): item is boolean; | ||
@@ -9,0 +11,0 @@ isString(item: any): item is string; |
@@ -1,2 +0,2 @@ | ||
import { IArbitraryObject, ITypeDetector, TypeOf } from './interface/ITypeDetector'; | ||
import { IArbitraryObject, ITypeDetector, TypeOf } from "./interface/ITypeDetector"; | ||
/** | ||
@@ -8,2 +8,15 @@ * A class that holds a set of predicate methods that detects the native type of the input. | ||
/** | ||
* Returns true if the given input is a class constructor.. | ||
* @param {?} item | ||
* @returns {Function} | ||
*/ | ||
isClassConstructor(item: any): item is new (args: any[]) => Function; | ||
/** | ||
* Returns true if the given input is a class. | ||
* @template T | ||
* @param {?} item | ||
* @returns {T} | ||
*/ | ||
isClassInstance<T>(item: any): item is T; | ||
/** | ||
* Returns true f the given item is a boolean. | ||
@@ -10,0 +23,0 @@ * @param {*} item |
@@ -7,2 +7,31 @@ /** | ||
/** | ||
* Returns true if the given input is a class constructor.. | ||
* @param {?} item | ||
* @returns {Function} | ||
*/ | ||
/* tslint:disable */ | ||
isClassConstructor(item) { | ||
/* tslint:enable */ | ||
if (!item || typeof item !== "function") | ||
return false; | ||
return item.toString().substring(0, 5) === "class"; | ||
} | ||
/** | ||
* Returns true if the given input is a class. | ||
* @template T | ||
* @param {?} item | ||
* @returns {T} | ||
*/ | ||
/* tslint:disable */ | ||
isClassInstance(item) { | ||
/* tslint:enable */ | ||
if (!item || this.isClassConstructor(item)) | ||
return false; | ||
const isCtorClass = item.constructor && item.constructor.toString().substring(0, 5) === "class"; | ||
if (item.prototype === undefined) | ||
return isCtorClass; | ||
const isPrototypeCtorClass = item.prototype.constructor && item.prototype.constructor.toString && item.prototype.constructor.toString().substring(0, 5) === "class"; | ||
return isCtorClass || isPrototypeCtorClass; | ||
} | ||
/** | ||
* Returns true f the given item is a boolean. | ||
@@ -46,2 +75,6 @@ * @param {*} item | ||
/* tslint:enable */ | ||
if (!item) | ||
return false; | ||
if (this.isClassInstance(item) || this.isClassConstructor(item)) | ||
return false; | ||
return item ? item.constructor === {}.constructor : false; | ||
@@ -77,2 +110,4 @@ } | ||
/* tslint:enable */ | ||
if (this.isClassInstance(item) || this.isClassConstructor(item)) | ||
return false; | ||
return !!(item && item.constructor && item.call && item.apply); | ||
@@ -133,2 +168,6 @@ } | ||
return "array"; | ||
if (this.isClassInstance(data)) | ||
return "class"; | ||
if (this.isClassConstructor(data)) | ||
return "constructor"; | ||
if (this.isObject(data)) | ||
@@ -135,0 +174,0 @@ return "object"; |
{ | ||
"name": "@wessberg/typedetector", | ||
"version": "1.0.4", | ||
"version": "1.0.5", | ||
"description": "A class that holds a set of predicate methods that detects the native type of the input.", | ||
@@ -5,0 +5,0 @@ "main": "./dist/cjs/index.js", |
@@ -30,2 +30,6 @@ # TypeDetector [![NPM version][npm-image]][npm-url] | ||
**v1.0.5**: | ||
- Added typedetection for class instances and class constructors. Added new 'getTypeOf' values. Classes no longer has the types 'function' for constructors and 'object' for instances, but rather 'constructor' for constructors and 'class' for class instances. | ||
**v1.0.4**: | ||
@@ -32,0 +36,0 @@ |
27338
716
51