From 128e0e32b4d9f8b61c83f9681a25d0dfc834fd6a Mon Sep 17 00:00:00 2001 From: Jacek Fiszer Date: Thu, 19 Dec 2024 23:19:33 +0100 Subject: [PATCH] fix(random): refactor `alpha` function to resolve failing test --- lib/random.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/random.ts b/lib/random.ts index 04f2bd9fb..c3bfcc8af 100644 --- a/lib/random.ts +++ b/lib/random.ts @@ -272,17 +272,19 @@ class Random { upcase?: boolean; } | number, ): string => { - if (typeof options === "number") options = { count: options }; + // Extract options with safe defaults + const { count = 1, upcase = false } = typeof options === "number" + ? { count: options } + : options || {}; - options = options || { count: 1, upcase: false }; const letters = "abcdefghijklmnopqrstuvwxyz"; let wholeString = ""; - for (let i = 0; i < options.count!; i++) { + for (let i = 0; i < count; i++) { wholeString += this.faker.random.arrayElement(letters.split("")); } - return options.upcase ? wholeString.toUpperCase() : wholeString; + return upcase ? wholeString.toUpperCase() : wholeString; }; /**