From 3dece0904933f9632afabdacd0f2b5b32d8bde2a Mon Sep 17 00:00:00 2001 From: Matt Mayer <152770+matthewmayer@users.noreply.github.com> Date: Thu, 27 Jul 2023 12:16:21 -0400 Subject: fix(helpers): prevent uniqueArray from hanging (#2239) --- src/modules/helpers/index.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/modules/helpers/index.ts b/src/modules/helpers/index.ts index 92647942..28c63693 100644 --- a/src/modules/helpers/index.ts +++ b/src/modules/helpers/index.ts @@ -649,6 +649,11 @@ export class HelpersModule { * and outputs a unique array of strings based on that source. * This method does not store the unique state between invocations. * + * If there are not enough unique values to satisfy the length, if + * the source is an array, it will only return as many items as are + * in the array. If the source is a function, it will return after + * a maximum number of attempts has been reached. + * * @template T The type of the elements. * * @param source The strings to choose from or a function that generates a string. @@ -671,8 +676,11 @@ export class HelpersModule { const set = new Set(); try { if (typeof source === 'function') { - while (set.size < length) { + const maxAttempts = 1000 * length; + let attempts = 0; + while (set.size < length && attempts < maxAttempts) { set.add(source()); + attempts++; } } } catch { -- cgit v1.2.3