Skip to content

Commit

Permalink
feat: πŸ”₯ Add json check unique function. Clean up code
Browse files Browse the repository at this point in the history
βœ… Closes: #220
  • Loading branch information
theryansmee committed Mar 24, 2022
1 parent 96acb7e commit af09284
Show file tree
Hide file tree
Showing 17 changed files with 62 additions and 96 deletions.
8 changes: 3 additions & 5 deletions packages/falso/src/lib/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,10 @@ export function randAddress<Options extends AddressOptions = never>(
return address;
};

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

const comparisonFunction: (item: Address, items: Address[]) => boolean = (
const checkUnique: (item: Address, items: Address[]) => boolean = (
item: Address,
items: Address[]
) => {
return items.some((i) => i.street + i.zipCode === item.street + item.zipCode);
};
) => 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 { dateComparisonFunction, fake, FakeOptions } from './core/core';
import { checkUniqueDate, 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, dateComparisonFunction);
return fake(generator, options, checkUniqueDate);
}
29 changes: 13 additions & 16 deletions packages/falso/src/lib/core/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,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 = primitiveComparisonFunction
comparisonFunction: (item: T, items: T[]) => boolean = checkUniquePrimitive
): Return<T, Options> {
if (Array.isArray(data)) {
return fakeFromArray(data, options) as any;
Expand Down Expand Up @@ -87,20 +84,20 @@ export function fakeFromArray<T, Options extends FakeOptions>(
return newArray;
}

export const primitiveComparisonFunction: <T>(item: T, items: T[]) => boolean =
(item, items) => items.includes(item);
export const objectWithIdComparisonFunction: <T extends { id: string }>(
item: T,
items: T[]
) => boolean = (item, items) => {
return items.some((i) => i.id === item.id);
};
export const dateComparisonFunction: (date: Date, dates: Date[]) => boolean = (
export const checkUniquePrimitive: <T>(item: T, items: T[]) => boolean = (
item,
items
) => items.includes(item);

export const checkUniqueDate: (date: Date, dates: Date[]) => boolean = (
date,
dates
) => {
return dates.some((d) => d.valueOf() === date.valueOf());
};
) => dates.some((d) => d.valueOf() === date.valueOf());

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

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

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

export const comparisonFunction: (
card: CreditCard,
cards: CreditCard[]
) => boolean = (card, cards) => {
return cards.some((c) => c.number === card.number);
};
const checkUnique: (card: CreditCard, cards: CreditCard[]) => boolean = (
card,
cards
) => cards.some((c) => c.number === card.number);
9 changes: 4 additions & 5 deletions packages/falso/src/lib/flight-details.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,15 @@ export function randFlightDetails<Options extends FlightDetailsOptions = never>(
};
};

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

export const comparisonFunction: (
const checkUnique: (
flight: FlightDetails,
flights: FlightDetails[]
) => boolean = (flight, flights) => {
return flights.some(
) => boolean = (flight, flights) =>
flights.some(
(f) =>
f.passenger + f.flightNumber + f.date ===
flight.passenger + flight.flightNumber + flight.date
);
};
4 changes: 2 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 { dateComparisonFunction, fake, FakeOptions } from './core/core';
import { checkUniqueDate, fake, FakeOptions } from './core/core';

interface FutureOptions extends FakeOptions {
years?: number;
Expand Down Expand Up @@ -37,5 +37,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, dateComparisonFunction);
return fake(factory, options, checkUniqueDate);
}
7 changes: 6 additions & 1 deletion packages/falso/src/lib/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,10 @@ export function randJSON<Options extends RandomJSONOptions = never>(
return generatedObject;
};

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

const checkUnique: (item: object, items: object[]) => boolean = (
item: object,
items: object[]
) => items.some((i) => JSON.stringify(i) === JSON.stringify(item));
15 changes: 4 additions & 11 deletions packages/falso/src/lib/nearby-gpscoordinate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,9 @@ import { randLongitude } from './longitude';
export function randNearbyGPSCoordinate<Options extends FakeOptions = never>(
options?: Options
) {
return fake(
() => [randLatitude(), randLongitude()],
options,
comparisonFunction
);
return fake(() => [randLatitude(), randLongitude()], options, checkUnique);
}

const comparisonFunction: (
coordinate: number[],
coordinates: number[][]
) => boolean = (coordinate, coordinates) => {
return coordinates.some((c) => c.join('') === c.join(''));
};
const checkUnique: (coordinate: number[], coordinates: number[][]) => boolean =
(coordinate, coordinates) =>
coordinates.some((c) => c.join('') === c.join(''));
8 changes: 2 additions & 6 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 { dateComparisonFunction, fake, FakeOptions } from './core/core';
import { checkUniqueDate, fake, FakeOptions } from './core/core';

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

return fake(
() => randBetweenDate({ from, to }),
options,
dateComparisonFunction
);
return fake(() => randBetweenDate({ from, to }), options, checkUniqueDate);
}
4 changes: 2 additions & 2 deletions packages/falso/src/lib/post.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FakeOptions, fake, objectWithIdComparisonFunction } from './core/core';
import { FakeOptions, fake, checkUniqueObjectWithId } from './core/core';
import { randUser, User } from './user';
import { randUuid } from './uuid';
import { randText } from './text';
Expand Down Expand Up @@ -45,5 +45,5 @@ export function randPost<Options extends FakeOptions = never>(
return post;
};

return fake(factory, options, objectWithIdComparisonFunction);
return fake(factory, options, checkUniqueObjectWithId);
}
4 changes: 2 additions & 2 deletions packages/falso/src/lib/product.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
FakeOptions,
fake,
getRandomInRange,
objectWithIdComparisonFunction,
checkUniqueObjectWithId,
} from './core/core';
import { randUuid } from './uuid';
import { randProductName } from './product-name';
Expand Down Expand Up @@ -53,5 +53,5 @@ export function randProduct<Options extends FakeOptions = never>(
},
});

return fake(factory, options, objectWithIdComparisonFunction);
return fake(factory, options, checkUniqueObjectWithId);
}
8 changes: 2 additions & 6 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 { dateComparisonFunction, fake, FakeOptions } from './core/core';
import { checkUniqueDate, fake, FakeOptions } from './core/core';

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

return fake(
() => randBetweenDate({ from, to }),
options,
dateComparisonFunction
);
return fake(() => randBetweenDate({ from, to }), options, checkUniqueDate);
}
8 changes: 2 additions & 6 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 { dateComparisonFunction, fake, FakeOptions } from './core/core';
import { checkUniqueDate, fake, FakeOptions } from './core/core';

interface SoonOptions extends FakeOptions {
days?: number;
Expand Down Expand Up @@ -35,9 +35,5 @@ 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,
dateComparisonFunction
);
return fake(() => randBetweenDate({ from, to }), options, checkUniqueDate);
}
4 changes: 2 additions & 2 deletions packages/falso/src/lib/superhero.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
fake,
FakeOptions,
objectWithIdComparisonFunction,
checkUniqueObjectWithId,
randElement,
} from './core/core';
import { data } from './superhero.json';
Expand Down Expand Up @@ -55,5 +55,5 @@ export function randSuperhero<Options extends SuperheroOptions = never>(
};
};

return fake(factory, options, objectWithIdComparisonFunction);
return fake(factory, options, checkUniqueObjectWithId);
}
4 changes: 2 additions & 2 deletions packages/falso/src/lib/todo.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { fake, FakeOptions, objectWithIdComparisonFunction } from './core/core';
import { fake, FakeOptions, checkUniqueObjectWithId } from './core/core';
import { randUuid } from './uuid';
import { randBoolean } from './boolean';
import { randText } from './text';
Expand Down Expand Up @@ -32,5 +32,5 @@ export function randTodo<Options extends FakeOptions = never>(
completed: randBoolean(),
});

return fake(factory, options, objectWithIdComparisonFunction);
return fake(factory, options, checkUniqueObjectWithId);
}
4 changes: 2 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, objectWithIdComparisonFunction } from './core/core';
import { fake, FakeOptions, checkUniqueObjectWithId } from './core/core';
import { randUuid } from './uuid';
import { randEmail } from './email';
import { randFirstName } from './first-name';
Expand Down Expand Up @@ -58,5 +58,5 @@ export function randUser<Options extends FakeOptions = never>(
return user;
};

return fake(factory, options, objectWithIdComparisonFunction);
return fake(factory, options, checkUniqueObjectWithId);
}
26 changes: 7 additions & 19 deletions packages/falso/src/tests/json.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { randNumber } from '../lib/number';
import { randJSON } from '../lib/json';
import { randNumber } from '../lib/number';

describe('randJSON', () => {
describe('when it returns the expected values', () => {
Expand All @@ -26,28 +26,16 @@ describe('randJSON', () => {
});

describe('length is passed', () => {
describe('length is 1', () => {
it('should return an array length of 1', () => {
const result = randJSON({ length: 1 });

expect(result?.length).toEqual(1);
});
});

describe('length is 5', () => {
it('should return an array length of 5', () => {
const result = randJSON({ length: 5 });
let length: number;

expect(result?.length).toEqual(5);
});
beforeEach(() => {
length = randNumber({ min: 1, max: 30, fraction: 1 });
});

describe('length is 100', () => {
it('should return an array length of 100', () => {
const result = randJSON({ length: 100 });
it('should return an array length passed', () => {
const result = randJSON({ length });

expect(result?.length).toEqual(100);
});
expect(result?.length).toEqual(length);
});
});
});

0 comments on commit af09284

Please sign in to comment.