1.0.2 • Published 2 years ago

tydec v1.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

tyDec

tyDec is a decorator helper package in typescript. With Dec you can create decorators easily.

Installing

npm install tyDec

Running Tests

To run tests, run the following command

  npm run test

Documentation

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);

Usage

export class SomeTestClass{
  @ShouldbeLessThan({value:100})
  someProperty:number;
 }

Contributing

Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.

Versioning

We use SemVer for versioning. For the versions available, see the tags on this repository.

Authors

See also the list of contributors who participated in this project.

License

This project is licensed under the MIT License - see the LICENSE.md file for details