aurelia-metadata
Version:
Utilities for reading and writing the metadata of JavaScript functions.
47 lines (40 loc) • 1.38 kB
text/typescript
/**
* An object capable of applying it's captured decorators to a target.
*/
export interface DecoratorApplicator {
/**
* Applies the decorators to the target.
* @param target The target.
* @param key If applying to a method, the member name.
* @param descriptor If applying to a method, you may supply an initial descriptor to pass to the decorators.
*/
on(target: any, key?: string, descriptor?: PropertyDescriptor): any;
}
/**
* Enables applying decorators, particularly for use when there is no syntax support in the language, such as with ES5 and ES2016.
* @param rest The decorators to apply.
*/
export function decorators(...rest: Function[]): DecoratorApplicator {
const applicator = function(target, key, descriptor) {
let i = rest.length;
if (key) {
descriptor = descriptor || {
value: target[key],
writable: true,
configurable: true,
enumerable: true
};
while (i--) {
descriptor = rest[i](target, key, descriptor) || descriptor;
}
Object.defineProperty(target, key, descriptor);
} else {
while (i--) {
target = rest[i](target) || target;
}
}
return target;
} as unknown as DecoratorApplicator;
applicator.on = applicator as unknown as DecoratorApplicator['on'];
return applicator;
}