Skip to content

enncy/custom-decorator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Jan 25, 2024
d58ea49 · Jan 25, 2024

History

42 Commits
Jan 22, 2024
Jan 25, 2024
Jan 25, 2024
Jan 22, 2024
Jan 22, 2024
Jan 22, 2024
Jan 22, 2024
Jan 25, 2024
Jan 25, 2024
Jan 23, 2024

Repository files navigation

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

Releases

No releases published

Packages

No packages published