From 0628df39eb1eb95630d08d9759a83c750a34ff7e Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sun, 27 Aug 2023 23:27:47 +0200 Subject: [PATCH] refactor: factor out task function validation MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/worker/abstract-worker.ts | 37 +++++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/src/worker/abstract-worker.ts b/src/worker/abstract-worker.ts index 23c661c9..c00cf083 100644 --- a/src/worker/abstract-worker.ts +++ b/src/worker/abstract-worker.ts @@ -103,6 +103,27 @@ export abstract class AbstractWorker< delete this.opts.async } + private checkValidTaskFunction ( + name: string, + fn: TaskFunction + ): void { + if (typeof name !== 'string') { + throw new TypeError( + 'A taskFunctions parameter object key is not a string' + ) + } + if (typeof name === 'string' && name.trim().length === 0) { + throw new TypeError( + 'A taskFunctions parameter object key is an empty string' + ) + } + if (typeof fn !== 'function') { + throw new TypeError( + 'A taskFunctions parameter object value is not a function' + ) + } + } + /** * Checks if the `taskFunctions` parameter is passed to the constructor. * @@ -128,21 +149,7 @@ export abstract class AbstractWorker< } else if (isPlainObject(taskFunctions)) { let firstEntry = true for (const [name, fn] of Object.entries(taskFunctions)) { - if (typeof name !== 'string') { - throw new TypeError( - 'A taskFunctions parameter object key is not a string' - ) - } - if (typeof name === 'string' && name.trim().length === 0) { - throw new TypeError( - 'A taskFunctions parameter object key is an empty string' - ) - } - if (typeof fn !== 'function') { - throw new TypeError( - 'A taskFunctions parameter object value is not a function' - ) - } + this.checkValidTaskFunction(name, fn) const boundFn = fn.bind(this) if (firstEntry) { this.taskFunctions.set(DEFAULT_TASK_NAME, boundFn) -- 2.34.1