diff --git a/package.json b/package.json index 3389901..096cb66 100644 --- a/package.json +++ b/package.json @@ -97,4 +97,4 @@ "validate-commit-msg": "^2.14.0", "webpack-config-utils": "^2.3.0" } -} +} \ No newline at end of file diff --git a/src/core/type.ts b/src/core/type.ts index 778a1a3..c80ecad 100644 --- a/src/core/type.ts +++ b/src/core/type.ts @@ -5,7 +5,7 @@ export interface Type extends Function { } export declare type IdentityType = T | symbol; -export declare type PropertyKeyType = IdentityType; +export declare type PropertyKeyType = IdentityType; export declare type MetadataKeyType = IdentityType; export enum PrimitiveType { diff --git a/src/reflect/AccessibleObject.ts b/src/reflect/AccessibleObject.ts index 2150163..151b3bb 100644 --- a/src/reflect/AccessibleObject.ts +++ b/src/reflect/AccessibleObject.ts @@ -21,7 +21,7 @@ export abstract class AccessibleObject implements AnnotatedElement { } public isAnnotationPresent(annotationClass: Type): boolean { - return null !== this.getAnnotation(annotationClass); + return undefined !== this.getAnnotation(annotationClass); } public getOwnAnnotation(annotationClass: Type): AnnotationType | undefined { diff --git a/src/reflect/Class.ts b/src/reflect/Class.ts index 29180d3..00175db 100644 --- a/src/reflect/Class.ts +++ b/src/reflect/Class.ts @@ -38,11 +38,9 @@ export class Class extends AccessibleObject { SystemClassRegistry.set(type, clazz); } - if (null === clazz._constructor) { + if (undefined === clazz._constructor) { const parameterTypes = Metadata.getOwnParamTypes(type); - if (undefined !== parameterTypes) { - clazz._constructor = new Constructor(clazz, parameterTypes); - } + clazz._constructor = new Constructor(clazz, parameterTypes); } return clazz; @@ -124,9 +122,6 @@ export class Class extends AccessibleObject { const fields: Map = new Map(); const types = TypeUtil.ancestorsOf(this._type); - if (null === types || 0 === types.length) { - return fields; - } for (let i = 0; i < types.length; i++) { const tType = types[i]; const tClazz = Class.forType(tType); @@ -160,9 +155,6 @@ export class Class extends AccessibleObject { const methods: Map = new Map(); const types = TypeUtil.ancestorsOf(this._type); - if (null === types || 0 === types.length) { - return methods; - } for (let i = 0; i < types.length; i++) { const tClazzType = types[i]; const tClazz = Class.forType(tClazzType); @@ -188,9 +180,6 @@ export class Class extends AccessibleObject { const annotations: Map, Annotation> = new Map(); const types = TypeUtil.ancestorsOf(this._type); - if (null === types || 0 === types.length) { - return annotations; - } for (let i = 0; i < types.length; i++) { const tClazzType = types[i]; const tClazz = Class.forType(tClazzType); diff --git a/src/reflect/Constructor.ts b/src/reflect/Constructor.ts index 50dc52b..f0f669e 100644 --- a/src/reflect/Constructor.ts +++ b/src/reflect/Constructor.ts @@ -5,6 +5,8 @@ import { Class } from './Class'; import { Executable } from './Executable'; +const CONSTRUCTOR_NAME = 'constructor'; + export class Constructor extends Executable { // private _rawConstructor: Function; @@ -19,5 +21,3 @@ export class Constructor extends Executable { return new (ctor.bind.apply(ctor, [null].concat(args)))(); } } - -const CONSTRUCTOR_NAME = 'constructor'; diff --git a/src/reflect/Executable.ts b/src/reflect/Executable.ts index c6ef735..0199642 100644 --- a/src/reflect/Executable.ts +++ b/src/reflect/Executable.ts @@ -49,10 +49,6 @@ export abstract class Executable extends AccessibleObject implements Member { * getParameterCount */ public getParameterCount(): number { - if (null === this._parameters) { - return 0; - } - return this._parameters.length; } /** @@ -65,9 +61,6 @@ export abstract class Executable extends AccessibleObject implements Member { * getParameter */ public getParameter(index: number): Parameter | undefined { - if (null === this._parameters) { - return undefined; - } if (0 > index || this._parameters.length <= index) { return undefined; } diff --git a/src/reflect/Metadata.ts b/src/reflect/Metadata.ts index 881df21..055ccf5 100644 --- a/src/reflect/Metadata.ts +++ b/src/reflect/Metadata.ts @@ -5,6 +5,31 @@ import { import { TypeUtil } from '../util/TypeUtil'; +/** + * Metadata key + * @private + * @type {string} + */ +const DESIGN_PARAM_TYPES = 'design:paramtypes'; +/** + * Metadata key + * @private + * @type {string} + */ +const DESIGN_TYPE = 'design:type'; +/** + * Metadata key + * @private + * @type {string} + */ +const DESIGN_RETURN_TYPE = 'design:returntype'; +/** + * Properties collections + * @private + * @type {string} + */ +const PROPERTIES: Map = new Map(); + export class Metadata { /** * Gets the metadata value for the provided metadata key on the target object or its prototype chain. @@ -504,28 +529,3 @@ export class Metadata { return Reflect.getOwnMetadata(DESIGN_PARAM_TYPES, target, propertyKey); } } - -/** - * Metadata key - * @private - * @type {string} - */ -const DESIGN_PARAM_TYPES = 'design:paramtypes'; -/** - * Metadata key - * @private - * @type {string} - */ -const DESIGN_TYPE = 'design:type'; -/** - * Metadata key - * @private - * @type {string} - */ -const DESIGN_RETURN_TYPE = 'design:returntype'; -/** - * Properties collections - * @private - * @type {string} - */ -const PROPERTIES: Map = new Map(); diff --git a/src/util/TypeUtil.ts b/src/util/TypeUtil.ts index a1467b2..4a7b761 100644 --- a/src/util/TypeUtil.ts +++ b/src/util/TypeUtil.ts @@ -4,6 +4,10 @@ import { Type, } from '../core'; +const COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg; +const DEFAULT_PARAMS = /=[^,]+/mg; +const FAT_ARROWS = /=>.*$/mg; + export class TypeUtil { /** * Get the provide constructor. @@ -151,7 +155,7 @@ export class TypeUtil { * @returns {boolean} */ public static isMethod(target: any, propertyKey: PropertyKeyType): boolean { - if (typeof(target[propertyKey]) === undefined) { + if (typeof target.propertyKey === 'undefined') { return false; } @@ -389,26 +393,27 @@ export class TypeUtil { * @param {number} time * @returns {Promise} */ - public static promiseTimeout(promise: Promise, time = 1000): Promise<{ ok: boolean, response: any }> { - const timeout = (p: Promise, t: number) => new Promise((resolve) => { - p.then((response) => { - resolve(); - - return response; - }); - setTimeout(() => resolve({ ok: false }), t); - }); - - promise = promise.then((response) => ({ ok: true, response })); + public static promiseTimeout(promise: Promise, timeout = 1000): Promise<{ ok: boolean, response?: any }> { + let _hTimeout: any; return Promise.race([ promise, - timeout(promise, time), + new Promise((resolve, reject) => { + promise.then(response => { + clearTimeout(_hTimeout); + + resolve(); + + return { ok: true, response }; + }).catch(reason => { + clearTimeout(_hTimeout); + reject(reason); + }); + + _hTimeout = setTimeout(() => { + resolve({ ok: false }); + }, timeout); + }), ]); } - } - -const COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg; -const DEFAULT_PARAMS = /=[^,]+/mg; -const FAT_ARROWS = /=>.*$/mg; diff --git a/src/util/index.ts b/src/util/index.ts index d289752..fc67c99 100644 --- a/src/util/index.ts +++ b/src/util/index.ts @@ -1,3 +1,3 @@ export * from './AnnotationUtil'; export * from './Registry'; -export * from './TypeUtil'; \ No newline at end of file +export * from './TypeUtil'; diff --git a/tsconfig.json b/tsconfig.json index 78b0be7..a3a84db 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -4,6 +4,8 @@ "module": "esnext", "target": "es5", "types": [ + "jest", + "node", "reflect-metadata", ], "lib": [