refactor: cleanup default arguments handling
[poolifier.git] / src / utils.ts
index 20493767db87d80689a6cb067df3e22f72b759ce..3226e7eb3499af2fe2b1b6000962c83e14f10992 100644 (file)
@@ -291,12 +291,12 @@ export const buildInternalWorkerChoiceStrategyOptions = (
   opts?: InternalWorkerChoiceStrategyOptions
 ): InternalWorkerChoiceStrategyOptions => {
   opts = clone(opts ?? {})
-  if (opts.weights == null) {
+  if (opts?.weights == null) {
     opts.weights = getDefaultWeights(poolMaxSize)
   }
   return {
     ...getDefaultInternalWorkerChoiceStrategyOptions(
-      poolMaxSize + Object.keys(opts?.weights ?? {}).length
+      poolMaxSize + Object.keys(opts.weights).length
     ),
     ...opts
   }
@@ -304,8 +304,9 @@ export const buildInternalWorkerChoiceStrategyOptions = (
 
 const getDefaultWeights = (
   poolMaxSize: number,
-  defaultWorkerWeight: number = getDefaultWorkerWeight()
+  defaultWorkerWeight?: number
 ): Record<number, number> => {
+  defaultWorkerWeight = defaultWorkerWeight ?? getDefaultWorkerWeight()
   const weights: Record<number, number> = {}
   for (let workerNodeKey = 0; workerNodeKey < poolMaxSize; workerNodeKey++) {
     weights[workerNodeKey] = defaultWorkerWeight