Skip to content

Instantly share code, notes, and snippets.

@erajabzadeh
Created July 2, 2021 06:56
Show Gist options
  • Save erajabzadeh/612db4a62089f5cb615a2ac85e01aa56 to your computer and use it in GitHub Desktop.
Save erajabzadeh/612db4a62089f5cb615a2ac85e01aa56 to your computer and use it in GitHub Desktop.
ApplyToAll Decorator
const ApplyToAll = (decorator: Function, options: { exclude?: string[] } = {}) => {
return (target: any) => {
const descriptors = Object.getOwnPropertyDescriptors(target.prototype);
for (const [propName, descriptor] of Object.entries(descriptors)) {
const isMethod = (typeof descriptor.value == 'function') && propName != 'constructor';
if (options.exclude?.includes(propName)) continue;
if (!isMethod) continue;
decorator(target, propName, descriptor)
Object.defineProperty(target.prototype, propName, descriptor);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment