From e5a51167f36d634f2b461e8fcd70b11533245193 Mon Sep 17 00:00:00 2001 From: szdytom Date: Sun, 11 Feb 2024 19:58:34 +0800 Subject: [PATCH] [js][utility] add applyDecorators() --- shared/utility/decorator.mjs | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 shared/utility/decorator.mjs diff --git a/shared/utility/decorator.mjs b/shared/utility/decorator.mjs new file mode 100644 index 0000000..6cffe45 --- /dev/null +++ b/shared/utility/decorator.mjs @@ -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); +}