[js][utility] add applyDecorators()

This commit is contained in:
方而静 2024-02-11 19:58:34 +08:00
parent 10cf8b2630
commit e5a51167f3
Signed by: szTom
GPG Key ID: 072D999D60C6473C

View File

@ -0,0 +1,10 @@
/**
* Applies an array of decorators to a given function in a right-to-left order.
*
* @param {Function[]} decorators - An array of decorator functions to apply.
* @param {Function} func - The original function to decorate.
* @returns {Function} - The decorated function after applying all the decorators.
*/
export function applyDecorators(decorators, func) {
return decorators.reduceRight((acc, dec) => dec(acc), func);
}