Skip to content

Commit

Permalink
feat: πŸ”₯ Combine uniqueComparer and comparisonKeys into config
Browse files Browse the repository at this point in the history
βœ… Closes: #220

feat: πŸ”₯ Combine uniqueComparer and comparisonKeys into config

βœ… Closes: #220
  • Loading branch information
Ryan Smee committed May 10, 2022
1 parent dbddca3 commit 2331772
Show file tree
Hide file tree
Showing 16 changed files with 38 additions and 25 deletions.
2 changes: 1 addition & 1 deletion packages/falso/src/lib/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export function randAddress<Options extends AddressOptions = never>(
return address;
};

return fake(factory, options, checkUnique);
return fake(factory, options, { uniqueComparer: checkUnique });
}

const checkUnique: (address: Address, addresses: Address[]) => boolean = (
Expand Down
2 changes: 1 addition & 1 deletion packages/falso/src/lib/between-date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,5 @@ export function randBetweenDate<Options extends BetweenOptions = never>(
);
};

return fake(generator, options, dateIsUnique);
return fake(generator, options, { uniqueComparer: dateIsUnique });
}
25 changes: 15 additions & 10 deletions packages/falso/src/lib/core/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ export interface FakeOptions {
priority?: 'length' | 'unique';
}

export interface FakeConfig<T> {
uniqueComparer: (
item: T,
items: T[],
comparisonKeys?: string[]
) => boolean | ((item: T, items: T[]) => boolean);
comparisonKeys?: string[];
}

type Return<T, O extends FakeOptions> = [O] extends [never]
? T
: O['length'] extends number
Expand All @@ -15,8 +24,9 @@ type Return<T, O extends FakeOptions> = [O] extends [never]
export function fake<T, Options extends FakeOptions>(
data: T[] | (() => T),
options?: Options,
uniqueComparer: (item: T, items: T[]) => boolean = primitiveValueIsUnique,
comparisonKeys?: string[]
config: FakeConfig<T> = {
uniqueComparer: primitiveValueIsUnique,
}
): Return<T, Options> {
if (options?.length === 0) {
return [] as any;
Expand All @@ -26,17 +36,12 @@ export function fake<T, Options extends FakeOptions>(
return fakeFromArray(data, options) as any;
}

return fakeFromFunction(data, uniqueComparer, comparisonKeys, options) as any;
return fakeFromFunction(data, config, options) as any;
}

export function fakeFromFunction<T, Options extends FakeOptions>(
data: () => T,
isItemADuplicateFunction: (
item: T,
items: T[],
comparisonKeys?: string[]
) => boolean,
comparisonKeys?: string[],
config: FakeConfig<T>,
options?: Options
): T | T[] {
if (!options?.length) {
Expand All @@ -57,7 +62,7 @@ export function fakeFromFunction<T, Options extends FakeOptions>(
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);
}

Expand Down
2 changes: 1 addition & 1 deletion packages/falso/src/lib/credit-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export function randCreditCard<Options extends CreditCardOptions = never>(
};
};

return fake(factory, options, checkUnique);
return fake(factory, options, { uniqueComparer: checkUnique });
}

const checkUnique: (card: CreditCard, cards: CreditCard[]) => boolean = (
Expand Down
2 changes: 1 addition & 1 deletion packages/falso/src/lib/flight-details.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export function randFlightDetails<Options extends FlightDetailsOptions = never>(
};
};

return fake(factory, options, checkUnique);
return fake(factory, options, { uniqueComparer: checkUnique });
}

const checkUnique: (
Expand Down
2 changes: 1 addition & 1 deletion packages/falso/src/lib/future-date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,5 @@ export function randFutureDate<Options extends FutureOptions = never>(
const to = new Date(from.getTime() + yearsInMilliseconds);
const factory: () => Date = () => randBetweenDate({ from, to });

return fake(factory, options, dateIsUnique);
return fake(factory, options, { uniqueComparer: dateIsUnique });
}
2 changes: 1 addition & 1 deletion packages/falso/src/lib/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export function randJSON<Options extends RandomJSONOptions = never>(
return generatedObject;
};

return fake(factory, options, checkUnique);
return fake(factory, options, { uniqueComparer: checkUnique });
}

const checkUnique: (item: object, items: object[]) => boolean = (
Expand Down
4 changes: 3 additions & 1 deletion packages/falso/src/lib/nearby-gpscoordinate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ import { randLongitude } from './longitude';
export function randNearbyGPSCoordinate<Options extends FakeOptions = never>(
options?: Options
) {
return fake(() => [randLatitude(), randLongitude()], options, checkUnique);
return fake(() => [randLatitude(), randLongitude()], options, {
uniqueComparer: checkUnique,
});
}

const checkUnique: (coordinate: number[], coordinates: number[][]) => boolean =
Expand Down
4 changes: 3 additions & 1 deletion packages/falso/src/lib/past-date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,7 @@ export function randPastDate<Options extends PastOptions = never>(
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,
});
}
2 changes: 1 addition & 1 deletion packages/falso/src/lib/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,5 @@ export function randPost<Options extends FakeOptions = never>(
return post;
};

return fake(factory, options, objectWithIdIsUnique);
return fake(factory, options, { uniqueComparer: objectWithIdIsUnique });
}
2 changes: 1 addition & 1 deletion packages/falso/src/lib/product.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,5 @@ export function randProduct<Options extends FakeOptions = never>(
},
});

return fake(factory, options, objectWithIdIsUnique);
return fake(factory, options, { uniqueComparer: objectWithIdIsUnique });
}
4 changes: 3 additions & 1 deletion packages/falso/src/lib/recent-date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,7 @@ export function randRecentDate<Options extends RecentOptions = never>(
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,
});
}
4 changes: 3 additions & 1 deletion packages/falso/src/lib/soon-date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,7 @@ export function randSoonDate<Options extends SoonOptions = never>(
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,
});
}
2 changes: 1 addition & 1 deletion packages/falso/src/lib/superhero.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,5 @@ export function randSuperhero<Options extends SuperheroOptions = never>(
};
};

return fake(factory, options, objectWithIdIsUnique);
return fake(factory, options, { uniqueComparer: objectWithIdIsUnique });
}
2 changes: 1 addition & 1 deletion packages/falso/src/lib/todo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,5 @@ export function randTodo<Options extends FakeOptions = never>(
completed: randBoolean(),
});

return fake(factory, options, objectWithIdIsUnique);
return fake(factory, options, { uniqueComparer: objectWithIdIsUnique });
}
2 changes: 1 addition & 1 deletion packages/falso/src/lib/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,5 @@ export function randUser<Options extends FakeOptions = never>(
return user;
};

return fake(factory, options, objectWithIdIsUnique);
return fake(factory, options, { uniqueComparer: objectWithIdIsUnique });
}

0 comments on commit 2331772

Please sign in to comment.