2.0.0 • Published 10 years ago
decorator-utils v2.0.0
decorator-utils

Helpful utility functions to use when writing your own ES7 decorators.
Installation
Install the package with NPM:
$ npm install decorator-utilsUsage
A simple example:
import {DecoratorUtils} from "decorator-utils";
function decorator(target, name, descriptor) {
let declarationType = DecoratorUtils.getDeclarationType(arguments);
if (declarationType !== DecoratorUtils.declarationTypes.CLASS_METHOD) {
throw new Error("Decorator must be applied to a class method declaration.");
}
// The rest of the decorator's logic...
}
@decorator // Error will be thrown.
class Dog {
@decorator // Error will NOT be thrown.
woof() {}
}API
The package exposes a static class, DecoratorUtils, which has the following functions and properties:
getDeclarationType()
- A function that can be called from within a decorator to determine the type of declaration that is being targeted. Useful for guarding a decorator against certain declaration types.
- Parameters:
- arguments - The
argumentsobject of the decorator function. Just pass it through!
- arguments - The
- Returns a value from
DecoratorUtils.declarationTypes.
createDecorator()
- A function that produces a decorator that can only be applied to a subset of "supported" declaration types; all "unsupported" ones will throw an error. Handy if you want to create decorators in a factory-like manner.
- Parameters:
- supportedDeclarationTypes - An array of values from
DecoratorUtils.declarationTypes. - fn - A function that contains the rest of the decorator's logic.
- supportedDeclarationTypes - An array of values from
- Returns a decorator (function).
declarationTypes
- A property describing the set of possible declaration types that a decorator can target, as key-value pairs of an object (
String->Symbol). To be used in conjunction withDecoratorUtils.getDeclarationType(). - Keys:
CLASSCLASS_METHODCLASS_ACCESSOROBJECT_LITERAL_METHODOBJECT_LITERAL_ACCESSOR