From c410b8b1c866d9caa9083d9102e097343f3ff4e6 Mon Sep 17 00:00:00 2001 From: TimMikeladze Date: Thu, 10 Aug 2023 21:13:28 -0700 Subject: [PATCH] fix: make noConsecutive digits option optional --- src/index.ts | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/src/index.ts b/src/index.ts index a11685a..28bea81 100644 --- a/src/index.ts +++ b/src/index.ts @@ -112,17 +112,25 @@ export const animal = word('animals'); export const cosmos = word('cosmos'); -// eslint-disable-next-line @typescript-eslint/no-unused-vars -export const digits = (count?: number) => (options: SpaceSlugOptions) => { - const set = new Set(); - - while (set.size < (count || 4)) { - const index = Math.floor(Math.random() * 10); - set.add(index.toString()); - } +export const digits = + // eslint-disable-next-line @typescript-eslint/no-unused-vars + (count?: number, noConsecutive?: boolean) => (options: SpaceSlugOptions) => { + const c = count || 4; + + const list: string[] = []; + + while (list.length < c) { + const value = Math.floor(Math.random() * 10).toString(); + const latest = list.length ? list[list.length - 1] : null; + if (noConsecutive && latest !== null && latest === value) { + // eslint-disable-next-line no-continue + continue; + } + list.push(value); + } - return Array.from(set).join(''); -}; + return list.map((x) => x.toString()).join(''); + }; export const cleanString = (inputString: string, separator: string) => inputString