diff --git a/README.md b/README.md index 88959eb0..1b36faad 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ Create massive amounts of fake data in the browser and NodeJS. Tree Shakeable &

-✅  193 Functions +✅  192 Functions ✅  Tree Shakable ✅  Fully Typed ✅  Entity Functions diff --git a/docs/docs/getting-started.mdx b/docs/docs/getting-started.mdx index 9efc9309..bd00e6dc 100644 --- a/docs/docs/getting-started.mdx +++ b/docs/docs/getting-started.mdx @@ -13,7 +13,7 @@ title: Getting Started />

-✅  193 Functions +✅  192 Functions ✅  Tree Shakable ✅  Fully Typed ✅  Entity Functions diff --git a/packages/falso/src/lib/core/core.ts b/packages/falso/src/lib/core/core.ts index e62e30f9..5e4b0f96 100644 --- a/packages/falso/src/lib/core/core.ts +++ b/packages/falso/src/lib/core/core.ts @@ -41,11 +41,7 @@ export function fakeFromFunction( let attempts = 0; const maxAttempts = options.length * 2; - while (items.length < options.length) { - if (attempts >= maxAttempts) { - break; - } - + while (items.length < options.length && attempts < maxAttempts) { const item = data(); if (!items.includes(item)) { @@ -75,17 +71,17 @@ export function fakeFromArray( } const clonedData: T[] = JSON.parse(JSON.stringify(data)); - return Array.from({ length: options.length }, (_, index) => - randUniqueElement(clonedData) - ).filter((item) => item); -} + const newArray: T[] = []; -export function randUniqueElement(arr: T[]): T { - const index = Math.floor(random() * arr.length); - const item = arr[index]; - arr.splice(index, 1); + while (clonedData.length && newArray.length !== options.length) { + const randomIndex = getRandomInRange({ min: clonedData.length }); + const item = clonedData[randomIndex]; + + newArray.push(item); + clonedData.splice(randomIndex, 1); + } - return item; + return newArray; } export function randElement(arr: T[]): T {