Skip to content

Commit

Permalink
Merge pull request #316 from va-stefanek/feature/text-length
Browse files Browse the repository at this point in the history
feat(size): allow to specify string length of random values
  • Loading branch information
shaharkazaz authored Aug 20, 2023
2 parents fe0ccab + aa72a6e commit 4989449
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 3 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ const user = { email: randEmail(), name: randFullName() };
const emails = randEmail({ length: 10 });
```

You can specify the length of elements you want to generate. Below is an example of generating 10 emails with length equal or smaller than 20 characters.

```ts
const emails = randEmail({ length: 10, maxCharCount: 20 });
```

### Setting a Randomness Seed

You can set your own seed if you want consistent results:
Expand Down
15 changes: 12 additions & 3 deletions packages/falso/src/lib/core/core.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { random } from '../random';
import { isString } from '../utils/validators.utils';

export interface FakeOptions {
length?: number;
locale?: any | string[];
maxCharCount?: number;
}

export type markRequired<Type, Key extends keyof Type> = Type & {
Expand All @@ -21,9 +23,16 @@ export function fake<T, Options extends FakeOptions>(
data: Readonly<T[]> | FactoryFunction<T>,
options?: Options
): Return<T, Options> {
const dataSource = Array.isArray(data)
? () => randElement(data)
: (data as FactoryFunction<T>);
let dataSource = data as FactoryFunction<T>;
if (Array.isArray(data)) {
let resolvedData = data;
if (options?.maxCharCount && isString(data[0])) {
resolvedData = data.filter(
(item) => item.length <= options.maxCharCount!
);
}
dataSource = () => randElement(resolvedData);
}

if (options?.length === undefined) {
return dataSource(0) as any;
Expand Down
3 changes: 3 additions & 0 deletions packages/falso/src/lib/utils/validators.utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function isString(value: any): value is string {
return typeof value === 'string';
}
25 changes: 25 additions & 0 deletions packages/falso/src/tests/max-char-count.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { randAccount } from '../lib/account';
import { randMovie } from '../lib/movie';

describe('maxCharCount', () => {
it('should return 20 random movies with name length equal or smaller than 8', () => {
const result = randMovie({ length: 20, maxCharCount: 8 });

expect(result).toHaveLength(20);
result.forEach((movie) => expect(movie.length).toBeLessThanOrEqual(8));
});

it('should return 50 random movies with name length equal or smaller than 1', () => {
const result = randMovie({ length: 50, maxCharCount: 1 });

expect(result).toHaveLength(50);
result.forEach((movie) => expect(movie.length).toBeLessThanOrEqual(1));
});

it('should return 20 random movies with name length equal or smaller than 20', () => {
const result = randAccount({ length: 20, maxCharCount: 20 });

expect(result).toHaveLength(20);
result.forEach((account) => expect(account.length).toBeLessThanOrEqual(20));
});
});
4 changes: 4 additions & 0 deletions packages/falso/src/tests/movie.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ describe('movie', () => {
it('should return a random movie (type string)', () => {
expect(typeof randMovie()).toEqual('string');
});

it('should return 10 random movies', () => {
expect(randMovie({ length: 10 })).toHaveLength(10);
});
});

0 comments on commit 4989449

Please sign in to comment.