Created
July 2, 2021 06:56
-
-
Save erajabzadeh/612db4a62089f5cb615a2ac85e01aa56 to your computer and use it in GitHub Desktop.
ApplyToAll Decorator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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