Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into feat/220-unique-values
Browse files Browse the repository at this point in the history
  • Loading branch information
theryansmee committed Mar 24, 2022
2 parents 77831a5 + 7343237 commit 96acb7e
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 53 deletions.
2 changes: 1 addition & 1 deletion packages/falso/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ export { randSinger } from './lib/singer';
export { randSong } from './lib/song';
export { randAddress, Address } from './lib/address';
export { randFullAddress } from './lib/full-address';
export { randJSON } from './lib/rand-json';
export { randJSON } from './lib/json';
export { randCodeSnippet } from './lib/code-snippet';
export { randTextRange } from './lib/text-range';
export { randFlightNumber } from './lib/flight-number';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
import { randBoolean } from './boolean';
import { FakeOptions } from './core/core';
import { fake, FakeOptions } from './core/core';
import { randMovie } from './movie';
import { randMovieCharacter } from './movie-character';
import { randNumber } from './number';
import { randSinger } from './singer';
import { randSnake } from './snake';
import { randUuid } from './uuid';
import { randWord } from './word';
import { randUser } from './user';
import { randAddress } from './address';
import { randProduct } from './product';
import { randFlightDetails } from './flight-details';
import { randCreditCard } from './credit-card';

export interface RandomJSONOptions extends FakeOptions {
min?: number;
max?: number;
totalKeys?: number;
minKeys?: number;
maxKeys?: number;
}

const generateRandomValue = (): any => {
Expand All @@ -28,23 +34,23 @@ const generateRandomValue = (): any => {
randWord({ length: randNumber({ min: 1, max: 10 }) }),
randSnake(),
randSnake({ length: randNumber({ min: 1, max: 10 }) }),
randUser(),
randUser({ length: randNumber({ min: 1, max: 10 }) }),
randAddress(),
randAddress({ length: randNumber({ min: 1, max: 10 }) }),
randProduct(),
randProduct({ length: randNumber({ min: 1, max: 10 }) }),
randFlightDetails(),
randFlightDetails({ length: randNumber({ min: 1, max: 10 }) }),
randCreditCard(),
randCreditCard({ length: randNumber({ min: 1, max: 10 }) }),
];

return availableValuesGenerators[
randNumber({ min: 0, max: availableValuesGenerators.length - 1 })
];
};

const generateRandomObject = (length: number) => {
let o: { [key: string]: any} = {};

for (let index = 0; index < length; index++) {
o[randUuid().replace(/-/g, '')] = generateRandomValue()
}

return o;
};

/**
* Generate a random JSON Object.
*
Expand All @@ -55,21 +61,32 @@ const generateRandomObject = (length: number) => {
*
* @example If a fixed number of keys are required
*
* randJSON({ length: 10 })
* randJSON({ totalKeys: 10 })
*
* @example If a random number of keys are required
*
* randJSON({ min: 1, max: 10 })
* randJSON({ minKeys: 1, maxKeys: 10 })
*
*/
export function randJSON<Options extends RandomJSONOptions = never>(
options?: Options
) {
let objectSize = randNumber({
min: options?.min || 1,
max: options?.max || 99,
});
const objectSize =
options?.totalKeys ??
randNumber({
min: options?.minKeys || 1,
max: options?.maxKeys || 99,
});

const factory = () => {
const generatedObject: { [key: string]: any } = {};

for (let index = 0; index < objectSize; index++) {
generatedObject[randUuid().replace(/-/g, '')] = generateRandomValue();
}

if (options?.length) objectSize = options.length;
return generatedObject;
};

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

describe('randJSON', () => {
describe('when it returns the expected values', () => {
it('returned value should be an object', () => {
const result = randJSON();
expect(typeof result).toEqual('object');
});

it('should return an object with the given number of keys', () => {
const totalKeys = randNumber({ min: 1, max: 30 });
const result = randJSON({ totalKeys });

expect(Object.keys(result).length).toBe(totalKeys);
});

it('should return an object with a random number of keys', () => {
const minKeys = randNumber({ min: 1, max: 30 });
const maxKeys = randNumber({ min: minKeys + 1, max: 30 });
const result = randJSON({ minKeys, maxKeys });

expect(Object.keys(result).length).toBeGreaterThanOrEqual(minKeys);
expect(Object.keys(result).length).toBeLessThanOrEqual(maxKeys);
});
});

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

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

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

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

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

expect(result?.length).toEqual(100);
});
});
});
});
31 changes: 0 additions & 31 deletions packages/falso/src/tests/rand-json.spec.ts

This file was deleted.

0 comments on commit 96acb7e

Please sign in to comment.