Skip to content

Commit

Permalink
feat: πŸ”₯ Add custom comparison functions non-conforming function
Browse files Browse the repository at this point in the history
βœ… Closes: #220
  • Loading branch information
theryansmee committed Mar 23, 2022
1 parent 7f69bad commit 77831a5
Show file tree
Hide file tree
Showing 15 changed files with 88 additions and 29 deletions.
9 changes: 8 additions & 1 deletion packages/falso/src/lib/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,12 @@ export function randAddress<Options extends AddressOptions = never>(
return address;
};

return fake(factory, options);
return fake(factory, options, comparisonFunction);
}

const comparisonFunction: (item: Address, items: Address[]) => boolean = (
item: Address,
items: Address[]
) => {
return items.some((i) => i.street + i.zipCode === item.street + item.zipCode);
};
4 changes: 2 additions & 2 deletions packages/falso/src/lib/between-date.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { fake, FakeOptions } from './core/core';
import { dateComparisonFunction, fake, FakeOptions } from './core/core';
import { randNumber } from './number';

interface BetweenOptions extends FakeOptions {
Expand Down Expand Up @@ -39,5 +39,5 @@ export function randBetweenDate<Options extends BetweenOptions = never>(
);
};

return fake(generator, options);
return fake(generator, options, dateComparisonFunction);
}
6 changes: 6 additions & 0 deletions packages/falso/src/lib/core/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ export const objectWithIdComparisonFunction: <T extends { id: string }>(
) => boolean = (item, items) => {
return items.some((i) => i.id === item.id);
};
export const dateComparisonFunction: (date: Date, dates: Date[]) => boolean = (
date,
dates
) => {
return dates.some((d) => d.valueOf() === date.valueOf());
};

export function randElement<T>(arr: T[]): T {
return arr[Math.floor(random() * arr.length)];
Expand Down
9 changes: 8 additions & 1 deletion packages/falso/src/lib/credit-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,12 @@ export function randCreditCard<Options extends CreditCardOptions = never>(
};
};

return fake(factory, options);
return fake(factory, options, comparisonFunction);
}

export const comparisonFunction: (
card: CreditCard,
cards: CreditCard[]
) => boolean = (card, cards) => {
return cards.some((c) => c.number === card.number);
};
12 changes: 12 additions & 0 deletions packages/falso/src/lib/flight-details.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Airline, randFlightNumber } from './flight-number';
import { randFullName } from './full-name';
import { randSeatNumber } from './seat-number';
import { Airport, randAirport } from './airport';
import { CreditCard } from './credit-card';

export interface FlightDetailsOptions extends FakeOptions {
airline?: Airline;
Expand Down Expand Up @@ -73,3 +74,14 @@ export function randFlightDetails<Options extends FlightDetailsOptions = never>(

return fake(factory, options);
}

export const comparisonFunction: (
flight: FlightDetails,
flights: FlightDetails[]
) => boolean = (flight, flights) => {
return flights.some(
(f) =>
f.passenger + f.flightNumber + f.date ===
flight.passenger + flight.flightNumber + flight.date
);
};
2 changes: 0 additions & 2 deletions packages/falso/src/lib/float.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,3 @@ export function randFloat<Options extends RandomFloatOptions = never>(
};
return fake(() => getRandomInRange(o), options);
}

randFloat();
4 changes: 2 additions & 2 deletions packages/falso/src/lib/full-address.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FakeOptions, fake } from './core/core';
import { fake } from './core/core';
import { AddressOptions, randAddress } from './address';

/**
Expand Down Expand Up @@ -29,7 +29,7 @@ export function randFullAddress<Options extends AddressOptions = never>(
const includeCounty: boolean = options?.includeCounty ?? true;
const includeCountry: boolean = options?.includeCountry ?? true;

const factory = () => {
const factory: () => string = () => {
const { street, city, county, country, zipCode } = randAddress({
includeCounty,
includeCountry,
Expand Down
6 changes: 4 additions & 2 deletions packages/falso/src/lib/future-date.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { randBetweenDate } from './between-date';
import { fake, FakeOptions } from './core/core';
import { dateComparisonFunction, fake, FakeOptions } from './core/core';

interface FutureOptions extends FakeOptions {
years?: number;
Expand Down Expand Up @@ -35,5 +35,7 @@ export function randFutureDate<Options extends FutureOptions = never>(
const yearsInMilliseconds = years * 365 * 24 * 60 * 60 * 1000;
const from = new Date();
const to = new Date(from.getTime() + yearsInMilliseconds);
return fake(() => randBetweenDate({ from, to }), options);
const factory: () => Date = () => randBetweenDate({ from, to });

return fake(factory, options, dateComparisonFunction);
}
13 changes: 12 additions & 1 deletion packages/falso/src/lib/nearby-gpscoordinate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,16 @@ import { randLongitude } from './longitude';
export function randNearbyGPSCoordinate<Options extends FakeOptions = never>(
options?: Options
) {
return fake(() => [randLatitude(), randLongitude()], options);
return fake(
() => [randLatitude(), randLongitude()],
options,
comparisonFunction
);
}

const comparisonFunction: (
coordinate: number[],
coordinates: number[][]
) => boolean = (coordinate, coordinates) => {
return coordinates.some((c) => c.join('') === c.join(''));
};
8 changes: 6 additions & 2 deletions packages/falso/src/lib/past-date.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { randBetweenDate } from './between-date';
import { fake, FakeOptions } from './core/core';
import { dateComparisonFunction, fake, FakeOptions } from './core/core';

interface PastOptions extends FakeOptions {
years?: number;
Expand Down Expand Up @@ -36,5 +36,9 @@ export function randPastDate<Options extends PastOptions = never>(
const to = new Date();
const from = new Date(to.getTime() - yearsInMilliseconds);

return fake(() => randBetweenDate({ from, to }), options);
return fake(
() => randBetweenDate({ from, to }),
options,
dateComparisonFunction
);
}
8 changes: 6 additions & 2 deletions packages/falso/src/lib/recent-date.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { randBetweenDate } from './between-date';
import { fake, FakeOptions } from './core/core';
import { dateComparisonFunction, fake, FakeOptions } from './core/core';

interface RecentOptions extends FakeOptions {
days?: number;
Expand Down Expand Up @@ -36,5 +36,9 @@ export function randRecentDate<Options extends RecentOptions = never>(
const to = new Date();
const from = new Date(to.getTime() - daysInMilliseconds);

return fake(() => randBetweenDate({ from, to }), options);
return fake(
() => randBetweenDate({ from, to }),
options,
dateComparisonFunction
);
}
8 changes: 6 additions & 2 deletions packages/falso/src/lib/soon-date.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { randBetweenDate } from './between-date';
import { fake, FakeOptions } from './core/core';
import { dateComparisonFunction, fake, FakeOptions } from './core/core';

interface SoonOptions extends FakeOptions {
days?: number;
Expand Down Expand Up @@ -35,5 +35,9 @@ 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);
return fake(
() => randBetweenDate({ from, to }),
options,
dateComparisonFunction
);
}
16 changes: 8 additions & 8 deletions packages/falso/src/lib/todo.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { fake, FakeOptions } from './core/core';
import { fake, FakeOptions, objectWithIdComparisonFunction } from './core/core';
import { randUuid } from './uuid';
import { randBoolean } from './boolean';
import { randText } from './text';
Expand Down Expand Up @@ -26,11 +26,11 @@ export interface Todo {
export function randTodo<Options extends FakeOptions = never>(
options?: Options
) {
return fake(() => {
return {
id: randUuid(),
title: randText({ charCount: 40 }),
completed: randBoolean(),
} as Todo;
}, options);
const factory: () => Todo = () => ({
id: randUuid(),
title: randText({ charCount: 40 }),
completed: randBoolean(),
});

return fake(factory, options, objectWithIdComparisonFunction);
}
6 changes: 4 additions & 2 deletions packages/falso/src/lib/user-name.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export interface UserNameOptions extends FakeOptions {
export function randUserName<Options extends UserNameOptions = never>(
options?: Options
) {
return fake(() => {
const factory: () => string = () => {
const firstName = options?.firstName ?? randFirstName();
const lastName = options?.lastName ?? randLastName();
let userName = `${firstName} ${lastName}`.replace(' ', fake(['.', '_']));
Expand All @@ -44,5 +44,7 @@ export function randUserName<Options extends UserNameOptions = never>(
}

return userName;
}, options);
};

return fake(factory, options);
}
6 changes: 4 additions & 2 deletions packages/falso/src/lib/zip-code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ import { randBoolean } from './boolean';
export function randZipCode<Options extends FakeOptions = never>(
options?: Options
) {
return fake(() => {
const factory: () => string = () => {
let zipCode = '' + randNumber({ min: 10_000, max: 99_999 });

if (randBoolean()) {
zipCode += '-' + randNumber({ min: 1_000, max: 9_999 });
}

return zipCode;
}, options);
};

return fake(factory, options);
}

0 comments on commit 77831a5

Please sign in to comment.