diff --git a/packages/falso/src/lib/address.ts b/packages/falso/src/lib/address.ts index 3d7779a69..8173c7e96 100644 --- a/packages/falso/src/lib/address.ts +++ b/packages/falso/src/lib/address.ts @@ -69,7 +69,7 @@ export function randAddress( return address; }; - return fake(factory, options, checkUnique); + return fake(factory, options, { uniqueComparer: checkUnique }); } const checkUnique: (address: Address, addresses: Address[]) => boolean = ( diff --git a/packages/falso/src/lib/between-date.ts b/packages/falso/src/lib/between-date.ts index 22f968957..97f4e4316 100644 --- a/packages/falso/src/lib/between-date.ts +++ b/packages/falso/src/lib/between-date.ts @@ -44,5 +44,5 @@ export function randBetweenDate( ); }; - return fake(generator, options, dateIsUnique); + return fake(generator, options, { uniqueComparer: dateIsUnique }); } diff --git a/packages/falso/src/lib/core/core.ts b/packages/falso/src/lib/core/core.ts index e04a4b846..547813a8f 100644 --- a/packages/falso/src/lib/core/core.ts +++ b/packages/falso/src/lib/core/core.ts @@ -6,6 +6,13 @@ export interface FakeOptions { priority?: 'length' | 'unique'; } +export interface FakeConfig { + uniqueComparer: + | ((item: T, items: T[], comparisonKeys?: string[]) => boolean) + | ((item: T, items: T[]) => boolean); + comparisonKeys?: string[]; +} + type Return = [O] extends [never] ? T : O['length'] extends number @@ -15,8 +22,9 @@ type Return = [O] extends [never] export function fake( data: T[] | (() => T), options?: Options, - uniqueComparer: (item: T, items: T[]) => boolean = primitiveValueIsUnique, - comparisonKeys?: string[] + config: FakeConfig = { + uniqueComparer: primitiveValueIsUnique, + } ): Return { if (options?.length === 0) { return [] as any; @@ -26,17 +34,12 @@ export function fake( return fakeFromArray(data, options) as any; } - return fakeFromFunction(data, uniqueComparer, comparisonKeys, options) as any; + return fakeFromFunction(data, config, options) as any; } export function fakeFromFunction( data: () => T, - isItemADuplicateFunction: ( - item: T, - items: T[], - comparisonKeys?: string[] - ) => boolean, - comparisonKeys?: string[], + config: FakeConfig, options?: Options ): T | T[] { if (!options?.length) { @@ -57,7 +60,7 @@ export function fakeFromFunction( while (items.length < options.length && attempts < maxAttempts) { const item = data(); - if (!isItemADuplicateFunction(item, items, comparisonKeys)) { + if (!config.uniqueComparer(item, items, config.comparisonKeys)) { items.push(item); } diff --git a/packages/falso/src/lib/credit-card.ts b/packages/falso/src/lib/credit-card.ts index af4b95381..92d405abe 100644 --- a/packages/falso/src/lib/credit-card.ts +++ b/packages/falso/src/lib/credit-card.ts @@ -86,7 +86,7 @@ export function randCreditCard( }; }; - return fake(factory, options, checkUnique); + return fake(factory, options, { uniqueComparer: checkUnique }); } const checkUnique: (card: CreditCard, cards: CreditCard[]) => boolean = ( diff --git a/packages/falso/src/lib/flight-details.ts b/packages/falso/src/lib/flight-details.ts index dd3670a3d..a447642cc 100644 --- a/packages/falso/src/lib/flight-details.ts +++ b/packages/falso/src/lib/flight-details.ts @@ -76,7 +76,7 @@ export function randFlightDetails( }; }; - return fake(factory, options, checkUnique); + return fake(factory, options, { uniqueComparer: checkUnique }); } const checkUnique: ( diff --git a/packages/falso/src/lib/future-date.ts b/packages/falso/src/lib/future-date.ts index e00ca5b3d..84e091a51 100644 --- a/packages/falso/src/lib/future-date.ts +++ b/packages/falso/src/lib/future-date.ts @@ -42,5 +42,5 @@ export function randFutureDate( const to = new Date(from.getTime() + yearsInMilliseconds); const factory: () => Date = () => randBetweenDate({ from, to }); - return fake(factory, options, dateIsUnique); + return fake(factory, options, { uniqueComparer: dateIsUnique }); } diff --git a/packages/falso/src/lib/json.ts b/packages/falso/src/lib/json.ts index 0053dd783..1bb7d75ca 100644 --- a/packages/falso/src/lib/json.ts +++ b/packages/falso/src/lib/json.ts @@ -96,7 +96,7 @@ export function randJSON( return generatedObject; }; - return fake(factory, options, checkUnique); + return fake(factory, options, { uniqueComparer: checkUnique }); } const checkUnique: (item: object, items: object[]) => boolean = ( diff --git a/packages/falso/src/lib/nearby-gpscoordinate.ts b/packages/falso/src/lib/nearby-gpscoordinate.ts index 0d3a883bf..f9115c7d6 100644 --- a/packages/falso/src/lib/nearby-gpscoordinate.ts +++ b/packages/falso/src/lib/nearby-gpscoordinate.ts @@ -23,7 +23,9 @@ import { randLongitude } from './longitude'; export function randNearbyGPSCoordinate( options?: Options ) { - return fake(() => [randLatitude(), randLongitude()], options, checkUnique); + return fake(() => [randLatitude(), randLongitude()], options, { + uniqueComparer: checkUnique, + }); } const checkUnique: (coordinate: number[], coordinates: number[][]) => boolean = diff --git a/packages/falso/src/lib/past-date.ts b/packages/falso/src/lib/past-date.ts index 64b0b0b3d..9898ad801 100644 --- a/packages/falso/src/lib/past-date.ts +++ b/packages/falso/src/lib/past-date.ts @@ -41,5 +41,7 @@ export function randPastDate( const to = new Date(); const from = new Date(to.getTime() - yearsInMilliseconds); - return fake(() => randBetweenDate({ from, to }), options, dateIsUnique); + return fake(() => randBetweenDate({ from, to }), options, { + uniqueComparer: dateIsUnique, + }); } diff --git a/packages/falso/src/lib/post.ts b/packages/falso/src/lib/post.ts index 98de50930..713395af7 100644 --- a/packages/falso/src/lib/post.ts +++ b/packages/falso/src/lib/post.ts @@ -50,5 +50,5 @@ export function randPost( return post; }; - return fake(factory, options, objectWithIdIsUnique); + return fake(factory, options, { uniqueComparer: objectWithIdIsUnique }); } diff --git a/packages/falso/src/lib/product.ts b/packages/falso/src/lib/product.ts index 16c6f0cbb..39fadb0a3 100644 --- a/packages/falso/src/lib/product.ts +++ b/packages/falso/src/lib/product.ts @@ -53,5 +53,5 @@ export function randProduct( }, }); - return fake(factory, options, objectWithIdIsUnique); + return fake(factory, options, { uniqueComparer: objectWithIdIsUnique }); } diff --git a/packages/falso/src/lib/recent-date.ts b/packages/falso/src/lib/recent-date.ts index 2290590fb..6f6802634 100644 --- a/packages/falso/src/lib/recent-date.ts +++ b/packages/falso/src/lib/recent-date.ts @@ -41,5 +41,7 @@ export function randRecentDate( const to = new Date(); const from = new Date(to.getTime() - daysInMilliseconds); - return fake(() => randBetweenDate({ from, to }), options, dateIsUnique); + return fake(() => randBetweenDate({ from, to }), options, { + uniqueComparer: dateIsUnique, + }); } diff --git a/packages/falso/src/lib/soon-date.ts b/packages/falso/src/lib/soon-date.ts index feb527901..9592361d3 100644 --- a/packages/falso/src/lib/soon-date.ts +++ b/packages/falso/src/lib/soon-date.ts @@ -40,5 +40,7 @@ export function randSoonDate( const daysInMilliseconds = days * 24 * 60 * 60 * 1000; const from = new Date(); const to = new Date(from.getTime() + daysInMilliseconds); - return fake(() => randBetweenDate({ from, to }), options, dateIsUnique); + return fake(() => randBetweenDate({ from, to }), options, { + uniqueComparer: dateIsUnique, + }); } diff --git a/packages/falso/src/lib/superhero.ts b/packages/falso/src/lib/superhero.ts index bf2b5cbba..f5577dbc2 100644 --- a/packages/falso/src/lib/superhero.ts +++ b/packages/falso/src/lib/superhero.ts @@ -55,5 +55,5 @@ export function randSuperhero( }; }; - return fake(factory, options, objectWithIdIsUnique); + return fake(factory, options, { uniqueComparer: objectWithIdIsUnique }); } diff --git a/packages/falso/src/lib/todo.ts b/packages/falso/src/lib/todo.ts index ab49d3881..d793bb1e0 100644 --- a/packages/falso/src/lib/todo.ts +++ b/packages/falso/src/lib/todo.ts @@ -37,5 +37,5 @@ export function randTodo( completed: randBoolean(), }); - return fake(factory, options, objectWithIdIsUnique); + return fake(factory, options, { uniqueComparer: objectWithIdIsUnique }); } diff --git a/packages/falso/src/lib/user.ts b/packages/falso/src/lib/user.ts index b3632ceb8..3dfcfb4af 100644 --- a/packages/falso/src/lib/user.ts +++ b/packages/falso/src/lib/user.ts @@ -63,5 +63,5 @@ export function randUser( return user; }; - return fake(factory, options, objectWithIdIsUnique); + return fake(factory, options, { uniqueComparer: objectWithIdIsUnique }); }