tyDec is a decorator helper package in typescript. With Dec you can create decorators easily.
npm install tyDec
To run tests, run the following command
npm run test
Here how to use the package:
First of all, create an interface, if decorator has any parameter it should define here:
interface ShouldbeLessThan {
value: number;
}
The next step is to create a DecoratorDescriptor
class, this will have the implementation of the dectorator
class ShouldbeLessThanDecoratorDescriptor extends PropertyDecoratorDescriptor<ShouldbeLessThan> {
public implement(
target: any,
propertyKey: string,
arg: ShouldbeLessThan
) {
let value = target[propertyKey];
Object.defineProperty(target, propertyKey, {
set: newValue => {
if (newValue > arg.value) {
throw new Error(`value can not be greater than ${arg.max}`);
return;
}
value = newValue;
},
get: () => {
return value;
},
});
}
}
Finally create and export the decorator:
export const ShouldbeLessThan: IPropertyDecorator<ShouldbeLessThan> =
tyDec.create(ShouldbeLessThanDecoratorDescriptor);
export class SomeTestClass{
@ShouldbeLessThan({value:100})
someProperty:number;
}
Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.
We use SemVer for versioning. For the versions available, see the tags on this repository.
- MohamadArsalan Imamverdi - Initial work - ArsalanImamverdi
See also the list of contributors who participated in this project.
This project is licensed under the MIT License - see the LICENSE.md file for details