Compare commits
7 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
0b204fcced | ||
|
fba9e42c4e | ||
|
e81ce83303 | ||
|
036d91898d | ||
|
5e1b1c4be0 | ||
|
ec3602cbba | ||
|
8b3c97cfd6 |
@ -2,5 +2,10 @@
|
||||
|
||||
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
|
||||
|
||||
<a name="0.0.4"></a>
|
||||
## [0.0.4](https://git.loafle.net/overflow/core-js/compare/v0.0.2...v0.0.4) (2018-09-05)
|
||||
|
||||
|
||||
|
||||
<a name="0.0.2"></a>
|
||||
## 0.0.2 (2018-09-05)
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@overflow/core-js",
|
||||
"version": "0.0.2",
|
||||
"version": "0.0.11",
|
||||
"description": "TypeScript library setup for multiple compilation targets using tsc and webpack",
|
||||
"main": "./bundles/index.umd.js",
|
||||
"module": "./esm5/index.js",
|
||||
@ -55,9 +55,7 @@
|
||||
"maxSubjectLength": 120
|
||||
}
|
||||
},
|
||||
"peerDependencies": {
|
||||
"tslib": ">=1.9.0"
|
||||
},
|
||||
"peerDependencies": {},
|
||||
"dependencies": {
|
||||
"reflect-metadata": "^0.1.12"
|
||||
},
|
||||
|
@ -4,3 +4,17 @@ export class IllegalArgumentError extends Error {
|
||||
Object.setPrototypeOf(this, new.target.prototype);
|
||||
}
|
||||
}
|
||||
|
||||
export class NotSupportedDecoratorError extends Error {
|
||||
public constructor(message?: string) {
|
||||
super(message);
|
||||
Object.setPrototypeOf(this, new.target.prototype);
|
||||
}
|
||||
}
|
||||
|
||||
export class NotDecoratedClassError extends Error {
|
||||
public constructor(message?: string) {
|
||||
super(message);
|
||||
Object.setPrototypeOf(this, new.target.prototype);
|
||||
}
|
||||
}
|
||||
|
@ -5,7 +5,7 @@ export interface Type<T> extends Function {
|
||||
}
|
||||
|
||||
export declare type IdentityType<T> = T | symbol;
|
||||
export declare type PropertyKeyType = IdentityType<string | number>;
|
||||
export declare type PropertyKeyType = IdentityType<string>;
|
||||
export declare type MetadataKeyType = IdentityType<string>;
|
||||
|
||||
export enum PrimitiveType {
|
||||
@ -14,3 +14,12 @@ export enum PrimitiveType {
|
||||
NUMBER = 'number',
|
||||
BOOLEAN = 'boolean',
|
||||
}
|
||||
|
||||
export enum DecoratorType {
|
||||
CLASS = 'Clazz',
|
||||
PROPERTY = 'Property',
|
||||
METHOD = 'Method',
|
||||
PARAMETER = 'Parameter',
|
||||
}
|
||||
|
||||
export type DecoratorParametersType = [any, string | symbol, number | PropertyDescriptor | undefined];
|
||||
|
22
src/decorator/Decorator.ts
Normal file
22
src/decorator/Decorator.ts
Normal file
@ -0,0 +1,22 @@
|
||||
import {
|
||||
PropertyKeyType,
|
||||
} from '../core';
|
||||
|
||||
import {
|
||||
Annotation,
|
||||
} from '../reflect';
|
||||
|
||||
export interface Decorator<Attribute = {}> {
|
||||
classDecorator?: <TFunction extends Function>(target: TFunction) => TFunction | void;
|
||||
propertyDecorator?: (target: Object, propertyKey: PropertyKeyType) => void;
|
||||
methodDecorator?: <T>(target: Object, propertyKey: PropertyKeyType,
|
||||
descriptor: TypedPropertyDescriptor<T>) => TypedPropertyDescriptor<T> | void;
|
||||
parameterDecorator?: (target: Object, propertyKey: PropertyKeyType, parameterIndex: number) => void;
|
||||
}
|
||||
|
||||
export abstract class Decorator<Attribute = {}> extends Annotation<Attribute> {
|
||||
|
||||
public constructor(attribute?: Attribute) {
|
||||
super(attribute);
|
||||
}
|
||||
}
|
164
src/decorator/DecoratorHelper.ts
Normal file
164
src/decorator/DecoratorHelper.ts
Normal file
@ -0,0 +1,164 @@
|
||||
import {
|
||||
DecoratorParametersType,
|
||||
DecoratorType,
|
||||
NotSupportedDecoratorError,
|
||||
PropertyKeyType, Type,
|
||||
} from '../core';
|
||||
|
||||
import {
|
||||
Class,
|
||||
Constructor,
|
||||
Field,
|
||||
Metadata,
|
||||
Method,
|
||||
Parameter,
|
||||
} from '../reflect';
|
||||
|
||||
import {
|
||||
TypeUtil,
|
||||
} from '../util';
|
||||
|
||||
import { Decorator } from './Decorator';
|
||||
|
||||
export class DecoratorHelper {
|
||||
|
||||
public static register<Attribute = {}>(DecoratorClass: Type<Decorator<Attribute>>, attribute?: Attribute) {
|
||||
const annotation: Decorator<Attribute> = new DecoratorClass(attribute);
|
||||
const name: string = DecoratorClass.name;
|
||||
|
||||
return function (...decoratorArgs: any[]) {
|
||||
return DecoratorHelper.registerAnnotation(name, annotation, decoratorArgs);
|
||||
};
|
||||
}
|
||||
|
||||
public static create<Attribute = {}>(DecoratorClass: Type<Decorator<Attribute>>) {
|
||||
return function (attribute?: Attribute) {
|
||||
const annotation: Decorator<Attribute> = new DecoratorClass(attribute);
|
||||
const name: string = DecoratorClass.name;
|
||||
|
||||
return function (...decoratorArgs: any[]) {
|
||||
return DecoratorHelper.registerAnnotation(name, annotation, decoratorArgs);
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
private static registerAnnotation(name: string, annotation: any, decoratorArgs: any[]) {
|
||||
const decoratorType: DecoratorType = DecoratorHelper.getDecoratorType(decoratorArgs);
|
||||
|
||||
const [target, propertyKey, descriptorOrParameterIndex] = decoratorArgs;
|
||||
|
||||
const clazz: Class = Class._defineClass(TypeUtil.getType(target));
|
||||
let field: Field;
|
||||
let method: Method;
|
||||
let parameter: Parameter | undefined;
|
||||
let cons: Constructor | null = null;
|
||||
|
||||
switch (decoratorType) {
|
||||
case DecoratorType.CLASS:
|
||||
try {
|
||||
cons = clazz._defineConstructor(Metadata.getOwnParamTypes(target));
|
||||
clazz._addAnnotation(annotation);
|
||||
if (annotation instanceof Decorator && undefined !== annotation.classDecorator) {
|
||||
return annotation.classDecorator.call(annotation, target);
|
||||
}
|
||||
} catch (e) {
|
||||
if (e instanceof NotSupportedDecoratorError) {
|
||||
throw new NotSupportedDecoratorError(`Cannot apply @${name} decorator on Class.`);
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
break;
|
||||
case DecoratorType.PROPERTY:
|
||||
try {
|
||||
field = clazz._defineField(propertyKey, Metadata.getOwnType(target, propertyKey));
|
||||
field._addAnnotation(annotation);
|
||||
if (annotation instanceof Decorator && undefined !== annotation.propertyDecorator) {
|
||||
return annotation.propertyDecorator.call(annotation, target, propertyKey);
|
||||
}
|
||||
} catch (e) {
|
||||
if (e instanceof NotSupportedDecoratorError) {
|
||||
throw new NotSupportedDecoratorError(`Cannot apply @${name} decorator on Property.`);
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
break;
|
||||
case DecoratorType.METHOD:
|
||||
try {
|
||||
method = clazz._defineMethod(propertyKey,
|
||||
Metadata.getOwnParamTypes(target, propertyKey),
|
||||
Metadata.getOwnReturnType(target, propertyKey));
|
||||
method._addAnnotation(annotation);
|
||||
|
||||
if (annotation instanceof Decorator && undefined !== annotation.methodDecorator) {
|
||||
return annotation.methodDecorator.call(annotation, target, propertyKey, descriptorOrParameterIndex);
|
||||
}
|
||||
} catch (e) {
|
||||
if (e instanceof NotSupportedDecoratorError) {
|
||||
throw new NotSupportedDecoratorError(`Cannot apply @${name} decorator on Method.`);
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
break;
|
||||
case DecoratorType.PARAMETER:
|
||||
try {
|
||||
if (undefined === propertyKey) {
|
||||
cons = clazz.getConstructor();
|
||||
parameter = cons.getParameter(descriptorOrParameterIndex);
|
||||
} else {
|
||||
method = clazz._defineMethod(propertyKey,
|
||||
Metadata.getOwnParamTypes(target, propertyKey),
|
||||
Metadata.getOwnReturnType(target, propertyKey));
|
||||
parameter = method.getParameter(descriptorOrParameterIndex);
|
||||
}
|
||||
if (undefined !== parameter) {
|
||||
parameter._addAnnotation(annotation);
|
||||
}
|
||||
|
||||
if (annotation instanceof Decorator && undefined !== annotation.parameterDecorator) {
|
||||
return annotation.parameterDecorator.call(annotation, target, propertyKey, descriptorOrParameterIndex);
|
||||
}
|
||||
} catch (e) {
|
||||
if (e instanceof NotSupportedDecoratorError) {
|
||||
throw new NotSupportedDecoratorError(`Cannot apply @${name} decorator on Parameter.`);
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
throw new NotSupportedDecoratorError(`Cannot determine decorator[@${name}] type.`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {any[]} args
|
||||
* @returns {DecoratorType}
|
||||
*/
|
||||
public static getDecoratorType(args: any[]): DecoratorType {
|
||||
const [, propertyKey, descriptor] = args;
|
||||
|
||||
if (typeof descriptor === 'number') {
|
||||
return DecoratorType.PARAMETER;
|
||||
}
|
||||
|
||||
if (propertyKey && descriptor === undefined || descriptor && (descriptor.get || descriptor.set)) {
|
||||
return DecoratorType.PROPERTY;
|
||||
}
|
||||
|
||||
return (descriptor && descriptor.value) ? DecoratorType.METHOD : DecoratorType.CLASS;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param target
|
||||
* @param {string} propertyKey
|
||||
* @returns {DecoratorParametersType}
|
||||
*/
|
||||
public static decoratorArgs(target: any, propertyKey: PropertyKeyType): DecoratorParametersType {
|
||||
return [
|
||||
target,
|
||||
propertyKey,
|
||||
TypeUtil.descriptorOf(target, propertyKey),
|
||||
];
|
||||
}
|
||||
}
|
2
src/decorator/index.ts
Normal file
2
src/decorator/index.ts
Normal file
@ -0,0 +1,2 @@
|
||||
export * from './Decorator';
|
||||
export * from './DecoratorHelper';
|
@ -1,3 +1,4 @@
|
||||
export * from './core';
|
||||
export * from './decorator';
|
||||
export * from './reflect';
|
||||
export * from './util';
|
||||
|
@ -21,7 +21,7 @@ export abstract class AccessibleObject implements AnnotatedElement {
|
||||
}
|
||||
|
||||
public isAnnotationPresent<AnnotationType extends Annotation>(annotationClass: Type<AnnotationType>): boolean {
|
||||
return null !== this.getAnnotation(annotationClass);
|
||||
return undefined !== this.getAnnotation(annotationClass);
|
||||
}
|
||||
|
||||
public getOwnAnnotation<AnnotationType extends Annotation>(annotationClass: Type<AnnotationType>): AnnotationType | undefined {
|
||||
|
@ -1,4 +1,4 @@
|
||||
export abstract class Annotation<Attribute = {}> {
|
||||
export class Annotation<Attribute = {}> {
|
||||
public readonly attribute: Attribute | undefined;
|
||||
|
||||
public constructor(attribute?: Attribute) {
|
||||
|
@ -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<PropertyKeyType, Field> = 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<PropertyKeyType, Method> = 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<Type<any>, 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);
|
||||
|
@ -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';
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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<MetadataKeyType, any[]> = new Map<MetadataKeyType, any[]>();
|
||||
|
||||
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<MetadataKeyType, any[]> = new Map<MetadataKeyType, any[]>();
|
||||
|
32
src/util/ReflectionUtil.ts
Normal file
32
src/util/ReflectionUtil.ts
Normal file
@ -0,0 +1,32 @@
|
||||
import { Method } from '../reflect';
|
||||
|
||||
export abstract class ReflectionUtil {
|
||||
public static getParamTypes(method: Method): any[] {
|
||||
if (0 === method.getParameterCount()) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const parameters = method.getParameters();
|
||||
if (undefined === parameters) {
|
||||
return [];
|
||||
}
|
||||
const results: any[] = [];
|
||||
for (let indexI = 0; indexI < parameters.length; indexI++) {
|
||||
const paramType = parameters[indexI].getType();
|
||||
results.push(paramType);
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
public static getParamTypeStrings(method: Method): string[] {
|
||||
const paramTypes = ReflectionUtil.getParamTypes(method);
|
||||
|
||||
const results: string[] = [];
|
||||
paramTypes.forEach(paramType => {
|
||||
results.push(paramType.name);
|
||||
});
|
||||
|
||||
return results;
|
||||
}
|
||||
}
|
@ -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<any>}
|
||||
*/
|
||||
public static promiseTimeout(promise: Promise<any>, time = 1000): Promise<{ ok: boolean, response: any }> {
|
||||
const timeout = (p: Promise<any>, 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<any>, 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;
|
||||
|
@ -1,3 +1,4 @@
|
||||
export * from './AnnotationUtil';
|
||||
export * from './ReflectionUtil';
|
||||
export * from './Registry';
|
||||
export * from './TypeUtil';
|
@ -4,6 +4,8 @@
|
||||
"module": "esnext",
|
||||
"target": "es5",
|
||||
"types": [
|
||||
"jest",
|
||||
"node",
|
||||
"reflect-metadata",
|
||||
],
|
||||
"lib": [
|
||||
|
Loading…
x
Reference in New Issue
Block a user