diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..485dee64bcfb48793379b200a1afd14e85a8aaf4 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea diff --git a/dist/index.d.ts b/dist/index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..7f75c92d4d7a061ec3c04ef5a9cc3f445baaa67b --- /dev/null +++ b/dist/index.d.ts @@ -0,0 +1,10 @@ +export interface IDecorator { + (clazz: object, name: string, props: PropertyDescriptor): any; +} +export interface IDecoratorBuilder { + (input: T): IDecorator; +} +export interface IDynamicDecorator extends IDecorator, IDecoratorBuilder { + (clazz: object | T, name: string, props: PropertyDescriptor): any | IDecorator; +} +export declare function dynamicDecorator(defaultArg: T, builder: IDecoratorBuilder): IDynamicDecorator; diff --git a/dist/index.js b/dist/index.js new file mode 100644 index 0000000000000000000000000000000000000000..952dfabdc68b09806ea98bf0aa423ad8fedb5341 --- /dev/null +++ b/dist/index.js @@ -0,0 +1,15 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +function dynamicDecorator(defaultArg, builder) { + return function () { + const args = arguments; + if (args.length > 1) { + builder(defaultArg).apply(null, args); + } + else { + return builder(args[0]); + } + }; +} +exports.dynamicDecorator = dynamicDecorator; +//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi9saWIvaW5kZXgudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7QUFZQSxTQUFnQixnQkFBZ0IsQ0FBSSxVQUFhLEVBQUUsT0FBNkI7SUFDNUUsT0FBTztRQUNILE1BQU0sSUFBSSxHQUFHLFNBQVMsQ0FBQztRQUN2QixJQUFJLElBQUksQ0FBQyxNQUFNLEdBQUcsQ0FBQyxFQUFFO1lBQ2pCLE9BQU8sQ0FBQyxVQUFVLENBQUMsQ0FBQyxLQUFLLENBQUMsSUFBSSxFQUFFLElBQUksQ0FBQyxDQUFDO1NBQ3pDO2FBQU07WUFDSCxPQUFPLE9BQU8sQ0FBQyxJQUFJLENBQUMsQ0FBQyxDQUFDLENBQUMsQ0FBQztTQUMzQjtJQUNMLENBQUMsQ0FBQTtBQUNMLENBQUM7QUFURCw0Q0FTQyJ9 \ No newline at end of file diff --git a/lib/index.ts b/lib/index.ts new file mode 100644 index 0000000000000000000000000000000000000000..c3711baa6502c0dbc1ea4dd2bfc6fc45e14ce118 --- /dev/null +++ b/lib/index.ts @@ -0,0 +1,22 @@ +export interface IDecorator { + (clazz: object, name: string, props: PropertyDescriptor): any +} + +export interface IDecoratorBuilder { + (input: T): IDecorator +} + +export interface IDynamicDecorator extends IDecorator, IDecoratorBuilder { + (clazz: object | T, name: string, props: PropertyDescriptor): any | IDecorator; +} + +export function dynamicDecorator(defaultArg: T, builder: IDecoratorBuilder): IDynamicDecorator { + return function () { + const args = arguments; + if (args.length > 1) { + builder(defaultArg).apply(null, args); + } else { + return builder(args[0]); + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000000000000000000000000000000000000..6b5fad2881b8acc4f70b4d78bc81ac0fae006f52 --- /dev/null +++ b/package.json @@ -0,0 +1,17 @@ +{ + "name": "dynamic-decorator", + "version": "1.0.0", + "description": "create dynamic decorator", + "main": "dist/index.js", + "scripts": { + "test": "ts-node test.ts" + }, + "keywords": [ + "dynamic", + "decorator", + "TypeScript", + "ts" + ], + "author": "jerry4718", + "license": "ISC" +} diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000000000000000000000000000000000000..46ccbb60b2001e2d172bc545f5edc8ca6680add7 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,24 @@ +{ + "compileOnSave": true, + "compilerOptions": { + "module": "commonjs", + "experimentalDecorators": true, + "emitDecoratorMetadata": true, + "esModuleInterop": true, + "target": "es6", + "noImplicitAny": true, + "moduleResolution": "node", + "inlineSourceMap": true, + "outDir": "dist", + "baseUrl": ".", + "lib": [ + "es2017" + ], + "pretty": true, + "declaration": true, + "paths": {} + }, + "include": [ + "lib/**/*" + ] +} diff --git a/tslint.json b/tslint.json new file mode 100644 index 0000000000000000000000000000000000000000..49eec87926885a605278a6ad48fa05085391dc02 --- /dev/null +++ b/tslint.json @@ -0,0 +1,55 @@ +{ + "rules": { + "class-name": true, + "comment-format": [ + true, + "check-space" + ], + "indent": [ + true, + "spaces" + ], + "one-line": [ + true, + "check-open-brace", + "check-whitespace" + ], + "no-var-keyword": true, + "semicolon": [ + true, + "always", + "ignore-bound-class-methods" + ], + "whitespace": [ + true, + "check-branch", + "check-decl", + "check-operator", + "check-module", + "check-separator", + "check-type" + ], + "typedef-whitespace": [ + true, + { + "call-signature": "nospace", + "index-signature": "nospace", + "parameter": "nospace", + "property-declaration": "nospace", + "variable-declaration": "nospace" + }, + { + "call-signature": "onespace", + "index-signature": "onespace", + "parameter": "onespace", + "property-declaration": "onespace", + "variable-declaration": "onespace" + } + ], + "no-internal-module": true, + "no-trailing-whitespace": true, + "no-null-keyword": true, + "prefer-const": true, + "jsdoc-format": true + } +}