Skip to content

Commit

Permalink
feat: πŸ”₯ Replace duplicate checkUnique logic with objectIsUnique
Browse files Browse the repository at this point in the history
βœ… Closes: #220
  • Loading branch information
Ryan Smee committed Apr 4, 2022
1 parent 6833592 commit 13998f6
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 17 deletions.
10 changes: 5 additions & 5 deletions packages/falso/src/lib/address.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FakeOptions, fake } from './core/core';
import { FakeOptions, fake, objectIsUnique } from './core/core';
import { randCity } from './city';
import { randStreetAddress } from './street-address';
import { randZipCode } from './zip-code';
Expand Down Expand Up @@ -67,7 +67,7 @@ export function randAddress<Options extends AddressOptions = never>(
return fake(factory, options, checkUnique);
}

const checkUnique: (item: Address, items: Address[]) => boolean = (
item: Address,
items: Address[]
) => items.some((i) => i.street + i.zipCode === item.street + item.zipCode);
const checkUnique: (address: Address, addresses: Address[]) => boolean = (
address: Address,
addresses: Address[]
) => objectIsUnique(address, addresses, ['street', 'zipCode']);
2 changes: 1 addition & 1 deletion packages/falso/src/lib/core/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export const dateIsUnique: (date: Date, dates: Date[]) => boolean = (
export const checkUniqueObjectWithId: <T extends { id: string }>(
item: T,
items: T[]
) => boolean = (item, items) => items.some((i) => i.id === item.id);
) => boolean = (item, items) => objectIsUnique(item, items, ['id']);

export const objectIsUnique: (
item: any,
Expand Down
9 changes: 5 additions & 4 deletions packages/falso/src/lib/credit-card.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { fake, FakeOptions } from './core/core';
import { fake, FakeOptions, objectIsUnique } from './core/core';
import { randCreditCardCVV } from './credit-card-cvv';
import { randCreditCardBrand } from './credit-card-brand';
import { Brand, randCreditCardNumber } from './credit-card-number';
Expand All @@ -8,6 +8,7 @@ import { rand } from './rand';
import { randPastDate } from './past-date';
import { randFutureDate } from './future-date';
import { randPersonTitle } from './person-title';
import { Address } from './address';

export interface CreditCardOptions extends FakeOptions {
fullName?: string;
Expand Down Expand Up @@ -85,6 +86,6 @@ export function randCreditCard<Options extends CreditCardOptions = never>(
}

const checkUnique: (card: CreditCard, cards: CreditCard[]) => boolean = (
card,
cards
) => cards.some((c) => c.number === card.number);
card: CreditCard,
cards: CreditCard[]
) => objectIsUnique(card, cards, ['number']);
15 changes: 8 additions & 7 deletions packages/falso/src/lib/flight-details.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { fake, FakeOptions, getRandomInRange } from './core/core';
import {
fake,
FakeOptions,
getRandomInRange,
objectIsUnique,
} from './core/core';
import { randFutureDate } from './future-date';
import { randAirline } from './airline';
import { Airline, randFlightNumber } from './flight-number';
Expand Down Expand Up @@ -78,9 +83,5 @@ export function randFlightDetails<Options extends FlightDetailsOptions = never>(
const checkUnique: (
flight: FlightDetails,
flights: FlightDetails[]
) => boolean = (flight, flights) =>
flights.some(
(f) =>
f.passenger + f.flightNumber + f.date ===
flight.passenger + flight.flightNumber + flight.date
);
) => boolean = (flight: FlightDetails, flights: FlightDetails[]) =>
objectIsUnique(flight, flights, ['passenger', 'flightNumber', 'date']);

0 comments on commit 13998f6

Please sign in to comment.