Skip to content

Commit

Permalink
feat: πŸ”₯ clean up unique validators
Browse files Browse the repository at this point in the history
Move validators to own file. Rename validators to follow similar naming
convention

βœ… Closes: #220
  • Loading branch information
Ryan Smee committed May 3, 2022
1 parent d4d1fa3 commit 32b84de
Show file tree
Hide file tree
Showing 16 changed files with 67 additions and 74 deletions.
3 changes: 2 additions & 1 deletion packages/falso/src/lib/address.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { FakeOptions, fake, objectIsUnique } from './core/core';
import { FakeOptions, fake } from './core/core';
import { randCity } from './city';
import { randStreetAddress } from './street-address';
import { randZipCode } from './zip-code';
import { randCounty } from './county';
import { randCountry } from './country';
import { objectIsUnique } from './core/unique-validators';

export interface AddressOptions extends FakeOptions {
includeCounty?: boolean;
Expand Down
3 changes: 2 additions & 1 deletion packages/falso/src/lib/between-date.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { dateIsUnique, fake, FakeOptions } from './core/core';
import { fake, FakeOptions } from './core/core';
import { randNumber } from './number';
import { dateIsUnique } from './core/unique-validators';

interface BetweenOptions extends FakeOptions {
from: Date;
Expand Down
43 changes: 3 additions & 40 deletions packages/falso/src/lib/core/core.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { random } from '../random';
import { primitiveValueIsUnique } from './unique-validators';

export interface FakeOptions {
length?: number;
Expand All @@ -14,7 +15,7 @@ type Return<T, O extends FakeOptions> = [O] extends [never]
export function fake<T, Options extends FakeOptions>(
data: T[] | (() => T),
options?: Options,
comparisonFunction: (item: T, items: T[]) => boolean = primitiveValueIsUnique,
uniqueComparer: (item: T, items: T[]) => boolean = primitiveValueIsUnique,
comparisonKeys?: string[]
): Return<T, Options> {
if (options?.length === 0) {
Expand All @@ -25,12 +26,7 @@ export function fake<T, Options extends FakeOptions>(
return fakeFromArray(data, options) as any;
}

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

export function fakeFromFunction<T, Options extends FakeOptions>(
Expand Down Expand Up @@ -102,39 +98,6 @@ export function fakeFromArray<T, Options extends FakeOptions>(
return newArray;
}

export const primitiveValueIsUnique: <T>(item: T, items: T[]) => boolean = (
item,
items
) => !items.includes(item);

export const dateIsUnique: (date: Date, dates: Date[]) => boolean = (
date,
dates
) => !dates.some((d) => d.valueOf() === date.valueOf());

export const checkUniqueObjectWithId: <T extends { id: string }>(
item: T,
items: T[]
) => boolean = (item, items) => objectIsUnique(item, items, ['id']);

export const objectIsUnique: (
item: any,
items: any[],
keys: string[]
) => boolean = (item: any, items: any[], keys: string[]) => {
for (const key of keys) {
if (!item[key]) {
throw new Error(`${key} does not exist in this array value type`);
}

if (items.some((arrayItem) => arrayItem[key] === item[key])) {
return true;
}
}

return false;
};

export function randElement<T>(arr: T[]): T {
return arr[Math.floor(random() * arr.length)];
}
Expand Down
32 changes: 32 additions & 0 deletions packages/falso/src/lib/core/unique-validators.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
export const primitiveValueIsUnique: <T>(item: T, items: T[]) => boolean = (
item,
items
) => !items.includes(item);

export const dateIsUnique: (date: Date, dates: Date[]) => boolean = (
date,
dates
) => !dates.some((d) => d.valueOf() === date.valueOf());

export const objectWithIdIsUnique: <T extends { id: string }>(
item: T,
items: T[]
) => boolean = (item, items) => objectIsUnique(item, items, ['id']);

export const objectIsUnique: (
item: any,
items: any[],
keys: string[]
) => boolean = (item: any, items: any[], keys: string[]) => {
for (const key of keys) {
if (!item[key]) {
throw new Error(`${key} does not exist in this array value type`);
}

if (items.some((arrayItem) => arrayItem[key] === item[key])) {
return true;
}
}

return false;
};
3 changes: 2 additions & 1 deletion packages/falso/src/lib/credit-card.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { fake, FakeOptions, objectIsUnique } from './core/core';
import { fake, FakeOptions } 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 { objectIsUnique } from './core/unique-validators';

export interface CreditCardOptions extends FakeOptions {
fullName?: string;
Expand Down
8 changes: 2 additions & 6 deletions packages/falso/src/lib/flight-details.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import {
fake,
FakeOptions,
getRandomInRange,
objectIsUnique,
} from './core/core';
import { fake, FakeOptions, getRandomInRange } from './core/core';
import { randFutureDate } from './future-date';
import { randAirline } from './airline';
import { Airline, randFlightNumber } from './flight-number';
import { randFullName } from './full-name';
import { randSeatNumber } from './seat-number';
import { Airport, randAirport } from './airport';
import { objectIsUnique } from './core/unique-validators';

export interface FlightDetailsOptions extends FakeOptions {
airline?: Airline;
Expand Down
3 changes: 2 additions & 1 deletion packages/falso/src/lib/future-date.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { randBetweenDate } from './between-date';
import { dateIsUnique, fake, FakeOptions } from './core/core';
import { fake, FakeOptions } from './core/core';
import { dateIsUnique } from './core/unique-validators';

interface FutureOptions extends FakeOptions {
years?: number;
Expand Down
3 changes: 2 additions & 1 deletion packages/falso/src/lib/past-date.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { randBetweenDate } from './between-date';
import { dateIsUnique, fake, FakeOptions } from './core/core';
import { fake, FakeOptions } from './core/core';
import { dateIsUnique } from './core/unique-validators';

interface PastOptions extends FakeOptions {
years?: number;
Expand Down
5 changes: 3 additions & 2 deletions packages/falso/src/lib/post.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { FakeOptions, fake, checkUniqueObjectWithId } from './core/core';
import { FakeOptions, fake } from './core/core';
import { randUser, User } from './user';
import { randUuid } from './uuid';
import { randText } from './text';
import { randNumber } from './number';
import { objectWithIdIsUnique } from './core/unique-validators';

export interface Post {
id: string;
Expand Down Expand Up @@ -49,5 +50,5 @@ export function randPost<Options extends FakeOptions = never>(
return post;
};

return fake(factory, options, checkUniqueObjectWithId);
return fake(factory, options, objectWithIdIsUnique);
}
10 changes: 3 additions & 7 deletions packages/falso/src/lib/product.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import {
FakeOptions,
fake,
getRandomInRange,
checkUniqueObjectWithId,
} from './core/core';
import { FakeOptions, fake, getRandomInRange } from './core/core';
import { randUuid } from './uuid';
import { randProductName } from './product-name';
import { randProductDescription } from './product-description';
import { randProductCategory } from './product-category';
import { randImg } from './img';
import { objectWithIdIsUnique } from './core/unique-validators';

export interface Product {
id: string;
Expand Down Expand Up @@ -57,5 +53,5 @@ export function randProduct<Options extends FakeOptions = never>(
},
});

return fake(factory, options, checkUniqueObjectWithId);
return fake(factory, options, objectWithIdIsUnique);
}
3 changes: 2 additions & 1 deletion packages/falso/src/lib/recent-date.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { randBetweenDate } from './between-date';
import { dateIsUnique, fake, FakeOptions } from './core/core';
import { fake, FakeOptions } from './core/core';
import { dateIsUnique } from './core/unique-validators';

interface RecentOptions extends FakeOptions {
days?: number;
Expand Down
3 changes: 2 additions & 1 deletion packages/falso/src/lib/soon-date.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { randBetweenDate } from './between-date';
import { dateIsUnique, fake, FakeOptions } from './core/core';
import { fake, FakeOptions } from './core/core';
import { dateIsUnique } from './core/unique-validators';

interface SoonOptions extends FakeOptions {
days?: number;
Expand Down
10 changes: 3 additions & 7 deletions packages/falso/src/lib/superhero.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import {
fake,
FakeOptions,
checkUniqueObjectWithId,
randElement,
} from './core/core';
import { fake, FakeOptions, randElement } from './core/core';
import { data } from './superhero.json';
import { randUuid } from './uuid';
import { objectWithIdIsUnique } from './core/unique-validators';

export type ComicBookCompany = 'Marvel' | 'DC';

Expand Down Expand Up @@ -59,5 +55,5 @@ export function randSuperhero<Options extends SuperheroOptions = never>(
};
};

return fake(factory, options, checkUniqueObjectWithId);
return fake(factory, options, objectWithIdIsUnique);
}
5 changes: 3 additions & 2 deletions packages/falso/src/lib/todo.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { fake, FakeOptions, checkUniqueObjectWithId } from './core/core';
import { fake, FakeOptions } from './core/core';
import { randUuid } from './uuid';
import { randBoolean } from './boolean';
import { randText } from './text';
import { objectWithIdIsUnique } from './core/unique-validators';

export interface Todo {
id: string;
Expand Down Expand Up @@ -36,5 +37,5 @@ export function randTodo<Options extends FakeOptions = never>(
completed: randBoolean(),
});

return fake(factory, options, checkUniqueObjectWithId);
return fake(factory, options, objectWithIdIsUnique);
}
5 changes: 3 additions & 2 deletions packages/falso/src/lib/user.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { fake, FakeOptions, checkUniqueObjectWithId } from './core/core';
import { fake, FakeOptions } from './core/core';
import { randUuid } from './uuid';
import { randEmail } from './email';
import { randFirstName } from './first-name';
Expand All @@ -7,6 +7,7 @@ import { randPhoneNumber } from './phone-number';
import { randUserName } from './user-name';
import { randAvatar } from './avatar';
import { randAddress } from './address';
import { objectWithIdIsUnique } from './core/unique-validators';

export interface User {
id: string;
Expand Down Expand Up @@ -62,5 +63,5 @@ export function randUser<Options extends FakeOptions = never>(
return user;
};

return fake(factory, options, checkUniqueObjectWithId);
return fake(factory, options, objectWithIdIsUnique);
}
2 changes: 1 addition & 1 deletion packages/falso/src/tests/core/object-is-unique.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { objectIsUnique } from '../../lib/core/core';
import { randUser, User } from '../../lib/user';
import { randUuid } from '../../lib/uuid';
import { randFirstName } from '../../lib/first-name';
import { objectIsUnique } from '../../lib/core/unique-validators';

describe('objectIsUnique', () => {
describe("keys contains a key that doesn't exist", () => {
Expand Down

0 comments on commit 32b84de

Please sign in to comment.