https://github.com/enncy/custom-decorator
easier to create your javascript custom decorator
https://github.com/enncy/custom-decorator
custom-decorator decorator javascript reflect-metadata typescript
Last synced: 3 months ago
JSON representation
easier to create your javascript custom decorator
- Host: GitHub
- URL: https://github.com/enncy/custom-decorator
- Owner: enncy
- License: mit
- Created: 2024-01-22T08:53:25.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-01-25T09:12:09.000Z (over 1 year ago)
- Last Synced: 2025-02-27T07:29:27.293Z (3 months ago)
- Topics: custom-decorator, decorator, javascript, reflect-metadata, typescript
- Language: TypeScript
- Homepage:
- Size: 39.1 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
# custom-decorator
> `custom-decorator` making it easier for you to customize decorators to get/set reflect-metadata values
```sh
npm i custom-decorator
```## Example
```ts
import { CustomDecorator } from 'custom-decorator';// example
export function Decorator(val: string): ClassDecorator {
return (target) => {
CustomDecorator.setClassMetadata(Decorator, target, val);
};
}@Decorator('hello')
class A {}console.log(CustomDecorator.getClassMetadata(Decorator, A)); // hello
// simplify
function SimplifyDecorator(val: string) {
return CustomDecorator.classFactory(SimplifyDecorator, val);
}function TestMethod(val: string) {
return CustomDecorator.methodFactory(TestMethod, val);
}function TestParameter(num: number) {
return CustomDecorator.parameterFactory(TestParameter, num * 2);
}@SimplifyDecorator('hello')
class B {
@TestMethod('test')
test(@TestParameter(2) test: number) {}
}console.log(CustomDecorator.getClassMetadata(SimplifyDecorator, B)); // hello
console.log(CustomDecorator.getPropertyMetadata(TestMethod, new B(), 'test')); // test
console.log(CustomDecorator.getParameterMetadata(TestParameter, new B(), 'test', 0)); // 4
```**defineGetter** : define getter by decorator-value type mapping
```ts
import { CustomDecorator } from 'custom-decorator';/**
* type prompt
*/
const getter = CustomDecorator.defineGetter<{
TestMethod: [typeof TestMethod, string];
TestParameter: [typeof TestParameter, number];
}>();console.log(getter.TestMethod(new B(), 'test'));
// type: string
// value: 'test'
console.log(getter.TestParameter(new B(), 'test', 0));
// type: number
// value: 4
```