0.0.19 • Published 1 year ago

custom-decorator v0.0.19

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

custom-decorator

custom-decorator making it easier for you to customize decorators to get/set reflect-metadata values

npm i custom-decorator

Example

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

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
0.0.17

1 year ago

0.0.18

1 year ago

0.0.19

1 year ago

0.0.10

1 year ago

0.0.11

1 year ago

0.0.12

1 year ago

0.0.13

1 year ago

0.0.14

1 year ago

0.0.15

1 year ago

0.0.9

1 year ago

0.0.16

1 year ago

0.0.8

1 year ago

0.0.7

1 year ago

0.0.6

1 year ago

0.0.5

1 year ago

0.0.4

1 year ago

0.0.3

1 year ago

0.0.2

1 year ago

0.0.1

1 year ago