diff --git a/packages/falso/jest.config.ts b/packages/falso/jest.config.ts index 43634e627..f513ee574 100644 --- a/packages/falso/jest.config.ts +++ b/packages/falso/jest.config.ts @@ -13,4 +13,5 @@ module.exports = { moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'], coverageDirectory: '../../coverage/packages/falso', preset: '../../jest.preset.ts', + snapshotResolver: './snapshot-resolver.js', }; diff --git a/packages/falso/snapshot-resolver.js b/packages/falso/snapshot-resolver.js new file mode 100644 index 000000000..d37877caf --- /dev/null +++ b/packages/falso/snapshot-resolver.js @@ -0,0 +1,11 @@ +module.exports = { + testPathForConsistencyCheck: 'some/tests/example.spec.ts', + resolveSnapshotPath: (testPath, snapshotExtension) => { + return testPath.replace('tests/', 'tests/__snapshots__/') + snapshotExtension + }, + resolveTestPath: (snapshotFilePath, snapshotExtension) => { + return snapshotFilePath + .replace('tests/__snapshots__/', 'tests/') + .slice(0, -snapshotExtension.length) + }, +} diff --git a/packages/falso/src/lib/abbreviation.ts b/packages/falso/src/lib/abbreviation.ts index 385cf873f..1b207f585 100644 --- a/packages/falso/src/lib/abbreviation.ts +++ b/packages/falso/src/lib/abbreviation.ts @@ -14,6 +14,10 @@ import { data } from './abbreviation.json'; * * randAbbreviation({ length: 10 }) * + * @example + * + * randAbbreviation({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randAbbreviation(options?: O) { return fake(data, options); diff --git a/packages/falso/src/lib/accessory.ts b/packages/falso/src/lib/accessory.ts index fb9eae78d..63ab3d976 100644 --- a/packages/falso/src/lib/accessory.ts +++ b/packages/falso/src/lib/accessory.ts @@ -14,6 +14,10 @@ import { data } from './accessory.json'; * * randAccessory({ length: 10 }) * + * @example + * + * randAccessory({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randAccessory( options?: Options diff --git a/packages/falso/src/lib/account.ts b/packages/falso/src/lib/account.ts index 73a10c872..28d37ee42 100644 --- a/packages/falso/src/lib/account.ts +++ b/packages/falso/src/lib/account.ts @@ -21,6 +21,10 @@ export interface AccountOptions extends FakeOptions { * * randAccount({ length: 10 }) * + * @example + * + * randAccount({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randAccount( options?: Options diff --git a/packages/falso/src/lib/address.ts b/packages/falso/src/lib/address.ts index cbafe0631..ef1caeb78 100644 --- a/packages/falso/src/lib/address.ts +++ b/packages/falso/src/lib/address.ts @@ -4,6 +4,7 @@ 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; @@ -39,6 +40,10 @@ export interface Address { * * randAddress({ length: 10 }) * + * @example + * + * randAddress({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randAddress( options?: Options @@ -64,5 +69,9 @@ export function randAddress( return address; }; - return fake(factory, options); + return fake(factory, options, { uniqueComparer: checkUnique }); +} + +function checkUnique(address: Address, addresses: Address[]): boolean { + return objectIsUnique(address, addresses, ['street', 'zipCode']); } diff --git a/packages/falso/src/lib/airline.ts b/packages/falso/src/lib/airline.ts index 9b37efdb8..f5222edc7 100644 --- a/packages/falso/src/lib/airline.ts +++ b/packages/falso/src/lib/airline.ts @@ -14,6 +14,10 @@ import { data } from './airline.json'; * * randAirline({ length: 10 }) * + * @example + * + * randAirline({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randAirline( options?: Options diff --git a/packages/falso/src/lib/airport-code.ts b/packages/falso/src/lib/airport-code.ts index c3aa0854c..d406c9a67 100644 --- a/packages/falso/src/lib/airport-code.ts +++ b/packages/falso/src/lib/airport-code.ts @@ -15,6 +15,10 @@ import { rand } from './rand'; * * randAirportCode({ length: 10 }) * + * @example + * + * randAirportCode({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randAirportCode( options?: Options diff --git a/packages/falso/src/lib/airport-name.ts b/packages/falso/src/lib/airport-name.ts index c81461b5c..dd5b7e705 100644 --- a/packages/falso/src/lib/airport-name.ts +++ b/packages/falso/src/lib/airport-name.ts @@ -15,6 +15,10 @@ import { rand } from './rand'; * * randAirportName({ length: 10 }) * + * @example + * + * randAirportName({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randAirportName( options?: Options diff --git a/packages/falso/src/lib/airport.ts b/packages/falso/src/lib/airport.ts index fac0e177f..917e650f3 100644 --- a/packages/falso/src/lib/airport.ts +++ b/packages/falso/src/lib/airport.ts @@ -21,6 +21,10 @@ export interface Airport { * * randAirport({ length: 10 }) * + * @example + * + * randAirport({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randAirport( options?: Options diff --git a/packages/falso/src/lib/alpha-numeric.ts b/packages/falso/src/lib/alpha-numeric.ts index c47465624..7f8f65463 100644 --- a/packages/falso/src/lib/alpha-numeric.ts +++ b/packages/falso/src/lib/alpha-numeric.ts @@ -16,6 +16,10 @@ import { randNumber } from './number'; * * randAlphaNumeric({ length: 3 }) * + * @example + * + * randAlphaNumeric({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randAlphaNumeric( options?: Options diff --git a/packages/falso/src/lib/alpha.ts b/packages/falso/src/lib/alpha.ts index 30125cca0..5a2f71077 100644 --- a/packages/falso/src/lib/alpha.ts +++ b/packages/falso/src/lib/alpha.ts @@ -14,6 +14,10 @@ import { alphaChars } from './sequence'; * * randAlpha({ length: 3 }) * + * @example + * + * randAlpha({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randAlpha( options?: Options diff --git a/packages/falso/src/lib/american-football-team.ts b/packages/falso/src/lib/american-football-team.ts index d3aec877f..d5471adbc 100644 --- a/packages/falso/src/lib/american-football-team.ts +++ b/packages/falso/src/lib/american-football-team.ts @@ -14,6 +14,10 @@ import { data } from './american-football-team.json'; * * randAmericanFootballTeam({ length: 10 }) * + * @example + * + * randAmericanFootballTeam({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randAmericanFootballTeam( options?: Options diff --git a/packages/falso/src/lib/animal-type.ts b/packages/falso/src/lib/animal-type.ts index b08f3ddaf..081df7227 100644 --- a/packages/falso/src/lib/animal-type.ts +++ b/packages/falso/src/lib/animal-type.ts @@ -14,6 +14,10 @@ import { data } from './animal-type.json'; * * randAnimalType({ length: 10 }) * + * @example + * + * randAnimalType({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randAnimalType( options?: Options diff --git a/packages/falso/src/lib/arn.ts b/packages/falso/src/lib/arn.ts index 723d4fbb0..5b370bce8 100644 --- a/packages/falso/src/lib/arn.ts +++ b/packages/falso/src/lib/arn.ts @@ -35,6 +35,10 @@ const serviceArn: Record string> = { * * randArn({ length: 10 }) * + * @example + * + * randArn({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randArn( options?: Options diff --git a/packages/falso/src/lib/avatar.ts b/packages/falso/src/lib/avatar.ts index 04dda6dfd..73c5ce33b 100644 --- a/packages/falso/src/lib/avatar.ts +++ b/packages/falso/src/lib/avatar.ts @@ -15,11 +15,15 @@ interface AvatarOptions extends FakeOptions { * * @example * + * randAvatar({ size: 200 }) // default is 100 + * + * @example + * * randAvatar({ length: 10 }) * * @example * - * randAvatar({ size: 200 }) // default is 100 + * randAvatar({ length: 10, priority: 'unique' }) // default priority is 'length' * */ export function randAvatar( diff --git a/packages/falso/src/lib/aws-region.ts b/packages/falso/src/lib/aws-region.ts index 9998257ad..a57745858 100644 --- a/packages/falso/src/lib/aws-region.ts +++ b/packages/falso/src/lib/aws-region.ts @@ -14,6 +14,10 @@ import { data } from './aws-region.json'; * * randAwsRegion({ length: 10 }) * + * @example + * + * randAwsRegion({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randAwsRegion( options?: Options diff --git a/packages/falso/src/lib/aws-request-id.ts b/packages/falso/src/lib/aws-request-id.ts index 3b63608b5..f66223945 100644 --- a/packages/falso/src/lib/aws-request-id.ts +++ b/packages/falso/src/lib/aws-request-id.ts @@ -14,6 +14,10 @@ import { fake, FakeOptions } from './core/core'; * * randAwsRequestId({ length: 10 }) * + * @example + * + * randAwsRequestId({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randAwsRequestId( options?: Options diff --git a/packages/falso/src/lib/aws-service.ts b/packages/falso/src/lib/aws-service.ts index c3a8f46bc..8c1b6fedf 100644 --- a/packages/falso/src/lib/aws-service.ts +++ b/packages/falso/src/lib/aws-service.ts @@ -14,6 +14,10 @@ import { data } from './aws-service.json'; * * randAwsService({ length: 10 }) * + * @example + * + * randAwsService({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randAwsService( options?: Options diff --git a/packages/falso/src/lib/baseball-team.ts b/packages/falso/src/lib/baseball-team.ts index 2ab088916..f1a3833e7 100644 --- a/packages/falso/src/lib/baseball-team.ts +++ b/packages/falso/src/lib/baseball-team.ts @@ -14,6 +14,10 @@ import { data } from './baseball-team.json'; * * randBaseballTeam({ length: 10 }) * + * @example + * + * randBaseballTeam({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randBaseballTeam( options?: Options diff --git a/packages/falso/src/lib/basketball-team.ts b/packages/falso/src/lib/basketball-team.ts index afcec1f29..784f932f6 100644 --- a/packages/falso/src/lib/basketball-team.ts +++ b/packages/falso/src/lib/basketball-team.ts @@ -14,6 +14,10 @@ import { data } from './basketball-team.json'; * * randBasketballTeam({ length: 10 }) * + * @example + * + * randBasketballTeam({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randBasketballTeam( options?: Options diff --git a/packages/falso/src/lib/bear.ts b/packages/falso/src/lib/bear.ts index af00eff74..a2766fe78 100644 --- a/packages/falso/src/lib/bear.ts +++ b/packages/falso/src/lib/bear.ts @@ -14,6 +14,10 @@ import { data } from './bear.json'; * * randBear({ length: 10 }) * + * @example + * + * randBear({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randBear( options?: Options diff --git a/packages/falso/src/lib/between-date.ts b/packages/falso/src/lib/between-date.ts index 2b06d5c21..bc06dd932 100644 --- a/packages/falso/src/lib/between-date.ts +++ b/packages/falso/src/lib/between-date.ts @@ -1,5 +1,6 @@ import { fake, FakeOptions } from './core/core'; import { randNumber } from './number'; +import { dateIsUnique } from './core/unique-validators'; interface BetweenOptions extends FakeOptions { from: Date | string; @@ -19,6 +20,10 @@ interface BetweenOptions extends FakeOptions { * * randBetweenDate({ from: new Date('10/07/2020'), to: new Date(), length: 10 }) * + * @example + * + * randBetweenDate({ from: new Date('10/07/2020'), to: new Date(), length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randBetweenDate( options: Options @@ -39,5 +44,5 @@ export function randBetweenDate( ); }; - return fake(generator, options); + return fake(generator, options, { uniqueComparer: dateIsUnique }); } diff --git a/packages/falso/src/lib/binary.ts b/packages/falso/src/lib/binary.ts index cb44d5d00..1df24d728 100644 --- a/packages/falso/src/lib/binary.ts +++ b/packages/falso/src/lib/binary.ts @@ -13,6 +13,10 @@ import { FakeOptions, fake, getRandomInRange } from './core/core'; * * randBinary({ length: 10 }) * + * @example + * + * randBinary({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randBinary( options?: Options diff --git a/packages/falso/src/lib/bird.ts b/packages/falso/src/lib/bird.ts index ba7236b50..50c85f02d 100644 --- a/packages/falso/src/lib/bird.ts +++ b/packages/falso/src/lib/bird.ts @@ -14,6 +14,10 @@ import { data } from './bird.i18n.json'; * * randBird({ length: 10 }) * + * @example + * + * randBird({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randBird( options?: Options diff --git a/packages/falso/src/lib/bitcoin-address.ts b/packages/falso/src/lib/bitcoin-address.ts index 27492d95c..d415fd346 100644 --- a/packages/falso/src/lib/bitcoin-address.ts +++ b/packages/falso/src/lib/bitcoin-address.ts @@ -14,6 +14,10 @@ import { randSequence } from './sequence'; * * randBitcoinAddress({ length: 10 }) * + * @example + * + * randBitcoinAddress({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randBitcoinAddress( options?: Options diff --git a/packages/falso/src/lib/book.ts b/packages/falso/src/lib/book.ts index 2cf35b499..0c4acf632 100644 --- a/packages/falso/src/lib/book.ts +++ b/packages/falso/src/lib/book.ts @@ -29,11 +29,15 @@ export interface Book { * * @example * + * randBook({ category: 'Comedy' }) + * + * @example + * * randBook({ length: 10 }) * * @example * - * randBook({ category: 'Comedy' }) + * randBook({ length: 10, priority: 'unique' }) // default priority is 'length' * */ export function randBook( diff --git a/packages/falso/src/lib/boolean.ts b/packages/falso/src/lib/boolean.ts index fbcaf8c64..5a33e4a90 100644 --- a/packages/falso/src/lib/boolean.ts +++ b/packages/falso/src/lib/boolean.ts @@ -13,6 +13,10 @@ import { fake, FakeOptions, randElement } from './core/core'; * * randBoolean({ length: 10 }) * + * @example + * + * randBoolean({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randBoolean( options?: Options diff --git a/packages/falso/src/lib/brand.ts b/packages/falso/src/lib/brand.ts index a295b9c48..b8385c24c 100644 --- a/packages/falso/src/lib/brand.ts +++ b/packages/falso/src/lib/brand.ts @@ -14,6 +14,10 @@ import { data } from './brand.json'; * * randBrand({ length: 10 }) * + * @example + * + * randBrand({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randBrand( options?: Options diff --git a/packages/falso/src/lib/browser.ts b/packages/falso/src/lib/browser.ts index d9a42ad9d..0f8ab34ec 100644 --- a/packages/falso/src/lib/browser.ts +++ b/packages/falso/src/lib/browser.ts @@ -14,6 +14,10 @@ import { data } from './browser.json'; * * randBrowser({ length: 10 }) * + * @example + * + * randBrowser({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randBrowser( options?: Options diff --git a/packages/falso/src/lib/cardinal-direction.ts b/packages/falso/src/lib/cardinal-direction.ts index f83a1e2e4..3d5f43548 100644 --- a/packages/falso/src/lib/cardinal-direction.ts +++ b/packages/falso/src/lib/cardinal-direction.ts @@ -14,6 +14,10 @@ import { data } from './cardinal-direction.json'; * * randCardinalDirection({ length: 10 }) * + * @example + * + * randCardinalDirection({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randCardinalDirection( options?: Options diff --git a/packages/falso/src/lib/cat.ts b/packages/falso/src/lib/cat.ts index 5a32318d1..f65752552 100644 --- a/packages/falso/src/lib/cat.ts +++ b/packages/falso/src/lib/cat.ts @@ -14,6 +14,10 @@ import { data } from './cat.json'; * * randCat({ length: 10 }) * + * @example + * + * randCat({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randCat( options?: Options diff --git a/packages/falso/src/lib/catch-phrase.ts b/packages/falso/src/lib/catch-phrase.ts index 38e7e901a..497835fa6 100644 --- a/packages/falso/src/lib/catch-phrase.ts +++ b/packages/falso/src/lib/catch-phrase.ts @@ -14,6 +14,10 @@ import { data } from './catch-phrase.json'; * * randCatchPhrase({ length: 10 }) * + * @example + * + * randCatchPhrase({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randCatchPhrase( options?: Options diff --git a/packages/falso/src/lib/cetacean.ts b/packages/falso/src/lib/cetacean.ts index 902a7c74a..9aa5a23ec 100644 --- a/packages/falso/src/lib/cetacean.ts +++ b/packages/falso/src/lib/cetacean.ts @@ -14,6 +14,10 @@ import { data } from './cetacean.json'; * * randCetacean({ length: 10 }) * + * @example + * + * randCetacean({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randCetacean( options?: Options diff --git a/packages/falso/src/lib/city.ts b/packages/falso/src/lib/city.ts index 1f105852d..75918c54c 100644 --- a/packages/falso/src/lib/city.ts +++ b/packages/falso/src/lib/city.ts @@ -14,6 +14,10 @@ import { data } from './city.json'; * * randCity({ length: 10 }) * + * @example + * + * randCity({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randCity( options?: Options diff --git a/packages/falso/src/lib/clothing-size.ts b/packages/falso/src/lib/clothing-size.ts index f100045b2..f3e8d4b8c 100644 --- a/packages/falso/src/lib/clothing-size.ts +++ b/packages/falso/src/lib/clothing-size.ts @@ -14,6 +14,10 @@ import { data } from './clothing-size.json'; * * randClothingSize({ length: 10 }) * + * @example + * + * randClothingSize({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randClothingSize( options?: Options diff --git a/packages/falso/src/lib/code-snippet.ts b/packages/falso/src/lib/code-snippet.ts index 474d67358..3930ff54e 100644 --- a/packages/falso/src/lib/code-snippet.ts +++ b/packages/falso/src/lib/code-snippet.ts @@ -34,11 +34,15 @@ interface CodeSnippetOptions extends FakeOptions { * * @example * + * randCodeSnippet({ lang: 'html' }) // default is 'javascript' + * + * @example + * * randCodeSnippet({ length: 10 }) * * @example * - * randCodeSnippet({ lang: 'html' }) // default is 'javascript' + * randCodeSnippet({ length: 10, priority: 'unique' }) // default priority is 'length' * */ export function randCodeSnippet( diff --git a/packages/falso/src/lib/color.ts b/packages/falso/src/lib/color.ts index 2239aefeb..2f1bfdf79 100644 --- a/packages/falso/src/lib/color.ts +++ b/packages/falso/src/lib/color.ts @@ -14,6 +14,10 @@ import { data } from './color.json'; * * randColor({ length: 10 }) * + * @example + * + * randColor({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randColor( options?: Options diff --git a/packages/falso/src/lib/company-name.ts b/packages/falso/src/lib/company-name.ts index 3851917c2..608136bda 100644 --- a/packages/falso/src/lib/company-name.ts +++ b/packages/falso/src/lib/company-name.ts @@ -14,6 +14,10 @@ import { data } from './company-name.json'; * * randCompanyName({ length: 10 }) * + * @example + * + * randCompanyName({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randCompanyName( options?: Options diff --git a/packages/falso/src/lib/core/core.ts b/packages/falso/src/lib/core/core.ts index d0c2e2ed6..2f91ba9c2 100644 --- a/packages/falso/src/lib/core/core.ts +++ b/packages/falso/src/lib/core/core.ts @@ -1,7 +1,9 @@ import { random } from '../random'; +import { primitiveValueIsUnique } from './unique-validators'; export interface FakeOptions { length?: number; + priority?: 'length' | 'unique'; locale?: any | string[]; } @@ -9,29 +11,107 @@ export type markRequired = Type & { [Property in Key]-?: Type[Property]; }; +export interface FakeConfig { + uniqueComparer: ( + item: T, + items: T[], + comparisonKeys?: (keyof T)[] + ) => boolean | ((item: T, items: T[]) => boolean); + comparisonKeys?: (keyof T)[]; +} + export type Return = [O] extends [never] ? T : O['length'] extends number ? T[] : T; -type FactoryFunction = (i: number) => T; +export type FactoryFunction = (i: number) => T; export function fake( data: Readonly | FactoryFunction, - options?: Options + options?: Options, + config: FakeConfig = { + uniqueComparer: primitiveValueIsUnique, + } ): Return { - const dataSource = Array.isArray(data) - ? () => randElement(data) - : (data as FactoryFunction); + if (Array.isArray(data)) { + return fakeFromArray(data, options) as any; + } + + return fakeFromFunction(data as FactoryFunction, config, options) as any; +} + +export function fakeFromFunction( + data: FactoryFunction, + config: FakeConfig, + options?: Options +): T | T[] { + if (options?.length === 0) { + return []; + } + + if (!options?.length) { + return data(0); + } + + const priority = options?.priority ?? 'length'; + + if (priority === 'length') { + return Array.from({ length: options.length }, (_, index) => data(index)); + } + + const items: T[] = []; + + let attempts = 0; + const maxAttempts = options.length * 2; + + while (items.length < options.length && attempts < maxAttempts) { + const item = data(0); + + if (config.uniqueComparer(item, items, config.comparisonKeys)) { + items.push(item); + } + + attempts++; + } + + return items; +} + +export function fakeFromArray( + data: T[], + options?: Options +): T | T[] { + if (options?.length === 0) { + return []; + } + + if (!options?.length) { + return randElement(data); + } + + const priority = options?.priority ?? 'length'; + + if (priority === 'length') { + return Array.from({ length: options.length }, () => randElement(data)); + } + + const clonedData: T[] = JSON.parse(JSON.stringify(data)); + const newArray: T[] = []; + + while (clonedData.length && newArray.length !== options.length) { + const randomIndex = getRandomInRange({ + min: 0, + max: clonedData.length - 1, + }); + const item = clonedData[randomIndex]; - if (options?.length === undefined) { - return dataSource(0) as any; + newArray.push(item); + clonedData.splice(randomIndex, 1); } - return Array.from({ length: options.length }, (_, index) => - dataSource(index) - ) as any; + return newArray; } export function randElement(arr: Readonly) { diff --git a/packages/falso/src/lib/core/fake.spec.ts b/packages/falso/src/lib/core/fake.spec.ts new file mode 100644 index 000000000..b00105302 --- /dev/null +++ b/packages/falso/src/lib/core/fake.spec.ts @@ -0,0 +1,119 @@ +import { randWord } from '@ngneat/falso'; +import { fake, FakeOptions, FactoryFunction } from './core'; + +describe('fake', () => { + describe('data from array', () => { + let data: string[]; + + beforeEach(() => { + data = randWord({ length: 4 }); + }); + + describe('length & priority', () => { + describe('length is 0', () => { + it('should return empty array', () => { + const response = fake(data, { length: 0 }); + + expect(response).toEqual([]); + }); + }); + + describe("priority: 'length'", () => { + let options: FakeOptions; + + beforeEach(() => { + options = { + length: 10, + priority: 'length', + }; + }); + + it('should return an array with passed length', () => { + const response = fake(data, options); + + expect(response.length).toEqual(10); + }); + }); + + describe("priority: 'unique'", () => { + let options: FakeOptions; + + beforeEach(() => { + options = { + length: 10, + priority: 'unique', + }; + }); + + it('should return an array with length >= data length', () => { + const response = fake(data, options); + + expect(response.length).toEqual(4); + }); + }); + }); + }); + + describe('data from function', () => { + let data: { + dataFunction: FactoryFunction; + }; + + beforeEach(() => { + data = { + dataFunction: () => 'zero', + }; + + jest + .spyOn(data, 'dataFunction') + .mockReturnValueOnce('one') + .mockReturnValueOnce('two') + .mockReturnValueOnce('tree') + .mockReturnValueOnce('four'); + }); + + describe('length & priority', () => { + describe('length is 0', () => { + it('should return empty array', () => { + const response = fake(data.dataFunction, { length: 0 }); + + expect(response).toEqual([]); + }); + }); + + describe("priority: 'length'", () => { + let options: FakeOptions; + + beforeEach(() => { + options = { + length: 10, + priority: 'length', + }; + }); + + it('should return an array with passed length', () => { + const response = fake(data.dataFunction, options); + + expect(response.length).toEqual(10); + }); + }); + + describe("priority: 'unique'", () => { + let options: FakeOptions; + + beforeEach(() => { + options = { + length: 10, + priority: 'unique', + }; + }); + + it('should return an array with length >= total data unique values length', () => { + const response = fake(data.dataFunction, options); + + expect(response.length).toEqual(5); + }); + }); + }); + }); +}); diff --git a/packages/falso/src/lib/core/unique-validators.ts b/packages/falso/src/lib/core/unique-validators.ts new file mode 100644 index 000000000..3d99b0432 --- /dev/null +++ b/packages/falso/src/lib/core/unique-validators.ts @@ -0,0 +1,30 @@ +import { User } from '@ngneat/falso'; + +export function primitiveValueIsUnique(item: T, items: T[]): boolean { + return !items.includes(item); +} + +export function dateIsUnique(date: Date, dates: Date[]): boolean { + return !dates.some((d) => d.getTime() === date.getTime()); +} + +export function objectWithIdIsUnique( + item: T, + items: T[] +): boolean { + return objectIsUnique(item, items, ['id']); +} + +export function objectIsUnique>( + item: T, + items: T[], + keys: (keyof T)[] +): boolean { + for (const key of keys) { + if (items.some((arrayItem) => arrayItem[key] === item[key])) { + return false; + } + } + + return true; +} diff --git a/packages/falso/src/lib/country-code.ts b/packages/falso/src/lib/country-code.ts index 0fa15c097..b4614d6e2 100644 --- a/packages/falso/src/lib/country-code.ts +++ b/packages/falso/src/lib/country-code.ts @@ -14,6 +14,10 @@ import { data } from './country-code.json'; * * randCountryCode({ length: 10 }) * + * @example + * + * randCountryCode({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randCountryCode( options?: Options diff --git a/packages/falso/src/lib/country.ts b/packages/falso/src/lib/country.ts index 838e3e9f7..9348f870f 100644 --- a/packages/falso/src/lib/country.ts +++ b/packages/falso/src/lib/country.ts @@ -14,6 +14,10 @@ import { data } from './country.json'; * * randCountry({ length: 10 }) * + * @example + * + * randCountry({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randCountry( options?: Options diff --git a/packages/falso/src/lib/county.ts b/packages/falso/src/lib/county.ts index 63b416c7c..4253fa891 100644 --- a/packages/falso/src/lib/county.ts +++ b/packages/falso/src/lib/county.ts @@ -14,6 +14,10 @@ import { data } from './county.json'; * * randCounty({ length: 10 }) * + * @example + * + * randCounty({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randCounty( options?: Options diff --git a/packages/falso/src/lib/cow.ts b/packages/falso/src/lib/cow.ts index 159dc5f2b..f9d0702b1 100644 --- a/packages/falso/src/lib/cow.ts +++ b/packages/falso/src/lib/cow.ts @@ -14,6 +14,10 @@ import { data } from './cow.json'; * * randCow({ length: 10 }) * + * @example + * + * randCow({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randCow( options?: Options diff --git a/packages/falso/src/lib/credit-card-brand.ts b/packages/falso/src/lib/credit-card-brand.ts index 7bbbe8a19..db71a9173 100644 --- a/packages/falso/src/lib/credit-card-brand.ts +++ b/packages/falso/src/lib/credit-card-brand.ts @@ -14,6 +14,10 @@ import { data } from './credit-card-brand.json'; * * randCreditCardBrand({ length: 10 }) * + * @example + * + * randCreditCardBrand({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randCreditCardBrand( options?: Options diff --git a/packages/falso/src/lib/credit-card-cvv.ts b/packages/falso/src/lib/credit-card-cvv.ts index bc002ac72..306395cb4 100644 --- a/packages/falso/src/lib/credit-card-cvv.ts +++ b/packages/falso/src/lib/credit-card-cvv.ts @@ -13,6 +13,10 @@ import { fake, FakeOptions, getRandomInRange } from './core/core'; * * randCreditCardCVV({ length: 10 }) * + * @example + * + * randCreditCardCVV({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randCreditCardCVV( options?: Options diff --git a/packages/falso/src/lib/credit-card-number.ts b/packages/falso/src/lib/credit-card-number.ts index 8cf4475ff..ab609d8cc 100644 --- a/packages/falso/src/lib/credit-card-number.ts +++ b/packages/falso/src/lib/credit-card-number.ts @@ -37,6 +37,10 @@ export type Brand = * * randCreditCardNumber({ length: 10 }) * + * @example + * + * randCreditCardNumber({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randCreditCardNumber< Options extends CreditCardNumberOptions = never diff --git a/packages/falso/src/lib/credit-card.ts b/packages/falso/src/lib/credit-card.ts index 6a538c962..7aa51430c 100644 --- a/packages/falso/src/lib/credit-card.ts +++ b/packages/falso/src/lib/credit-card.ts @@ -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; @@ -46,6 +47,10 @@ export interface CreditCard { * * randCreditCard({ length: 10 }) * + * @example + * + * randCreditCard({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randCreditCard( options?: Options @@ -81,5 +86,9 @@ export function randCreditCard( }; }; - return fake(factory, options); + return fake(factory, options, { uniqueComparer: checkUnique }); +} + +function checkUnique(card: CreditCard, cards: CreditCard[]): boolean { + return objectIsUnique(card, cards, ['number']); } diff --git a/packages/falso/src/lib/crocodilia.ts b/packages/falso/src/lib/crocodilia.ts index 7e7c9835c..a53c42236 100644 --- a/packages/falso/src/lib/crocodilia.ts +++ b/packages/falso/src/lib/crocodilia.ts @@ -14,6 +14,10 @@ import { data } from './crocodilia.json'; * * randCrocodilia({ length: 10 }) * + * @example + * + * randCrocodilia({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randCrocodilia( options?: Options diff --git a/packages/falso/src/lib/currency-code.ts b/packages/falso/src/lib/currency-code.ts index 68b91f82e..44f50dcfc 100644 --- a/packages/falso/src/lib/currency-code.ts +++ b/packages/falso/src/lib/currency-code.ts @@ -14,6 +14,10 @@ import { data } from './currency-code.json'; * * randCurrencyCode({ length: 10 }) * + * @example + * + * randCurrencyCode({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randCurrencyCode( options?: Options diff --git a/packages/falso/src/lib/currency-name.ts b/packages/falso/src/lib/currency-name.ts index b432c9832..8464fdde1 100644 --- a/packages/falso/src/lib/currency-name.ts +++ b/packages/falso/src/lib/currency-name.ts @@ -14,6 +14,10 @@ import { data } from './currency-name.json'; * * randCurrencyName({ length: 10 }) * + * @example + * + * randCurrencyName({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randCurrencyName( options?: Options diff --git a/packages/falso/src/lib/currency-symbol.ts b/packages/falso/src/lib/currency-symbol.ts index 57d291789..53a0d979b 100644 --- a/packages/falso/src/lib/currency-symbol.ts +++ b/packages/falso/src/lib/currency-symbol.ts @@ -14,6 +14,10 @@ import { data } from './currency-symbol.json'; * * randCurrencySymbol({ length: 10 }) * + * @example + * + * randCurrencySymbol({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randCurrencySymbol( options?: Options diff --git a/packages/falso/src/lib/database-collation.ts b/packages/falso/src/lib/database-collation.ts index d081f50cd..cade095c8 100644 --- a/packages/falso/src/lib/database-collation.ts +++ b/packages/falso/src/lib/database-collation.ts @@ -14,6 +14,10 @@ import { data } from './database-collation.json'; * * randDatabaseCollation({ length: 10 }) * + * @example + * + * randDatabaseCollation({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randDatabaseCollation( options?: Options diff --git a/packages/falso/src/lib/database-column.ts b/packages/falso/src/lib/database-column.ts index 36ff6c706..864e5b777 100644 --- a/packages/falso/src/lib/database-column.ts +++ b/packages/falso/src/lib/database-column.ts @@ -14,6 +14,10 @@ import { data } from './database-column.json'; * * randDatabaseColumn({ length: 10 }) * + * @example + * + * randDatabaseColumn({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randDatabaseColumn( options?: Options diff --git a/packages/falso/src/lib/database-engine.ts b/packages/falso/src/lib/database-engine.ts index 9d36dfd6d..46b799790 100644 --- a/packages/falso/src/lib/database-engine.ts +++ b/packages/falso/src/lib/database-engine.ts @@ -14,6 +14,10 @@ import { data } from './database-engine.json'; * * randDatabaseEngine({ length: 10 }) * + * @example + * + * randDatabaseEngine({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randDatabaseEngine( options?: Options diff --git a/packages/falso/src/lib/database-type.ts b/packages/falso/src/lib/database-type.ts index ca85e1e36..14511b025 100644 --- a/packages/falso/src/lib/database-type.ts +++ b/packages/falso/src/lib/database-type.ts @@ -14,6 +14,10 @@ import { data } from './database-type.json'; * * randDatabaseType({ length: 10 }) * + * @example + * + * randDatabaseType({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randDatabaseType( options?: Options diff --git a/packages/falso/src/lib/department.ts b/packages/falso/src/lib/department.ts index 2ea8f12bc..ed8e5f554 100644 --- a/packages/falso/src/lib/department.ts +++ b/packages/falso/src/lib/department.ts @@ -14,6 +14,10 @@ import { data } from './department.json'; * * randDepartment({ length: 10 }) * + * @example + * + * randDepartment({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randDepartment( options?: Options diff --git a/packages/falso/src/lib/direction.ts b/packages/falso/src/lib/direction.ts index c28c5cbe4..acec921c8 100644 --- a/packages/falso/src/lib/direction.ts +++ b/packages/falso/src/lib/direction.ts @@ -14,6 +14,10 @@ import { data } from './direction.json'; * * randDirection({ length: 10 }) * + * @example + * + * randDirection({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randDirection( options?: Options diff --git a/packages/falso/src/lib/directory-path.ts b/packages/falso/src/lib/directory-path.ts index be4f9bf0b..55bb7f751 100644 --- a/packages/falso/src/lib/directory-path.ts +++ b/packages/falso/src/lib/directory-path.ts @@ -14,6 +14,10 @@ import { data } from './directory-path.json'; * * randDirectoryPath({ length: 10 }) * + * @example + * + * randDirectoryPath({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randDirectoryPath( options?: Options diff --git a/packages/falso/src/lib/dog.ts b/packages/falso/src/lib/dog.ts index c05c9314a..85df27fc8 100644 --- a/packages/falso/src/lib/dog.ts +++ b/packages/falso/src/lib/dog.ts @@ -14,6 +14,10 @@ import { data } from './dog.json'; * * randDog({ length: 10 }) * + * @example + * + * randDog({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randDog( options?: Options diff --git a/packages/falso/src/lib/domain-name.ts b/packages/falso/src/lib/domain-name.ts index 675804e97..f27dc8014 100644 --- a/packages/falso/src/lib/domain-name.ts +++ b/packages/falso/src/lib/domain-name.ts @@ -15,6 +15,10 @@ import { randWord } from './word'; * * randDomainName({ length: 10 }) * + * @example + * + * randDomainName({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randDomainName( options?: Options diff --git a/packages/falso/src/lib/domain-suffix.ts b/packages/falso/src/lib/domain-suffix.ts index 06efa2467..f4332909a 100644 --- a/packages/falso/src/lib/domain-suffix.ts +++ b/packages/falso/src/lib/domain-suffix.ts @@ -14,6 +14,10 @@ import { data } from './domain-suffix.json'; * * randDomainSuffix({ length: 10 }) * + * @example + * + * randDomainSuffix({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randDomainSuffix( options?: Options diff --git a/packages/falso/src/lib/drinks.ts b/packages/falso/src/lib/drinks.ts index f60c59fa0..8378683d3 100644 --- a/packages/falso/src/lib/drinks.ts +++ b/packages/falso/src/lib/drinks.ts @@ -14,6 +14,10 @@ import { data } from './drinks.json'; * * randDrinks({ length: 10 }) * + * @example + * + * randDrinks({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randDrinks( options?: Options diff --git a/packages/falso/src/lib/email-provider.ts b/packages/falso/src/lib/email-provider.ts index 42bdb1f93..b0a156509 100644 --- a/packages/falso/src/lib/email-provider.ts +++ b/packages/falso/src/lib/email-provider.ts @@ -14,6 +14,10 @@ import { data } from './email-provider.json'; * * randEmailProvider({ length: 10 }) * + * @example + * + * randEmailProvider({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randEmailProvider( options?: Options diff --git a/packages/falso/src/lib/email.ts b/packages/falso/src/lib/email.ts index 6ade3cedf..ab78b94d2 100644 --- a/packages/falso/src/lib/email.ts +++ b/packages/falso/src/lib/email.ts @@ -49,10 +49,6 @@ function randFormattedName( * * @example * - * randEmail({ length: 10 }) - * - * @example - * * randEmail({ firstName: 'Netanel' }) * * @example @@ -71,6 +67,13 @@ function randFormattedName( * * randEmail({ suffix: 'com' }) * + * @example + * + * randEmail({ length: 10 }) + * + * @example + * + * randEmail({ length: 10, priority: 'unique' }) // default priority is 'length' * */ export function randEmail( diff --git a/packages/falso/src/lib/emoji.ts b/packages/falso/src/lib/emoji.ts index 37b82d8ef..5e83ec537 100644 --- a/packages/falso/src/lib/emoji.ts +++ b/packages/falso/src/lib/emoji.ts @@ -14,6 +14,10 @@ import { data } from './emoji.json'; * * randEmoji({ length: 10 }) * + * @example + * + * randEmoji({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randEmoji( options?: Options diff --git a/packages/falso/src/lib/ethereum-address.ts b/packages/falso/src/lib/ethereum-address.ts index 71a4132b0..6674fc1e8 100644 --- a/packages/falso/src/lib/ethereum-address.ts +++ b/packages/falso/src/lib/ethereum-address.ts @@ -14,6 +14,10 @@ import { data } from './ethereum-address.json'; * * randEthereumAddress({ length: 10 }) * + * @example + * + * randEthereumAddress({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randEthereumAddress( options?: Options diff --git a/packages/falso/src/lib/file-ext.ts b/packages/falso/src/lib/file-ext.ts index a973d8143..745238415 100644 --- a/packages/falso/src/lib/file-ext.ts +++ b/packages/falso/src/lib/file-ext.ts @@ -14,6 +14,10 @@ import { data } from './file-ext.json'; * * randFileExt({ length: 10 }) * + * @example + * + * randFileExt({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randFileExt( options?: Options diff --git a/packages/falso/src/lib/file-name.ts b/packages/falso/src/lib/file-name.ts index d14b87234..55155fea4 100644 --- a/packages/falso/src/lib/file-name.ts +++ b/packages/falso/src/lib/file-name.ts @@ -14,6 +14,10 @@ import { data } from './file-name.json'; * * randFileName({ length: 10 }) * + * @example + * + * randFileName({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randFileName< Options extends FakeOptions & { extension?: string } = never diff --git a/packages/falso/src/lib/file-path.ts b/packages/falso/src/lib/file-path.ts index 703111b91..a39fc5eb9 100644 --- a/packages/falso/src/lib/file-path.ts +++ b/packages/falso/src/lib/file-path.ts @@ -14,6 +14,10 @@ import { data } from './file-path.json'; * * randFilePath({ length: 10 }) * + * @example + * + * randFilePath({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randFilePath( options?: Options diff --git a/packages/falso/src/lib/file-type.ts b/packages/falso/src/lib/file-type.ts index 414ca4d9e..f189ae4d3 100644 --- a/packages/falso/src/lib/file-type.ts +++ b/packages/falso/src/lib/file-type.ts @@ -14,6 +14,10 @@ import { data } from './file-type.json'; * * randFileType({ length: 10 }) * + * @example + * + * randFileType({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randFileType( options?: Options diff --git a/packages/falso/src/lib/first-name.ts b/packages/falso/src/lib/first-name.ts index 970916127..98a902b9b 100644 --- a/packages/falso/src/lib/first-name.ts +++ b/packages/falso/src/lib/first-name.ts @@ -24,6 +24,10 @@ import { rand } from './rand'; * * randFirstName({ length: 10 }) * + * @example + * + * randFirstName({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randFirstName( options?: Options diff --git a/packages/falso/src/lib/fish.ts b/packages/falso/src/lib/fish.ts index f7a2e4f51..b19d5353f 100644 --- a/packages/falso/src/lib/fish.ts +++ b/packages/falso/src/lib/fish.ts @@ -14,6 +14,10 @@ import { data } from './fish.json'; * * randFish({ length: 10 }) * + * @example + * + * randFish({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randFish( options?: Options diff --git a/packages/falso/src/lib/flight-details.ts b/packages/falso/src/lib/flight-details.ts index cb43e378d..6b92c6c19 100644 --- a/packages/falso/src/lib/flight-details.ts +++ b/packages/falso/src/lib/flight-details.ts @@ -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 { objectIsUnique } from './core/unique-validators'; export interface FlightDetailsOptions extends FakeOptions { airline?: Airline; @@ -49,6 +50,10 @@ function generateFlightLength(): number { * * randFlightDetails({ length: 10 }) * + * @example + * + * randFlightDetails({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randFlightDetails( options?: Options @@ -71,5 +76,9 @@ export function randFlightDetails( }; }; - return fake(factory, options); + return fake(factory, options, { uniqueComparer: checkUnique }); +} + +function checkUnique(flight: FlightDetails, flights: FlightDetails[]): boolean { + return objectIsUnique(flight, flights, ['passenger', 'flightNumber', 'date']); } diff --git a/packages/falso/src/lib/flight-number.ts b/packages/falso/src/lib/flight-number.ts index ec3b43ac1..be5b48a4d 100644 --- a/packages/falso/src/lib/flight-number.ts +++ b/packages/falso/src/lib/flight-number.ts @@ -74,6 +74,10 @@ function generateStandardFlightNumber({ * * randFlightNumber({ length: 10 }) * + * @example + * + * randFlightNumber({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randFlightNumber( options?: Options diff --git a/packages/falso/src/lib/float.ts b/packages/falso/src/lib/float.ts index e4793adc1..7082ea152 100644 --- a/packages/falso/src/lib/float.ts +++ b/packages/falso/src/lib/float.ts @@ -18,15 +18,20 @@ export interface RandomFloatOptions extends RandomInRangeOptions, FakeOptions {} * * @example * - * randFloat({ length: 10 }) + * randFloat({ min: 10, max: 20, fraction: 1 }) // 12.5 * * @example * - * randFloat({ min: 10, max: 20, fraction: 1 }) // 12.5 + * randFloat({ min: 10, max: 20, fraction: 2 }) // 12.52 * * @example * - * randFloat({ min: 10, max: 20, fraction: 2 }) // 12.52 + * randFloat({ length: 10 }) + * + * @example + * + * randFloat({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randFloat( options?: Options diff --git a/packages/falso/src/lib/font-family.ts b/packages/falso/src/lib/font-family.ts index 9cc60407a..10406e088 100644 --- a/packages/falso/src/lib/font-family.ts +++ b/packages/falso/src/lib/font-family.ts @@ -14,6 +14,10 @@ import { data } from './font-family.json'; * * randFontFamily({ length: 10 }) * + * @example + * + * randFontFamily({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randFontFamily( options?: Options diff --git a/packages/falso/src/lib/font-size.ts b/packages/falso/src/lib/font-size.ts index a0319d7f9..27ca209f2 100644 --- a/packages/falso/src/lib/font-size.ts +++ b/packages/falso/src/lib/font-size.ts @@ -28,6 +28,10 @@ export interface FontSizeOptions extends FakeOptions { * * randFontSize({ length: 10 }) * + * @example + * + * randFontSize({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randFontSize( options?: Options diff --git a/packages/falso/src/lib/food.ts b/packages/falso/src/lib/food.ts index f7d10b318..c9cab1a81 100644 --- a/packages/falso/src/lib/food.ts +++ b/packages/falso/src/lib/food.ts @@ -45,6 +45,10 @@ const totalOrigins = Object.keys(data)?.length; * * randFood({ length: 10 }) * + * @example + * + * randFood({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randFood( options?: Options @@ -53,11 +57,11 @@ export function randFood( const origin: string | undefined = options?.origin; if (!totalOrigins) { - throw 'No foods found'; + throw new Error('No foods found'); } if (origin && !foodData[origin]) { - throw 'No foods found for selected origin'; + throw Error('No foods found for selected origin'); } const factory: () => string = () => { diff --git a/packages/falso/src/lib/football-team.ts b/packages/falso/src/lib/football-team.ts index 5aab7e0a1..151a33463 100644 --- a/packages/falso/src/lib/football-team.ts +++ b/packages/falso/src/lib/football-team.ts @@ -14,6 +14,10 @@ import { data } from './football-team.json'; * * randFootballTeam({ length: 10 }) * + * @example + * + * randFootballTeam({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randFootballTeam( options?: Options diff --git a/packages/falso/src/lib/frequency.ts b/packages/falso/src/lib/frequency.ts index 4f4742373..d0a72dd63 100644 --- a/packages/falso/src/lib/frequency.ts +++ b/packages/falso/src/lib/frequency.ts @@ -14,6 +14,10 @@ import { data } from './frequency.json'; * * randFrequency({ length: 10 }) * + * @example + * + * randFrequency({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randFrequency( options?: Options diff --git a/packages/falso/src/lib/full-address.ts b/packages/falso/src/lib/full-address.ts index f150e3db0..521c49f78 100644 --- a/packages/falso/src/lib/full-address.ts +++ b/packages/falso/src/lib/full-address.ts @@ -22,6 +22,10 @@ import { AddressOptions, randAddress } from './address'; * * randFullAddress({ length: 10 }) * + * @example + * + * randFullAddress({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randFullAddress( options?: Options @@ -29,7 +33,7 @@ export function randFullAddress( 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, diff --git a/packages/falso/src/lib/full-name.ts b/packages/falso/src/lib/full-name.ts index 6738af3d6..e828b3bb2 100644 --- a/packages/falso/src/lib/full-name.ts +++ b/packages/falso/src/lib/full-name.ts @@ -28,6 +28,10 @@ export interface NameOptions extends FakeOptions { * * randFullName({ length: 10 }) * + * @example + * + * randFullName({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randFullName( options?: Options diff --git a/packages/falso/src/lib/future-date.ts b/packages/falso/src/lib/future-date.ts index 4374bd4fe..c94490c51 100644 --- a/packages/falso/src/lib/future-date.ts +++ b/packages/falso/src/lib/future-date.ts @@ -1,5 +1,6 @@ import { randBetweenDate } from './between-date'; import { fake, FakeOptions } from './core/core'; +import { dateIsUnique } from './core/unique-validators'; interface FutureOptions extends FakeOptions { years?: number; @@ -16,11 +17,15 @@ interface FutureOptions extends FakeOptions { * * @example * + * randFutureDate({ years: 10 }) // default is 1 + * + * @example + * * randFutureDate({ length: 10 }) * * @example * - * randFutureDate({ years: 10 }) // default is 1 + * randFutureDate({ length: 10, priority: 'unique' }) // default priority is 'length' * */ export function randFutureDate( @@ -35,5 +40,7 @@ export function randFutureDate( 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, { uniqueComparer: dateIsUnique }); } diff --git a/packages/falso/src/lib/gender.ts b/packages/falso/src/lib/gender.ts index ac1ac55dc..192ba4e71 100644 --- a/packages/falso/src/lib/gender.ts +++ b/packages/falso/src/lib/gender.ts @@ -17,11 +17,15 @@ interface GenderOptions extends FakeOptions { * * @example * + * randGender({ code: true }) // default is false + * + * @example + * * randGender({ length: 10 }) * * @example * - * randGender({ code: true }) // default is false + * randGender({ length: 10, priority: 'unique' }) // default priority is 'length' * */ export function randGender( diff --git a/packages/falso/src/lib/git-branch.ts b/packages/falso/src/lib/git-branch.ts index 0e43a4df7..d088c8104 100644 --- a/packages/falso/src/lib/git-branch.ts +++ b/packages/falso/src/lib/git-branch.ts @@ -14,6 +14,10 @@ import { data } from './git-branch.json'; * * randGitBranch({ length: 10 }) * + * @example + * + * randGitBranch({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randGitBranch( options?: Options diff --git a/packages/falso/src/lib/git-commit-entry.ts b/packages/falso/src/lib/git-commit-entry.ts index 22c7573db..2367aa267 100644 --- a/packages/falso/src/lib/git-commit-entry.ts +++ b/packages/falso/src/lib/git-commit-entry.ts @@ -17,6 +17,10 @@ import { randGitCommitMessage } from './git-commit-message'; * * randGitCommitEntry({ length: 10 }) * + * @example + * + * randGitCommitEntry({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randGitCommitEntry( options?: Options diff --git a/packages/falso/src/lib/git-commit-message.ts b/packages/falso/src/lib/git-commit-message.ts index da6b0cb60..4ba0a8cbc 100644 --- a/packages/falso/src/lib/git-commit-message.ts +++ b/packages/falso/src/lib/git-commit-message.ts @@ -15,6 +15,10 @@ import { randWord } from './word'; * * randGitCommitMessage({ length: 10 }) * + * @example + * + * randGitCommitMessage({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randGitCommitMessage( options?: Options diff --git a/packages/falso/src/lib/git-commit-sha.ts b/packages/falso/src/lib/git-commit-sha.ts index b6fa296e0..85ac35c73 100644 --- a/packages/falso/src/lib/git-commit-sha.ts +++ b/packages/falso/src/lib/git-commit-sha.ts @@ -16,6 +16,10 @@ const commitShaLen = 40; * * randGitCommitSha({ length: 10 }) * + * @example + * + * randGitCommitSha({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randGitCommitSha( options?: Options diff --git a/packages/falso/src/lib/git-short-sha.ts b/packages/falso/src/lib/git-short-sha.ts index c24013895..82cd26de8 100644 --- a/packages/falso/src/lib/git-short-sha.ts +++ b/packages/falso/src/lib/git-short-sha.ts @@ -16,6 +16,10 @@ const commitShortShaLen = 7; * * randGitShortSha({ length: 10 }) * + * @example + * + * randGitShortSha({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randGitShortSha( options?: Options diff --git a/packages/falso/src/lib/hex.ts b/packages/falso/src/lib/hex.ts index 08441917b..3e26ca0b6 100644 --- a/packages/falso/src/lib/hex.ts +++ b/packages/falso/src/lib/hex.ts @@ -14,6 +14,10 @@ import { random } from './random'; * * randHex({ length: 10 }) * + * @example + * + * randHex({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randHex( options?: Options diff --git a/packages/falso/src/lib/hexa-decimal.ts b/packages/falso/src/lib/hexa-decimal.ts index 32faedb87..d8a047cc2 100644 --- a/packages/falso/src/lib/hexa-decimal.ts +++ b/packages/falso/src/lib/hexa-decimal.ts @@ -2,7 +2,7 @@ import { fake, FakeOptions } from './core/core'; import { randNumber } from './number'; function generator() { - return randNumber({min:0, max:15}).toString(16); + return randNumber({ min: 0, max: 15 }).toString(16); } /** @@ -18,6 +18,10 @@ function generator() { * * randHexaDecimal({ length: 10 }) * + * @example + * + * randHexaDecimal({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randHexaDecimal( options?: Options diff --git a/packages/falso/src/lib/horse.ts b/packages/falso/src/lib/horse.ts index 2cf31fe95..0dc35b6b4 100644 --- a/packages/falso/src/lib/horse.ts +++ b/packages/falso/src/lib/horse.ts @@ -14,6 +14,10 @@ import { data } from './horse.json'; * * randHorse({ length: 10 }) * + * @example + * + * randHorse({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randHorse( options?: Options diff --git a/packages/falso/src/lib/hsl.ts b/packages/falso/src/lib/hsl.ts index b880fa9ba..fa96f560b 100644 --- a/packages/falso/src/lib/hsl.ts +++ b/packages/falso/src/lib/hsl.ts @@ -16,11 +16,15 @@ export interface HSLOptions extends FakeOptions { * * @example * + * randHsl({ alpha: true }) // default is false + * + * @example + * * randHsl({ length: 3 }) * * @example * - * randHsl({ alpha: true }) // default is false + * randHsl({ length: 10, priority: 'unique' }) // default priority is 'length' * */ export function randHsl(options?: Options) { diff --git a/packages/falso/src/lib/http-method.ts b/packages/falso/src/lib/http-method.ts index a57a0202e..02855347a 100644 --- a/packages/falso/src/lib/http-method.ts +++ b/packages/falso/src/lib/http-method.ts @@ -14,6 +14,10 @@ import { data } from './http-method.json'; * * randHttpMethod({ length: 10 }) * + * @example + * + * randHttpMethod({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randHttpMethod( options?: Options diff --git a/packages/falso/src/lib/iban.ts b/packages/falso/src/lib/iban.ts index 07d9cb700..97e98dbce 100644 --- a/packages/falso/src/lib/iban.ts +++ b/packages/falso/src/lib/iban.ts @@ -24,6 +24,10 @@ export interface IbanOptions extends FakeOptions { * * randIban({ length: 10 }) * + * @example + * + * randIban({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randIban( options?: Options diff --git a/packages/falso/src/lib/ice-hockey-team.ts b/packages/falso/src/lib/ice-hockey-team.ts index 415e3a46e..e7e06f52e 100644 --- a/packages/falso/src/lib/ice-hockey-team.ts +++ b/packages/falso/src/lib/ice-hockey-team.ts @@ -14,6 +14,10 @@ import { data } from './ice-hockey-team.json'; * * randIceHockeyTeam({ length: 10 }) * + * @example + * + * randIceHockeyTeam({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randIceHockeyTeam( options?: Options diff --git a/packages/falso/src/lib/img.ts b/packages/falso/src/lib/img.ts index 550576e15..42b23a568 100644 --- a/packages/falso/src/lib/img.ts +++ b/packages/falso/src/lib/img.ts @@ -15,6 +15,10 @@ type Category = 'animals' | 'arch' | 'nature' | 'people' | 'tech'; * * randImg({ length: 10 }) * + * @example + * + * randImg({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randImg< Options extends FakeOptions & { diff --git a/packages/falso/src/lib/integration.ts b/packages/falso/src/lib/integration.ts index 1d8ec9df5..510fa1e4f 100644 --- a/packages/falso/src/lib/integration.ts +++ b/packages/falso/src/lib/integration.ts @@ -14,6 +14,10 @@ import { data } from './integration.json'; * * randIntegrations({ length: 10 }) * + * @example + * + * randIntegrations({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randIntegration( options?: Options diff --git a/packages/falso/src/lib/ip.ts b/packages/falso/src/lib/ip.ts index 3d9a10990..55a5d6c7c 100644 --- a/packages/falso/src/lib/ip.ts +++ b/packages/falso/src/lib/ip.ts @@ -16,6 +16,10 @@ const ipRange = { min: 0, max: 255 }; * * randIp({ length: 10 }) * + * @example + * + * randIp({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randIp(options?: Options) { return fake( diff --git a/packages/falso/src/lib/ipv6.ts b/packages/falso/src/lib/ipv6.ts index dfaebfbc7..20d1c06f1 100644 --- a/packages/falso/src/lib/ipv6.ts +++ b/packages/falso/src/lib/ipv6.ts @@ -14,6 +14,10 @@ import { randHexaDecimal } from './hexa-decimal'; * * randIpv6({ length: 10 }) * + * @example + * + * randIpv6({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randIpv6( options?: Options diff --git a/packages/falso/src/lib/job-area.ts b/packages/falso/src/lib/job-area.ts index 99d566e6d..ddd0088f8 100644 --- a/packages/falso/src/lib/job-area.ts +++ b/packages/falso/src/lib/job-area.ts @@ -14,6 +14,10 @@ import { data } from './job-area.json'; * * randJobArea({ length: 10 }) * + * @example + * + * randJobArea({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randJobArea( options?: Options diff --git a/packages/falso/src/lib/job-descriptor.ts b/packages/falso/src/lib/job-descriptor.ts index 053e67021..ed42b37df 100644 --- a/packages/falso/src/lib/job-descriptor.ts +++ b/packages/falso/src/lib/job-descriptor.ts @@ -14,6 +14,10 @@ import { data } from './job-descriptor.json'; * * randJobDescriptor({ length: 10 }) * + * @example + * + * randJobDescriptor({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randJobDescriptor( options?: Options diff --git a/packages/falso/src/lib/job-title.ts b/packages/falso/src/lib/job-title.ts index c0553878b..e9b744a48 100644 --- a/packages/falso/src/lib/job-title.ts +++ b/packages/falso/src/lib/job-title.ts @@ -14,6 +14,10 @@ import { data } from './job-title.json'; * * randJobTitle({ length: 10 }) * + * @example + * + * randJobTitle({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randJobTitle( options?: Options diff --git a/packages/falso/src/lib/job-type.ts b/packages/falso/src/lib/job-type.ts index 7907de9a1..8b9605f13 100644 --- a/packages/falso/src/lib/job-type.ts +++ b/packages/falso/src/lib/job-type.ts @@ -14,6 +14,10 @@ import { data } from './job-type.json'; * * randJobType({ length: 10 }) * + * @example + * + * randJobType({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randJobType( options?: Options diff --git a/packages/falso/src/lib/json.ts b/packages/falso/src/lib/json.ts index 779fa30ae..fdda11ebe 100644 --- a/packages/falso/src/lib/json.ts +++ b/packages/falso/src/lib/json.ts @@ -59,13 +59,21 @@ const generateRandomValue = (): any => { * @example * randJSON() * - * @example If a fixed number of keys are required + * @example + * + * randJSON({ totalKeys: 10 }) // If a fixed number of keys are required + * + * @example + * + * randJSON({ minKeys: 1, maxKeys: 10 }) // If a random number of keys are required * - * randJSON({ totalKeys: 10 }) + * @example + * + * randJSON({ length: 10 }) * - * @example If a random number of keys are required + * @example * - * randJSON({ minKeys: 1, maxKeys: 10 }) + * randJSON({ length: 10, priority: 'unique' }) // default priority is 'length' * */ export function randJSON( @@ -79,7 +87,7 @@ export function randJSON( }); const factory = () => { - const generatedObject: { [key: string]: any } = {}; + const generatedObject: Record = {}; for (let index = 0; index < objectSize; index++) { generatedObject[randUuid().replace(/-/g, '')] = generateRandomValue(); @@ -88,5 +96,9 @@ export function randJSON( return generatedObject; }; - return fake(factory, options); + return fake(factory, options, { uniqueComparer: checkUnique }); +} + +export function checkUnique(item: object, items: object[]): boolean { + return !items.some((i) => JSON.stringify(i) === JSON.stringify(item)); } diff --git a/packages/falso/src/lib/language.ts b/packages/falso/src/lib/language.ts index e32436fb8..ea5a43653 100644 --- a/packages/falso/src/lib/language.ts +++ b/packages/falso/src/lib/language.ts @@ -17,11 +17,15 @@ interface LanguageOptions extends FakeOptions { * * @example * + * randLanguage({ code: true }) // default is false + * + * @example + * * randLanguage({ length: 10 }) * * @example * - * randLanguage({ code: true }) // default is false + * randLanguage({ length: 10, priority: 'unique' }) // default priority is 'length' * */ export function randLanguage( diff --git a/packages/falso/src/lib/last-name.ts b/packages/falso/src/lib/last-name.ts index 0ff755d50..9a2f3d1bf 100644 --- a/packages/falso/src/lib/last-name.ts +++ b/packages/falso/src/lib/last-name.ts @@ -20,6 +20,10 @@ import { NameOptions } from './full-name'; * * randLastName({ length: 10 }) * + * @example + * + * randLastName({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randLastName( options?: Options diff --git a/packages/falso/src/lib/latitude.ts b/packages/falso/src/lib/latitude.ts index 16411fd26..3d9a3112e 100644 --- a/packages/falso/src/lib/latitude.ts +++ b/packages/falso/src/lib/latitude.ts @@ -13,6 +13,10 @@ import { FakeOptions, fake, getRandomInRange } from './core/core'; * * randLatitude({ length: 10 }) * + * @example + * + * randLatitude({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randLatitude( options?: Options diff --git a/packages/falso/src/lib/line.ts b/packages/falso/src/lib/line.ts index 42ff0ab99..77eec48db 100644 --- a/packages/falso/src/lib/line.ts +++ b/packages/falso/src/lib/line.ts @@ -18,6 +18,10 @@ export interface LineOptions extends FakeOptions { * * randLine({ lineCount: 10 }) // default is 5 * + * @example + * + * randLine({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randLine( options?: Options @@ -25,7 +29,7 @@ export function randLine( const lineCount: number = options?.lineCount ?? 5; if (lineCount < 1 || isNaN(lineCount)) { - throw 'Line count must be greater than 0'; + throw Error('Line count must be greater than 0'); } const factory = () => { diff --git a/packages/falso/src/lib/lines.ts b/packages/falso/src/lib/lines.ts index a09bc6705..0a1af03c2 100644 --- a/packages/falso/src/lib/lines.ts +++ b/packages/falso/src/lib/lines.ts @@ -14,6 +14,10 @@ import { data } from './lines.json'; * * randLines({ length: 10 }) * + * @example + * + * randLines({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randLines( options?: Options diff --git a/packages/falso/src/lib/lion.ts b/packages/falso/src/lib/lion.ts index 791099481..32c1a247e 100644 --- a/packages/falso/src/lib/lion.ts +++ b/packages/falso/src/lib/lion.ts @@ -14,6 +14,10 @@ import { data } from './lion.json'; * * randLion({ length: 10 }) * + * @example + * + * randLion({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randLion( options?: Options diff --git a/packages/falso/src/lib/locale.ts b/packages/falso/src/lib/locale.ts index 481e5f084..b9a302a70 100644 --- a/packages/falso/src/lib/locale.ts +++ b/packages/falso/src/lib/locale.ts @@ -14,6 +14,10 @@ import { data } from './locale.json'; * * randLocale({ length: 10 }) * + * @example + * + * randLocale({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randLocale( options?: Options diff --git a/packages/falso/src/lib/longitude.ts b/packages/falso/src/lib/longitude.ts index e9802e30c..a11e43b01 100644 --- a/packages/falso/src/lib/longitude.ts +++ b/packages/falso/src/lib/longitude.ts @@ -13,6 +13,10 @@ import { FakeOptions, fake, getRandomInRange } from './core/core'; * * randLongitude({ length: 10 }) * + * @example + * + * randLongitude({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randLongitude( options?: Options diff --git a/packages/falso/src/lib/mac.ts b/packages/falso/src/lib/mac.ts index bd2d33d49..de95c3b71 100644 --- a/packages/falso/src/lib/mac.ts +++ b/packages/falso/src/lib/mac.ts @@ -14,6 +14,10 @@ import { randHexaDecimal } from './hexa-decimal'; * * randMac({ length: 10 }) * + * @example + * + * randMac({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randMac( options?: Options diff --git a/packages/falso/src/lib/mask.ts b/packages/falso/src/lib/mask.ts index 1c1f7102c..cfc4f3727 100644 --- a/packages/falso/src/lib/mask.ts +++ b/packages/falso/src/lib/mask.ts @@ -18,6 +18,10 @@ import { randAlpha } from './alpha'; * * randMask({ length: 10 }) * + * @example + * + * randMask({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randMask< Options extends FakeOptions & { diff --git a/packages/falso/src/lib/mime-type.ts b/packages/falso/src/lib/mime-type.ts index ced906cbe..150c222c2 100644 --- a/packages/falso/src/lib/mime-type.ts +++ b/packages/falso/src/lib/mime-type.ts @@ -14,6 +14,10 @@ import { data } from './mime-type.json'; * * randMimeType({ length: 10 }) * + * @example + * + * randMimeType({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randMimeType( options?: Options diff --git a/packages/falso/src/lib/month.ts b/packages/falso/src/lib/month.ts index 9ab5d9f76..52ecc288e 100644 --- a/packages/falso/src/lib/month.ts +++ b/packages/falso/src/lib/month.ts @@ -22,6 +22,10 @@ export interface MonthOptions extends FakeOptions { * * randMonth({ length: 10 }) * + * @example + * + * randMonth({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randMonth( options?: Options diff --git a/packages/falso/src/lib/movie-character.ts b/packages/falso/src/lib/movie-character.ts index 13de7a6ad..097e32635 100644 --- a/packages/falso/src/lib/movie-character.ts +++ b/packages/falso/src/lib/movie-character.ts @@ -13,6 +13,10 @@ import { data } from './movie-character.json'; * * randMovieCharacter({ length: 10 }) * + * @example + * + * randMovieCharacter({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randMovieCharacter( options?: Options diff --git a/packages/falso/src/lib/movie.ts b/packages/falso/src/lib/movie.ts index 89a0e1e31..492d5b1e4 100644 --- a/packages/falso/src/lib/movie.ts +++ b/packages/falso/src/lib/movie.ts @@ -13,6 +13,10 @@ import { data } from './movie.json'; * * randMovie({ length: 10 }) * + * @example + * + * randMovie({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randMovie( options?: Options diff --git a/packages/falso/src/lib/music-genre.ts b/packages/falso/src/lib/music-genre.ts index a2ce1ef8f..630be278a 100644 --- a/packages/falso/src/lib/music-genre.ts +++ b/packages/falso/src/lib/music-genre.ts @@ -14,6 +14,10 @@ import { data } from './music-genre.json'; * * randMusicGenre({ length: 10 }) * + * @example + * + * randMusicGenre({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randMusicGenre( options?: Options diff --git a/packages/falso/src/lib/nearby-gpscoordinate.ts b/packages/falso/src/lib/nearby-gpscoordinate.ts index a37f3d3f5..5d808e75c 100644 --- a/packages/falso/src/lib/nearby-gpscoordinate.ts +++ b/packages/falso/src/lib/nearby-gpscoordinate.ts @@ -15,9 +15,22 @@ import { randLongitude } from './longitude'; * * randNearbyGPSCoordinate({ length: 10 }) * + * @example + * + * randNearbyGPSCoordinate({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randNearbyGPSCoordinate( options?: Options ) { - return fake(() => [randLatitude(), randLongitude()], options); + return fake(() => [randLatitude(), randLongitude()], options, { + uniqueComparer: checkUnique, + }); +} + +export function checkUnique( + coordinate: number[], + coordinates: number[][] +): boolean { + return !coordinates.some((c) => c.join('') === coordinate.join('')); } diff --git a/packages/falso/src/lib/number.ts b/packages/falso/src/lib/number.ts index 50bd1c204..9f723b6db 100644 --- a/packages/falso/src/lib/number.ts +++ b/packages/falso/src/lib/number.ts @@ -43,6 +43,11 @@ export interface RandomNumberOptions extends RandomInRangeOptions, FakeOptions { * @example * * randNumber({ min: 1000, max: 2000, precision: 10 }) // 1_250 + * + * @example + * + * randNumber({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randNumber( options?: Options diff --git a/packages/falso/src/lib/oauth-provider.ts b/packages/falso/src/lib/oauth-provider.ts index e6eca46fa..6d17b5b18 100644 --- a/packages/falso/src/lib/oauth-provider.ts +++ b/packages/falso/src/lib/oauth-provider.ts @@ -14,6 +14,10 @@ import { data } from './oauth-provider.json'; * * randOAuthProvider({ length: 10 }) * + * @example + * + * randOAuthProvider({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randOAuthProvider( options?: Options diff --git a/packages/falso/src/lib/octal.ts b/packages/falso/src/lib/octal.ts index 93d92ed16..7c20e8a02 100644 --- a/packages/falso/src/lib/octal.ts +++ b/packages/falso/src/lib/octal.ts @@ -13,6 +13,10 @@ import { fake, FakeOptions } from './core/core'; * * randOctal({ length: 10 }) * + * @example + * + * randOctal({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randOctal( options?: Options diff --git a/packages/falso/src/lib/ordinal-direction.ts b/packages/falso/src/lib/ordinal-direction.ts index 0db1a03da..7925df907 100644 --- a/packages/falso/src/lib/ordinal-direction.ts +++ b/packages/falso/src/lib/ordinal-direction.ts @@ -14,6 +14,10 @@ import { data } from './ordinal-direction.json'; * * randOrdinalDirection({ length: 10 }) * + * @example + * + * randOrdinalDirection({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randOrdinalDirection( options?: Options diff --git a/packages/falso/src/lib/paragraph.ts b/packages/falso/src/lib/paragraph.ts index 1c2a4f6a6..4f2de4947 100644 --- a/packages/falso/src/lib/paragraph.ts +++ b/packages/falso/src/lib/paragraph.ts @@ -8,11 +8,15 @@ import { data } from './paragraph.json'; * * @example * - * randJobTitle() + * randParagraph() * * @example * - * randJobTitle({ length: 10 }) + * randParagraph({ length: 10 }) + * + * @example + * + * randParagraph({ length: 10, priority: 'unique' }) // default priority is 'length' * */ export function randParagraph( diff --git a/packages/falso/src/lib/password.ts b/packages/falso/src/lib/password.ts index 8120cfa8b..1b3d97df3 100644 --- a/packages/falso/src/lib/password.ts +++ b/packages/falso/src/lib/password.ts @@ -16,11 +16,15 @@ export interface PasswordOptions extends FakeOptions { * * @example * + * randPassword({ size: 10 }) // default is 15 + * + * @example + * * randPassword({ length: 10 }) * * @example * - * randPassword({ size: 10 }) // default is 15 + * randPassword({ length: 10, priority: 'unique' }) // default priority is 'length' * */ export function randPassword( diff --git a/packages/falso/src/lib/past-date.ts b/packages/falso/src/lib/past-date.ts index abc6b8ee9..a7daf4808 100644 --- a/packages/falso/src/lib/past-date.ts +++ b/packages/falso/src/lib/past-date.ts @@ -1,5 +1,6 @@ import { randBetweenDate } from './between-date'; import { fake, FakeOptions } from './core/core'; +import { dateIsUnique } from './core/unique-validators'; interface PastOptions extends FakeOptions { years?: number; @@ -16,11 +17,15 @@ interface PastOptions extends FakeOptions { * * @example * + * randPastDate({ years: 2 }) // default is 1 + * + * @example + * * randPastDate({ length: 10 }) * * @example * - * randPastDate({ years: 2 }) // default is 1 + * randPastDate({ length: 10, priority: 'unique' }) // default priority is 'length' * */ export function randPastDate( @@ -36,5 +41,7 @@ export function randPastDate( const to = new Date(); const from = new Date(to.getTime() - yearsInMilliseconds); - return fake(() => randBetweenDate({ from, to }), options); + return fake(() => randBetweenDate({ from, to }), options, { + uniqueComparer: dateIsUnique, + }); } diff --git a/packages/falso/src/lib/permission.ts b/packages/falso/src/lib/permission.ts index fb64021ca..04269cab8 100644 --- a/packages/falso/src/lib/permission.ts +++ b/packages/falso/src/lib/permission.ts @@ -16,11 +16,15 @@ interface PermissionOptions extends FakeOptions { * * @example * + * randPermission({ numeric: true }) + * + * @example + * * randPermission({ length: 10 }) * * @example * - * randPermission({ numeric: true }) + * randPermission({ length: 10, priority: 'unique' }) // default priority is 'length' * */ export function randPermission( diff --git a/packages/falso/src/lib/person-title.ts b/packages/falso/src/lib/person-title.ts index 0a7b4be26..9ffcd26f1 100644 --- a/packages/falso/src/lib/person-title.ts +++ b/packages/falso/src/lib/person-title.ts @@ -14,6 +14,10 @@ import { data } from './person-title.json'; * * randPersonTitle({ length: 10 }) * + * @example + * + * randPersonTitle({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randPersonTitle( options?: Options diff --git a/packages/falso/src/lib/phone-number.json b/packages/falso/src/lib/phone-number.json index 4122a0078..bfbb335d7 100644 --- a/packages/falso/src/lib/phone-number.json +++ b/packages/falso/src/lib/phone-number.json @@ -210,7 +210,8 @@ }, { "formats": [ - "+49(####)### ####", "+49(###)### ####", + "+49(####)### ####", + "+49(###)### ####", "+49(###)## ####", "+49(###)## ###", "+49(###)## ##", diff --git a/packages/falso/src/lib/phone-number.ts b/packages/falso/src/lib/phone-number.ts index b9864fe67..29232e540 100644 --- a/packages/falso/src/lib/phone-number.ts +++ b/packages/falso/src/lib/phone-number.ts @@ -252,6 +252,10 @@ type CountryCode = * * randPhoneNumber({ length: 10 }) * + * @example + * + * randPhoneNumber({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randPhoneNumber< Options extends FakeOptions & { diff --git a/packages/falso/src/lib/phrase.ts b/packages/falso/src/lib/phrase.ts index 620e2f778..d354aa9f3 100644 --- a/packages/falso/src/lib/phrase.ts +++ b/packages/falso/src/lib/phrase.ts @@ -14,6 +14,10 @@ import { data } from './phrase.json'; * * randPhrase({ length: 10 }) * + * @example + * + * randPhrase({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randPhrase( options?: Options diff --git a/packages/falso/src/lib/port.ts b/packages/falso/src/lib/port.ts index ab556f201..79e5d1d12 100644 --- a/packages/falso/src/lib/port.ts +++ b/packages/falso/src/lib/port.ts @@ -14,6 +14,10 @@ import { randNumber } from './number'; * * randPort({ length: 10 }) * + * @example + * + * randPort({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randPort( options?: Options diff --git a/packages/falso/src/lib/post.ts b/packages/falso/src/lib/post.ts index 648053b27..7a29fbbd2 100644 --- a/packages/falso/src/lib/post.ts +++ b/packages/falso/src/lib/post.ts @@ -3,6 +3,7 @@ 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; @@ -24,11 +25,15 @@ export interface Post { * * randPost({ length: 10 }) * + * @example + * + * randPost({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randPost( options?: Options ) { - return fake(() => { + const factory = () => { const post: Post = { id: randUuid(), title: randText({ charCount: 40 }), @@ -43,5 +48,7 @@ export function randPost( }; return post; - }, options); + }; + + return fake(factory, options, { uniqueComparer: objectWithIdIsUnique }); } diff --git a/packages/falso/src/lib/priority.ts b/packages/falso/src/lib/priority.ts index 467ce7fcd..a1e165d5c 100644 --- a/packages/falso/src/lib/priority.ts +++ b/packages/falso/src/lib/priority.ts @@ -14,6 +14,10 @@ import { data } from './priority.json'; * * randPriority({ length: 10 }) * + * @example + * + * randPriority({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randPriority( options?: Options diff --git a/packages/falso/src/lib/product-adjective.ts b/packages/falso/src/lib/product-adjective.ts index d457afc31..0525e46ea 100644 --- a/packages/falso/src/lib/product-adjective.ts +++ b/packages/falso/src/lib/product-adjective.ts @@ -14,6 +14,10 @@ import { data } from './product-adjective.json'; * * randProductAdjective({ length: 10 }) * + * @example + * + * randProductAdjective({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randProductAdjective( options?: Options diff --git a/packages/falso/src/lib/product-category.ts b/packages/falso/src/lib/product-category.ts index 15c2fa446..2b0fcbf69 100644 --- a/packages/falso/src/lib/product-category.ts +++ b/packages/falso/src/lib/product-category.ts @@ -14,6 +14,10 @@ import { data } from './product-category.json'; * * randProductCategory({ length: 10 }) * + * @example + * + * randProductCategory({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randProductCategory( options?: Options diff --git a/packages/falso/src/lib/product-description.ts b/packages/falso/src/lib/product-description.ts index 8e9b8dc50..8c524847e 100644 --- a/packages/falso/src/lib/product-description.ts +++ b/packages/falso/src/lib/product-description.ts @@ -14,6 +14,10 @@ import { data } from './product-description.json'; * * randProductDescription({ length: 10 }) * + * @example + * + * randProductDescription({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randProductDescription( options?: Options diff --git a/packages/falso/src/lib/product-material.ts b/packages/falso/src/lib/product-material.ts index 2faca0141..8d7c67a4c 100644 --- a/packages/falso/src/lib/product-material.ts +++ b/packages/falso/src/lib/product-material.ts @@ -14,6 +14,10 @@ import { data } from './product-material.json'; * * randProductMaterial({ length: 10 }) * + * @example + * + * randProductMaterial({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randProductMaterial( options?: Options diff --git a/packages/falso/src/lib/product-name.ts b/packages/falso/src/lib/product-name.ts index 265f51430..ed49be757 100644 --- a/packages/falso/src/lib/product-name.ts +++ b/packages/falso/src/lib/product-name.ts @@ -14,6 +14,10 @@ import { data } from './product-name.json'; * * randProductName({ length: 10 }) * + * @example + * + * randProductName({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randProductName( options?: Options diff --git a/packages/falso/src/lib/product.ts b/packages/falso/src/lib/product.ts index 4f393805b..753b5a026 100644 --- a/packages/falso/src/lib/product.ts +++ b/packages/falso/src/lib/product.ts @@ -4,6 +4,7 @@ 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; @@ -31,23 +32,26 @@ export interface Product { * * randProduct({ length: 10 }) * + * @example + * + * randProduct({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randProduct( options?: Options ) { - return fake( - () => ({ - id: randUuid(), - title: randProductName(), - description: randProductDescription(), - price: getRandomInRange({ fraction: 2 }).toString(), - category: randProductCategory(), - image: randImg(), - rating: { - rate: getRandomInRange({ min: 0.1, max: 5.0, fraction: 1 }).toString(), - count: getRandomInRange({ min: 0, max: 10000 }).toString(), - }, - }), - options - ); + const factory = () => ({ + id: randUuid(), + title: randProductName(), + description: randProductDescription(), + price: getRandomInRange({ fraction: 2 }).toString(), + category: randProductCategory(), + image: randImg(), + rating: { + rate: getRandomInRange({ min: 0.1, max: 5.0, fraction: 1 }).toString(), + count: getRandomInRange({ min: 0, max: 10000 }).toString(), + }, + }); + + return fake(factory, options, { uniqueComparer: objectWithIdIsUnique }); } diff --git a/packages/falso/src/lib/programming-language.ts b/packages/falso/src/lib/programming-language.ts index 051d86f96..7bc7cc280 100644 --- a/packages/falso/src/lib/programming-language.ts +++ b/packages/falso/src/lib/programming-language.ts @@ -14,6 +14,10 @@ import { data } from './programming-language.json'; * * randProgrammingLanguage({ length: 10 }) * + * @example + * + * randProgrammingLanguage({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randProgrammingLanguage( options?: Options diff --git a/packages/falso/src/lib/pronoun.ts b/packages/falso/src/lib/pronoun.ts index 5c46a59bc..83d6b3361 100644 --- a/packages/falso/src/lib/pronoun.ts +++ b/packages/falso/src/lib/pronoun.ts @@ -10,6 +10,14 @@ import { data } from './pronoun.json'; * * randPronoun() * + * @example + * + * randPronoun({ length: 10 }) + * + * @example + * + * randPronoun({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randPronoun( options?: Options diff --git a/packages/falso/src/lib/protocol.ts b/packages/falso/src/lib/protocol.ts index 2a91d31b9..5d0bfb068 100644 --- a/packages/falso/src/lib/protocol.ts +++ b/packages/falso/src/lib/protocol.ts @@ -17,11 +17,15 @@ interface ProtocolOptions extends FakeOptions { * * @example * + * randProtocol({ fullName: true }) + * + * @example + * * randProtocol({ length: 10 }) * * @example * - * randProtocol({ fullName: true }) + * randProtocol({ length: 10, priority: 'unique' }) // default priority is 'length' * */ export function randProtocol( diff --git a/packages/falso/src/lib/quote.ts b/packages/falso/src/lib/quote.ts index c12fc5846..ac9a09e9d 100644 --- a/packages/falso/src/lib/quote.ts +++ b/packages/falso/src/lib/quote.ts @@ -14,6 +14,10 @@ import { data } from './quote.json'; * * randQuote({ length: 10 }) * + * @example + * + * randQuote({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randQuote( options?: Options diff --git a/packages/falso/src/lib/rabbit.ts b/packages/falso/src/lib/rabbit.ts index fb6a83502..b9ed0832a 100644 --- a/packages/falso/src/lib/rabbit.ts +++ b/packages/falso/src/lib/rabbit.ts @@ -14,6 +14,10 @@ import { data } from './rabbit.json'; * * randRabbit({ length: 10 }) * + * @example + * + * randRabbit({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randRabbit( options?: Options diff --git a/packages/falso/src/lib/rand.ts b/packages/falso/src/lib/rand.ts index 4ae0747e4..6615d63ce 100644 --- a/packages/falso/src/lib/rand.ts +++ b/packages/falso/src/lib/rand.ts @@ -13,6 +13,10 @@ import { fake, FakeOptions } from './core/core'; * * rand([ 1, 2, 3 ], { length: 10 }) * + * @example + * + * rand({ [ 1, 2, 3 ], length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function rand( arr: Readonly, diff --git a/packages/falso/src/lib/recent-date.ts b/packages/falso/src/lib/recent-date.ts index dda73db0a..f0217eed8 100644 --- a/packages/falso/src/lib/recent-date.ts +++ b/packages/falso/src/lib/recent-date.ts @@ -1,5 +1,6 @@ import { randBetweenDate } from './between-date'; import { fake, FakeOptions } from './core/core'; +import { dateIsUnique } from './core/unique-validators'; interface RecentOptions extends FakeOptions { days?: number; @@ -22,6 +23,10 @@ interface RecentOptions extends FakeOptions { * * randRecentDate({ length: 10 }) * + * @example + * + * randRecentDate({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randRecentDate( options?: Options @@ -36,5 +41,7 @@ export function randRecentDate( const to = new Date(); const from = new Date(to.getTime() - daysInMilliseconds); - return fake(() => randBetweenDate({ from, to }), options); + return fake(() => randBetweenDate({ from, to }), options, { + uniqueComparer: dateIsUnique, + }); } diff --git a/packages/falso/src/lib/rgb.ts b/packages/falso/src/lib/rgb.ts index 3e4f8055c..fdc15e986 100644 --- a/packages/falso/src/lib/rgb.ts +++ b/packages/falso/src/lib/rgb.ts @@ -22,6 +22,10 @@ export interface RGBOptions extends FakeOptions { * * randRgb({ length: 10 }) * + * @example + * + * randRgb({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randRgb(options?: Options) { const factory = () => { diff --git a/packages/falso/src/lib/role.ts b/packages/falso/src/lib/role.ts index 54e9737ae..2658c4b6a 100644 --- a/packages/falso/src/lib/role.ts +++ b/packages/falso/src/lib/role.ts @@ -14,6 +14,10 @@ import { data } from './role.json'; * * randRole({ length: 10 }) * + * @example + * + * randRole({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randRole( options?: Options diff --git a/packages/falso/src/lib/routing-number.ts b/packages/falso/src/lib/routing-number.ts index 59be1c061..2da0b7951 100644 --- a/packages/falso/src/lib/routing-number.ts +++ b/packages/falso/src/lib/routing-number.ts @@ -14,6 +14,10 @@ import { randNumber } from './number'; * * randRoutingNumber({ length: 10 }) * + * @example + * + * randRoutingNumber({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randRoutingNumber( options?: Options diff --git a/packages/falso/src/lib/seat-number.ts b/packages/falso/src/lib/seat-number.ts index cc702c5a1..794dacbfc 100644 --- a/packages/falso/src/lib/seat-number.ts +++ b/packages/falso/src/lib/seat-number.ts @@ -14,6 +14,10 @@ import { rand } from './rand'; * * randSeatNumber({ length: 10 }) * + * @example + * + * randSeatNumber({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randSeatNumber( options?: Options diff --git a/packages/falso/src/lib/semver.ts b/packages/falso/src/lib/semver.ts index 02bf8bfaf..8f5a8512d 100644 --- a/packages/falso/src/lib/semver.ts +++ b/packages/falso/src/lib/semver.ts @@ -16,11 +16,15 @@ export interface SemverOptions extends FakeOptions { * * @example * + * randSemver({ prefix: 'v' }) + * + * @example + * * randSemver({ length: 10 }) * * @example * - * randSemver({ prefix: 'v' }) + * randSemver({ length: 10, priority: 'unique' }) // default priority is 'length' * */ export function randSemver( diff --git a/packages/falso/src/lib/sentence.ts b/packages/falso/src/lib/sentence.ts index 98368010b..b9c1637ec 100644 --- a/packages/falso/src/lib/sentence.ts +++ b/packages/falso/src/lib/sentence.ts @@ -30,6 +30,10 @@ function getSpecialCharacter(wordCount: number): string { * * randSentence({ length: 10 }) * + * @example + * + * randSentence({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randSentence( options?: Options diff --git a/packages/falso/src/lib/sequence.ts b/packages/falso/src/lib/sequence.ts index 77f51b4bf..039267428 100644 --- a/packages/falso/src/lib/sequence.ts +++ b/packages/falso/src/lib/sequence.ts @@ -51,6 +51,10 @@ type RandomSequenceOptions2 = { * * randSequence({ length: 10 }) * + * @example + * + * randSequence({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randSequence( options?: RandomSequenceOptions diff --git a/packages/falso/src/lib/shape.ts b/packages/falso/src/lib/shape.ts index b46cc0ae0..6ada3be45 100644 --- a/packages/falso/src/lib/shape.ts +++ b/packages/falso/src/lib/shape.ts @@ -14,6 +14,10 @@ import { data } from './shape.json'; * * randShape({ length: 10 }) * + * @example + * + * randShape({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randShape( options?: Options diff --git a/packages/falso/src/lib/singer.ts b/packages/falso/src/lib/singer.ts index 5b3db23ee..6e842320c 100644 --- a/packages/falso/src/lib/singer.ts +++ b/packages/falso/src/lib/singer.ts @@ -13,6 +13,10 @@ import { data } from './singer.json'; * * randSinger({ length: 10 }) * + * @example + * + * randSinger({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randSinger( options?: Options diff --git a/packages/falso/src/lib/skill.ts b/packages/falso/src/lib/skill.ts index 196ca01da..79c4b17ee 100644 --- a/packages/falso/src/lib/skill.ts +++ b/packages/falso/src/lib/skill.ts @@ -14,6 +14,10 @@ import { data } from './skill.json'; * * randSkill({ length: 10 }) * + * @example + * + * randSkill({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randSkill( options?: Options diff --git a/packages/falso/src/lib/slug.ts b/packages/falso/src/lib/slug.ts index 551081731..bb5d2cbce 100644 --- a/packages/falso/src/lib/slug.ts +++ b/packages/falso/src/lib/slug.ts @@ -14,6 +14,10 @@ import { randWord } from './word'; * * randSlug({ length: 10 }) * + * @example + * + * randSlug({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randSlug( options?: Options diff --git a/packages/falso/src/lib/snake.ts b/packages/falso/src/lib/snake.ts index d00bcc2e7..af0760453 100644 --- a/packages/falso/src/lib/snake.ts +++ b/packages/falso/src/lib/snake.ts @@ -14,6 +14,10 @@ import { data } from './snake.json'; * * randSnake({ length: 10 }) * + * @example + * + * randSnake({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randSnake( options?: Options diff --git a/packages/falso/src/lib/social.ts b/packages/falso/src/lib/social.ts index 32095b8bf..dd56bff92 100644 --- a/packages/falso/src/lib/social.ts +++ b/packages/falso/src/lib/social.ts @@ -14,6 +14,10 @@ import { data } from './social.json'; * * randSocial({ length: 2 }) * + * @example + * + * randSocial({ length: 10, priority: 'unique' }) // default priority is 'length' + * * */ diff --git a/packages/falso/src/lib/song.ts b/packages/falso/src/lib/song.ts index 2df7cef98..abc23052e 100644 --- a/packages/falso/src/lib/song.ts +++ b/packages/falso/src/lib/song.ts @@ -13,6 +13,10 @@ import { data } from './song.json'; * * randSong({ length: 10 }) * + * @example + * + * randSong({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randSong( options?: Options diff --git a/packages/falso/src/lib/soon-date.ts b/packages/falso/src/lib/soon-date.ts index fd53303f9..a53bb7ffc 100644 --- a/packages/falso/src/lib/soon-date.ts +++ b/packages/falso/src/lib/soon-date.ts @@ -1,5 +1,6 @@ import { randBetweenDate } from './between-date'; import { fake, FakeOptions } from './core/core'; +import { dateIsUnique } from './core/unique-validators'; interface SoonOptions extends FakeOptions { days?: number; @@ -22,6 +23,10 @@ interface SoonOptions extends FakeOptions { * * randSoonDate({ length: 10 }) * + * @example + * + * randSoonDate({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randSoonDate( options?: Options @@ -35,5 +40,7 @@ export function randSoonDate( 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, { + uniqueComparer: dateIsUnique, + }); } diff --git a/packages/falso/src/lib/sports-team.ts b/packages/falso/src/lib/sports-team.ts index 1b7195dd6..316c069fe 100644 --- a/packages/falso/src/lib/sports-team.ts +++ b/packages/falso/src/lib/sports-team.ts @@ -18,6 +18,10 @@ import { randIceHockeyTeam } from './ice-hockey-team'; * * randSportsTeam({ length: 10 }) * + * @example + * + * randSportsTeam({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randSportsTeam( options?: Options diff --git a/packages/falso/src/lib/sports.ts b/packages/falso/src/lib/sports.ts index 41581d905..e26a5b77e 100644 --- a/packages/falso/src/lib/sports.ts +++ b/packages/falso/src/lib/sports.ts @@ -25,6 +25,10 @@ const categoriesCount = Object.keys(data)?.length; * * randSports({ length: 10 }) * + * @example + * + * randSports({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randSports( options?: Options @@ -33,7 +37,7 @@ export function randSports( const category: string | undefined = options?.category; if (!categoriesCount) { - throw 'No Sport Categories found'; + throw Error('No Sport Categories found'); } if (category && !sportsData[category]) { diff --git a/packages/falso/src/lib/state-abbr.ts b/packages/falso/src/lib/state-abbr.ts index 79b50ce74..019859188 100644 --- a/packages/falso/src/lib/state-abbr.ts +++ b/packages/falso/src/lib/state-abbr.ts @@ -14,6 +14,10 @@ import { data } from './state-abbr.json'; * * randStateAbbr({ length: 10 }) * + * @example + * + * randStateAbbr({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randStateAbbr( options?: Options diff --git a/packages/falso/src/lib/state.ts b/packages/falso/src/lib/state.ts index a319e8343..f9b43800f 100644 --- a/packages/falso/src/lib/state.ts +++ b/packages/falso/src/lib/state.ts @@ -14,6 +14,10 @@ import { data } from './state.json'; * * randState({ length: 10 }) * + * @example + * + * randState({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randState( options?: Options diff --git a/packages/falso/src/lib/status.ts b/packages/falso/src/lib/status.ts index dcf24f615..801f86412 100644 --- a/packages/falso/src/lib/status.ts +++ b/packages/falso/src/lib/status.ts @@ -20,6 +20,10 @@ type Type = 'Project' | 'User Story' | 'Task'; * * randStatus({ length: 10 }) * + * @example + * + * randStatus({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randStatus< diff --git a/packages/falso/src/lib/street-address.ts b/packages/falso/src/lib/street-address.ts index 6cec0eaa4..d0556d8af 100644 --- a/packages/falso/src/lib/street-address.ts +++ b/packages/falso/src/lib/street-address.ts @@ -15,6 +15,10 @@ import { randStreetName } from './street-name'; * * randStreetAddress({ length: 10 }) * + * @example + * + * randStreetAddress({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randStreetAddress( options?: Options diff --git a/packages/falso/src/lib/street-name.ts b/packages/falso/src/lib/street-name.ts index df93c3f12..c8c556430 100644 --- a/packages/falso/src/lib/street-name.ts +++ b/packages/falso/src/lib/street-name.ts @@ -14,6 +14,10 @@ import { data } from './street-name.json'; * * randStreetName({ length: 10 }) * + * @example + * + * randStreetName({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randStreetName( options?: Options diff --git a/packages/falso/src/lib/subscription-plan.ts b/packages/falso/src/lib/subscription-plan.ts index 207b3b697..c8f6c72f4 100644 --- a/packages/falso/src/lib/subscription-plan.ts +++ b/packages/falso/src/lib/subscription-plan.ts @@ -14,6 +14,10 @@ import { data } from './subscription-plan.json'; * * randSubscriptionPlan({ length: 10 }) * + * @example + * + * randSubscriptionPlan({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randSubscriptionPlan( options?: Options diff --git a/packages/falso/src/lib/superhero-name.ts b/packages/falso/src/lib/superhero-name.ts index 54271534c..df9ba4159 100644 --- a/packages/falso/src/lib/superhero-name.ts +++ b/packages/falso/src/lib/superhero-name.ts @@ -19,6 +19,10 @@ import { data } from './superhero.json'; * * randSuperheroName({ length: 10 }) * + * @example + * + * randSuperheroName({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randSuperheroName( options?: Options diff --git a/packages/falso/src/lib/superhero.ts b/packages/falso/src/lib/superhero.ts index fa029cf90..afe6c6c15 100644 --- a/packages/falso/src/lib/superhero.ts +++ b/packages/falso/src/lib/superhero.ts @@ -1,6 +1,7 @@ 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'; @@ -35,6 +36,10 @@ export interface SuperheroEntity extends Superhero { * * randSuperhero({ length: 10 }) * + * @example + * + * randSuperhero({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randSuperhero( options?: Options @@ -50,5 +55,5 @@ export function randSuperhero( }; }; - return fake(factory, options); + return fake(factory, options, { uniqueComparer: objectWithIdIsUnique }); } diff --git a/packages/falso/src/lib/svg.ts b/packages/falso/src/lib/svg.ts index 523ee5a43..2ea68b0a1 100644 --- a/packages/falso/src/lib/svg.ts +++ b/packages/falso/src/lib/svg.ts @@ -14,6 +14,10 @@ import { data } from './svg.json'; * * randSvg({ length: 10 }) * + * @example + * + * randSvg({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randSvg( options?: Options diff --git a/packages/falso/src/lib/text-range.ts b/packages/falso/src/lib/text-range.ts index 8ac4b70bc..140a90469 100644 --- a/packages/falso/src/lib/text-range.ts +++ b/packages/falso/src/lib/text-range.ts @@ -19,6 +19,10 @@ export interface TextRangeOptions extends FakeOptions { * * randTextRange({ min: 10, max: 100, length: 10 }) * + * @example + * + * randTextRange({ min: 10, max: 100, length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randTextRange( options: Options diff --git a/packages/falso/src/lib/text.ts b/packages/falso/src/lib/text.ts index 4dc2691e8..b89cf628d 100644 --- a/packages/falso/src/lib/text.ts +++ b/packages/falso/src/lib/text.ts @@ -17,11 +17,15 @@ export interface TextOptions extends FakeOptions { * * @example * + * randText({ charCount: 10 }) // default is 10 + * + * @example + * * randText({ length: 10 }) * * @example * - * randText({ charCount: 10 }) // default is 10 + * randText({ length: 10, priority: 'unique' }) // default priority is 'length' * */ export function randText( @@ -30,7 +34,7 @@ export function randText( const charCount: number = options?.charCount ?? 10; if (charCount < 1 || isNaN(charCount)) { - throw 'Character count must be greater than 0'; + throw new Error('Character count must be greater than 0'); } const factory = () => { diff --git a/packages/falso/src/lib/time-zone.ts b/packages/falso/src/lib/time-zone.ts index 66922a489..c78d172e3 100644 --- a/packages/falso/src/lib/time-zone.ts +++ b/packages/falso/src/lib/time-zone.ts @@ -14,6 +14,10 @@ import { data } from './time-zone.json'; * * randTimeZone({ length: 10 }) * + * @example + * + * randTimeZone({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randTimeZone( options?: Options diff --git a/packages/falso/src/lib/todo.ts b/packages/falso/src/lib/todo.ts index 4cf0c51fc..65bcd5df2 100644 --- a/packages/falso/src/lib/todo.ts +++ b/packages/falso/src/lib/todo.ts @@ -2,6 +2,7 @@ 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; @@ -22,15 +23,19 @@ export interface Todo { * * randTodo({ length: 10 }) * + * @example + * + * randTodo({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randTodo( 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, { uniqueComparer: objectWithIdIsUnique }); } diff --git a/packages/falso/src/lib/transaction-type.ts b/packages/falso/src/lib/transaction-type.ts index 3d430d94c..01cfa11ce 100644 --- a/packages/falso/src/lib/transaction-type.ts +++ b/packages/falso/src/lib/transaction-type.ts @@ -14,6 +14,10 @@ import { data } from './transaction-type.json'; * * randTransactionType({ length: 10 }) * + * @example + * + * randTransactionType({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randTransactionType( options?: Options diff --git a/packages/falso/src/lib/url.ts b/packages/falso/src/lib/url.ts index f122cdcf8..e314109fc 100644 --- a/packages/falso/src/lib/url.ts +++ b/packages/falso/src/lib/url.ts @@ -15,6 +15,10 @@ import { randWord } from './word'; * * randUrl({ length: 10 }) * + * @example + * + * randUrl({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randUrl( options?: Options diff --git a/packages/falso/src/lib/user-agent.ts b/packages/falso/src/lib/user-agent.ts index 8cfb40c3b..988208217 100644 --- a/packages/falso/src/lib/user-agent.ts +++ b/packages/falso/src/lib/user-agent.ts @@ -14,6 +14,10 @@ import { data } from './user-agent.json'; * * randUserAgent({ length: 10 }) * + * @example + * + * randUserAgent({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randUserAgent( options?: Options diff --git a/packages/falso/src/lib/user-name.ts b/packages/falso/src/lib/user-name.ts index e005a45fb..c881ef7d0 100644 --- a/packages/falso/src/lib/user-name.ts +++ b/packages/falso/src/lib/user-name.ts @@ -20,21 +20,25 @@ export interface UserNameOptions extends FakeOptions { * * @example * - * randUserName({ length: 10 }) + * randUserName({ firstName: 'Ryan' }) * * @example * - * randUserName({ firstName: 'Ryan' }) + * randUserName({ lastName: 'Smee' }) * * @example * - * randUserName({ lastName: 'Smee' }) + * randUserName({ length: 10 }) + * + * @example + * + * randUserName({ length: 10, priority: 'unique' }) // default priority is 'length' * */ export function randUserName( options?: Options ) { - return fake(() => { + const factory: () => string = () => { const firstName = options?.firstName ?? randFirstName(); const lastName = options?.lastName ?? randLastName(); let userName = `${firstName} ${lastName}`.replace(' ', fake(['.', '_'])); @@ -44,5 +48,7 @@ export function randUserName( } return userName; - }, options); + }; + + return fake(factory, options); } diff --git a/packages/falso/src/lib/user.ts b/packages/falso/src/lib/user.ts index ca300b948..2bed038b7 100644 --- a/packages/falso/src/lib/user.ts +++ b/packages/falso/src/lib/user.ts @@ -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; @@ -36,11 +37,15 @@ export interface User { * * randUser({ length: 10 }) * + * @example + * + * randUser({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randUser( options?: Options ) { - return fake(() => { + const factory = () => { const firstName = randFirstName({ withAccents: false }); const lastName = randLastName({ withAccents: false }); @@ -56,5 +61,7 @@ export function randUser( }; return user; - }, options); + }; + + return fake(factory, options, { uniqueComparer: objectWithIdIsUnique }); } diff --git a/packages/falso/src/lib/uuid.ts b/packages/falso/src/lib/uuid.ts index 5af6110dd..b17470b5c 100644 --- a/packages/falso/src/lib/uuid.ts +++ b/packages/falso/src/lib/uuid.ts @@ -15,6 +15,10 @@ import { randNumber } from './number'; * * randUuid({ length: 10 }) * + * @example + * + * randUuid({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randUuid( options?: Options diff --git a/packages/falso/src/lib/vehicle-fuel.ts b/packages/falso/src/lib/vehicle-fuel.ts index e6f06a30a..738f44fdf 100644 --- a/packages/falso/src/lib/vehicle-fuel.ts +++ b/packages/falso/src/lib/vehicle-fuel.ts @@ -14,6 +14,10 @@ import { data } from './vehicle-fuel.json'; * * randVehicleFuel({ length: 10 }) * + * @example + * + * randVehicleFuel({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randVehicleFuel( options?: Options diff --git a/packages/falso/src/lib/vehicle-manufacturer.ts b/packages/falso/src/lib/vehicle-manufacturer.ts index 0b5c12568..404f0421a 100644 --- a/packages/falso/src/lib/vehicle-manufacturer.ts +++ b/packages/falso/src/lib/vehicle-manufacturer.ts @@ -14,6 +14,10 @@ import { data } from './vehicle-manufacturer.json'; * * randVehicleManufacturer({ length: 10 }) * + * @example + * + * randVehicleManufacturer({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randVehicleManufacturer( options?: Options diff --git a/packages/falso/src/lib/vehicle-model.ts b/packages/falso/src/lib/vehicle-model.ts index cfb4540e6..93a43436f 100644 --- a/packages/falso/src/lib/vehicle-model.ts +++ b/packages/falso/src/lib/vehicle-model.ts @@ -14,6 +14,10 @@ import { data } from './vehicle-model.json'; * * randVehicleModel({ length: 10 }) * + * @example + * + * randVehicleModel({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randVehicleModel( options?: Options diff --git a/packages/falso/src/lib/vehicle-type.ts b/packages/falso/src/lib/vehicle-type.ts index 7252c71cb..0722463a6 100644 --- a/packages/falso/src/lib/vehicle-type.ts +++ b/packages/falso/src/lib/vehicle-type.ts @@ -14,6 +14,10 @@ import { data } from './vehicle-type.json'; * * randVehicleType({ length: 10 }) * + * @example + * + * randVehicleType({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randVehicleType( options?: Options diff --git a/packages/falso/src/lib/vehicle.ts b/packages/falso/src/lib/vehicle.ts index 82aa28a3b..9bb53de58 100644 --- a/packages/falso/src/lib/vehicle.ts +++ b/packages/falso/src/lib/vehicle.ts @@ -14,6 +14,10 @@ import { data } from './vehicle.json'; * * randVehicle({ length: 10 }) * + * @example + * + * randVehicle({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randVehicle( options?: Options diff --git a/packages/falso/src/lib/verb.ts b/packages/falso/src/lib/verb.ts index 830f9d010..cb9dc4d90 100644 --- a/packages/falso/src/lib/verb.ts +++ b/packages/falso/src/lib/verb.ts @@ -14,6 +14,10 @@ import { data } from './verb.json'; * * randVerb({ length: 10 }) * + * @example + * + * randVerb({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randVerb( options?: Options diff --git a/packages/falso/src/lib/weekday.ts b/packages/falso/src/lib/weekday.ts index c81273b1e..63a9a1367 100644 --- a/packages/falso/src/lib/weekday.ts +++ b/packages/falso/src/lib/weekday.ts @@ -14,6 +14,10 @@ import { data } from './weekday.json'; * * randWeekday({ length: 10 }) * + * @example + * + * randWeekday({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randWeekday( options?: Options diff --git a/packages/falso/src/lib/word.ts b/packages/falso/src/lib/word.ts index 3406c2d90..39d726aed 100644 --- a/packages/falso/src/lib/word.ts +++ b/packages/falso/src/lib/word.ts @@ -26,6 +26,10 @@ function capitalizeFirstLetter(text: string): string { * * randWord({ length: 10 }) * + * @example + * + * randWord({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randWord( options?: Options diff --git a/packages/falso/src/lib/zip-code.ts b/packages/falso/src/lib/zip-code.ts index 5432bbf49..bbb8730a2 100644 --- a/packages/falso/src/lib/zip-code.ts +++ b/packages/falso/src/lib/zip-code.ts @@ -15,11 +15,15 @@ import { randBoolean } from './boolean'; * * randZipCode({ length: 10 }) * + * @example + * + * randZipCode({ length: 10, priority: 'unique' }) // default priority is 'length' + * */ export function randZipCode( options?: Options ) { - return fake(() => { + const factory: () => string = () => { let zipCode = '' + randNumber({ min: 10_000, max: 99_999 }); if (randBoolean()) { @@ -27,5 +31,7 @@ export function randZipCode( } return zipCode; - }, options); + }; + + return fake(factory, options); } diff --git a/packages/falso/src/tests/core/date-is-unique.spec.ts b/packages/falso/src/tests/core/date-is-unique.spec.ts new file mode 100644 index 000000000..f4a6f021d --- /dev/null +++ b/packages/falso/src/tests/core/date-is-unique.spec.ts @@ -0,0 +1,28 @@ +import { dateIsUnique } from '../../lib/core/unique-validators'; +import { randPastDate } from '@ngneat/falso'; + +describe('dateIsUnique', () => { + let dates: Date[]; + let date1: Date; + let date2: Date; + let date3: Date; + + beforeEach(() => { + date1 = randPastDate(); + date2 = randPastDate(); + date3 = randPastDate(); + dates = [date1, date2]; + }); + + it('should pass when date is unique', () => { + const result = dateIsUnique(date3, dates); + + expect(result).toEqual(true); + }); + + it('should not pass when date is not unique', () => { + const result = dateIsUnique(date2, dates); + + expect(result).toEqual(false); + }); +}); diff --git a/packages/falso/src/tests/core/object-is-unique.spec.ts b/packages/falso/src/tests/core/object-is-unique.spec.ts new file mode 100644 index 000000000..876fa37d4 --- /dev/null +++ b/packages/falso/src/tests/core/object-is-unique.spec.ts @@ -0,0 +1,76 @@ +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('1 key is passed', () => { + let array: User[]; + let newItem: User; + let keys: (keyof User)[]; + + beforeEach(() => { + array = randUser({ length: 3 }); + newItem = randUser(); + keys = ['id']; + }); + + it('should fail when passed item matches and array item have 1 matching key value', () => { + const sharedId = randUuid(); + newItem.id = sharedId; + array[1].id = sharedId; + + const result = objectIsUnique(newItem, array, keys); + + expect(result).toEqual(false); + }); + + it('should pass when passed item matches and array item have 0 matching key values', () => { + array[0].id = randUuid() + '0'; + array[1].id = randUuid() + '1'; + array[2].id = randUuid() + '2'; + newItem.id = randUuid() + '3'; + + const result = objectIsUnique(newItem, array, keys); + + expect(result).toEqual(true); + }); + }); + + describe('multiple keys are passed', () => { + let array: User[]; + let newItem: User; + let keys: (keyof User)[]; + + beforeEach(() => { + array = randUser({ length: 3 }); + newItem = randUser(); + keys = ['id', 'firstName']; + }); + + it('should fail when passed item matches and array item have 1 matching key value', () => { + const sharedFirstName = randFirstName(); + newItem.firstName = sharedFirstName; + array[1].firstName = sharedFirstName; + + const result = objectIsUnique(newItem, array, keys); + + expect(result).toEqual(false); + }); + + it('should pass when passed item matches and array item have 0 matching key values', () => { + array[0].id = randUuid() + '0'; + array[0].firstName = randFirstName() + '0'; + array[1].id = randUuid() + '1'; + array[1].id = randFirstName() + '1'; + array[2].id = randUuid() + '2'; + array[2].id = randFirstName() + '2'; + newItem.id = randUuid() + '3'; + newItem.id = randFirstName() + '3'; + + const result = objectIsUnique(newItem, array, keys); + + expect(result).toEqual(true); + }); + }); +}); diff --git a/packages/falso/src/tests/core/object-with-id-is-unique.spec.ts b/packages/falso/src/tests/core/object-with-id-is-unique.spec.ts new file mode 100644 index 000000000..3b89326f4 --- /dev/null +++ b/packages/falso/src/tests/core/object-with-id-is-unique.spec.ts @@ -0,0 +1,42 @@ +import { objectWithIdIsUnique } from '../../lib/core/unique-validators'; +import { randFullName, randNumber } from '@ngneat/falso'; + +describe('objectWithIdIsUnique', () => { + let objects: any[]; + let object1: any; + let object2: any; + let object3: any; + + beforeEach(() => { + const name = randFullName(); + const age = randNumber({ min: 10, max: 90 }); + object1 = { + id: '1', + name, + age, + }; + object2 = { + id: '2', + name, + age, + }; + object3 = { + id: '3', + name, + age, + }; + objects = [object1, object2]; + }); + + it('should pass when object ids are unique', () => { + const result = objectWithIdIsUnique(object3, objects); + + expect(result).toEqual(true); + }); + + it('should fail when object ids are not unique', () => { + const result = objectWithIdIsUnique(object2, objects); + + expect(result).toEqual(false); + }); +}); diff --git a/packages/falso/src/tests/core/primitive-value-is-unique.spec.ts b/packages/falso/src/tests/core/primitive-value-is-unique.spec.ts new file mode 100644 index 000000000..40361051c --- /dev/null +++ b/packages/falso/src/tests/core/primitive-value-is-unique.spec.ts @@ -0,0 +1,28 @@ +import { primitiveValueIsUnique } from '../../lib/core/unique-validators'; +import { randNumber } from '@ngneat/falso'; + +describe('primitiveValueIsUnique', () => { + let items: number[]; + let item1: number; + let item2: number; + let item3: number; + + beforeEach(() => { + item1 = randNumber(); + item2 = randNumber(); + item3 = randNumber(); + items = [item1, item2]; + }); + + it('should pass when item is unique', () => { + const result = primitiveValueIsUnique(item3, items); + + expect(result).toEqual(true); + }); + + it('should fail when item is not unique', () => { + const result = primitiveValueIsUnique(item2, items); + + expect(result).toEqual(false); + }); +}); diff --git a/packages/falso/src/tests/float.spec.ts b/packages/falso/src/tests/float.spec.ts index 682ac7b46..af51d02ab 100644 --- a/packages/falso/src/tests/float.spec.ts +++ b/packages/falso/src/tests/float.spec.ts @@ -35,8 +35,9 @@ describe('float', () => { }); it('should support fraction', () => { + const floatRegex = /^\d+\.\d?\d$/; // A number with 1-2 decimal places const num = randFloat({ min: 100, max: 200, fraction: 2 }); expect(typeof num).toBe('number'); - expect(String(num)).toMatch(/^\d+\.\d?\d$/); + expect(String(num)).toMatch(floatRegex); }); }); diff --git a/packages/falso/src/tests/json-check-unique.spec.ts b/packages/falso/src/tests/json-check-unique.spec.ts new file mode 100644 index 000000000..4c21fdc4b --- /dev/null +++ b/packages/falso/src/tests/json-check-unique.spec.ts @@ -0,0 +1,40 @@ +import { checkUnique } from '../lib/json'; + +describe('randJSON checkUnique', () => { + let json1: object; + let json2: object; + let json3: object; + + beforeEach(() => { + json1 = { + cat: { + age: 1, + name: 'Ryan', + }, + }; + json2 = { + dog: { + age: 17, + name: 'Pete', + }, + }; + json3 = { + fish: { + age: 100, + name: 'Mary', + }, + }; + }); + + it('should return true if jsons does not exist in jsons array', () => { + const result = checkUnique(json1, [json2, json3]); + + expect(result).toEqual(true); + }); + + it('should return false if jsons exists in jsons array', () => { + const result = checkUnique(json1, [json1, json3]); + + expect(result).toEqual(false); + }); +}); diff --git a/packages/falso/src/tests/json.spec.ts b/packages/falso/src/tests/json.spec.ts index 5c2f6260b..dee5f3eba 100644 --- a/packages/falso/src/tests/json.spec.ts +++ b/packages/falso/src/tests/json.spec.ts @@ -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', () => { @@ -42,11 +42,13 @@ describe('randJSON', () => { }); }); - describe('length is 100', () => { - it('should return an array length of 100', () => { - const result = randJSON({ length: 100 }); + describe('length is 3', () => { + it('should return an array length of 3, each with a random json', () => { + const [json1, json2, json3] = randJSON({ length: 3 }); - expect(result?.length).toEqual(100); + expect(typeof json1).toEqual('object'); + expect(typeof json2).toEqual('object'); + expect(typeof json3).toEqual('object'); }); }); }); diff --git a/packages/falso/src/tests/nearby-gpscoordinates-check-unique.spec.ts b/packages/falso/src/tests/nearby-gpscoordinates-check-unique.spec.ts new file mode 100644 index 000000000..39612eb8d --- /dev/null +++ b/packages/falso/src/tests/nearby-gpscoordinates-check-unique.spec.ts @@ -0,0 +1,25 @@ +import { checkUnique } from '../lib/nearby-gpscoordinate'; + +describe('randNearbyGPSCoordinate checkUnique', () => { + let coordinate1: [number, number]; + let coordinate2: [number, number]; + let coordinate3: [number, number]; + + beforeEach(() => { + coordinate1 = [70.633, -80.249]; + coordinate2 = [-32.073, -102.123]; + coordinate3 = [41.478, -4.485]; + }); + + it('should return true if coordinate1 does not exist in coordinate1 array', () => { + const result = checkUnique(coordinate1, [coordinate2, coordinate3]); + + expect(result).toEqual(true); + }); + + it('should return false if coordinate1 exists in coordinate1 array', () => { + const result = checkUnique(coordinate1, [coordinate1, coordinate3]); + + expect(result).toEqual(false); + }); +}); diff --git a/packages/falso/src/tests/text-range.spec.ts b/packages/falso/src/tests/text-range.spec.ts index ed13ca95b..26c2575d3 100644 --- a/packages/falso/src/tests/text-range.spec.ts +++ b/packages/falso/src/tests/text-range.spec.ts @@ -14,7 +14,7 @@ describe('randTextRange', () => { it('should return string of length between min and max', () => { const result = randTextRange({ min, max }); - expect(result.length).toBeGreaterThan(min); + expect(result.length).toBeGreaterThanOrEqual(min); expect(result.length).toBeLessThanOrEqual(max); }); }); diff --git a/packages/falso/src/lib/user-name.spec.ts b/packages/falso/src/tests/user-name.spec.ts similarity index 100% rename from packages/falso/src/lib/user-name.spec.ts rename to packages/falso/src/tests/user-name.spec.ts