Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(lorem): add character length parameter to lorem.text #2227

Closed
wants to merge 31 commits into from
Closed
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
8026316
add character length parameter to lorem.text
Jun 27, 2023
8d1ddd9
resolve issues noted in the review of ST-DDT and matthewmayer
Jun 28, 2023
30e73fb
fix example in comment of lorem.text
Jun 28, 2023
65f7d5d
return an empty string for an ill defined interval instead of throwin…
Jun 28, 2023
8f5ba98
fix typo
Jun 29, 2023
ac91a62
add quick primitive argument to lorem.text
Jun 29, 2023
425ad90
it throws an error if min is greater than max instead of returning an…
Jun 30, 2023
7cb71ed
the length parameter in lorem.text is a number or an object with min …
Jun 30, 2023
4fad2bf
corrected descriptions in lorem.spec.ts and lorem.text throws an erro…
Jun 30, 2023
a2f6bbd
Merge branch 'next' into feat/lorem/issue-1546/add_character_length_p…
Jul 16, 2023
fa121ee
remove explicit character information and repetitive examples from th…
Jul 19, 2023
33d0f37
Merge branch 'feat/lorem/issue-1546/add_character_length_parameter_to…
Jul 19, 2023
2abd70b
Merge branch 'next' into feat/lorem/issue-1546/add_character_length_p…
Jul 19, 2023
fa79dec
fix issue after merging and update snapshot for lorem module
Jul 19, 2023
956101b
Merge branch 'next' into feat/lorem/issue-1546/add_character_length_p…
Aug 8, 2023
bfa702f
Merge branch 'next' into feat/lorem/issue-1546/add_character_length_p…
Aug 13, 2023
9280569
feat(lorem): improve code
Aug 20, 2023
9fcacde
feat(lorem): remove .only and add lorem-snapshot
Aug 20, 2023
1bd5f3c
Merge branch 'next' into feat/lorem/issue-1546/add_character_length_p…
Aug 20, 2023
5cab96d
Merge branch 'next' into feat/lorem/issue-1546/add_character_length_p…
Aug 20, 2023
15a8f93
feat(lorem): improve jsdoc and simplify code of lorem.text()
Aug 20, 2023
402802d
feat(lorem): remove unnecessary checks in text()
Aug 20, 2023
153dcfa
feat(lorem): remove redundant tests
Aug 20, 2023
056886a
feat(lorem): remove tests for checks that were removed
Aug 20, 2023
1fbb6da
feat(lorem): simplify code
Aug 20, 2023
05fc62b
chore(lorem): simplify code and remove unnecessary tests
Sep 4, 2023
1774fc5
Merge branch 'next' into feat/lorem/issue-1546/add_character_length_p…
Sep 4, 2023
0046096
Merge branch 'next' into feat/lorem/issue-1546/add_character_length_p…
Sep 6, 2023
c3f19ad
feat(lorem): fix bug in text method
Sep 9, 2023
9cac43d
feat(lorem): improve text method
Sep 9, 2023
742e794
Merge branch 'next' into feat/lorem/issue-1546/add_character_length_p…
Sep 9, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ This is a shorthand for running the following scripts in order:

- `pnpm install` - installs npm packages defined in package.json
- `pnpm run generate:locales` - generates locale files
- `pnpm run generate:api-doc` - generates API documentation
- `pnpm run generate:api-docs` - generates API documentation
- `pnpm run format` - runs [prettify](https://github.com/prettier/prettier) to format code
- `pnpm run lint` - runs [ESLint](https://github.com/eslint/eslint) to enforce project code standards
- `pnpm run build:clean` - removes artifacts from previous builds
Expand Down
85 changes: 83 additions & 2 deletions src/modules/lorem/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Faker } from '../..';
import { FakerError } from '../../errors/faker-error';
import { filterWordListByLength } from '../word/filterWordListByLength';

/**
Expand Down Expand Up @@ -318,6 +319,11 @@ export class LoremModule {
/**
* Generates a random text based on a random lorem method.
*
* @param options The options for the text to generate or options.length as quick primitive argument.
* @param options.length The length (range) of text to generate.
* @param options.length.min The minimum length of text to generate.
* @param options.length.max The maximum length of text to generate.
*
* @example
* faker.lorem.text() // 'Doloribus autem non quis vero quia.'
* faker.lorem.text()
Expand All @@ -326,10 +332,35 @@ export class LoremModule {
* // Quis ut dolor dolores facilis possimus tempore voluptates.
* // Iure nam officia optio cumque.
* // Dolor tempora iusto.'
* faker.lorem.text(14) // 'Doloribus aut.' (14 characters)
* faker.lorem.text({length: 14}) // 'Doloribus aut.' (14 characters)
* faker.lorem.text({length: {min: 10, max: 15}}) // 'autem non quis.' (15 characters)
* faker.lorem.text({length: {min: 10, max: 12}}) // 'autem non.' (10 characters)
* faker.lorem.text({length: {min: 3, max: 10}}) // 'autem.' (6 characters)
*
* @since 3.1.0
*/
text(): string {
text(
options?:
| number
| {
/**
* The length (range) of text to generate.
*/
length?:
| number
| {
/**
* The minimum length of text to generate.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* The minimum length of text to generate.
* The minimum amount of characters to generate.

*/
min?: number;
/**
* The maximum length of text to generate.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* The maximum length of text to generate.
* The maximum amount of characters to generate.

*/
max?: number;
};
}
): string {
const methods: Array<keyof LoremModule> = [
'sentence',
'sentences',
Expand All @@ -340,7 +371,57 @@ export class LoremModule {

const method = this.faker.helpers.arrayElement(methods);

return `${this[method]()}`;
if (typeof options === 'undefined') {
return `${this[method]()}`;
}

if (typeof options === 'number') {
return this.faker.lorem.text({ length: options });
}

if (typeof options?.length === 'number') {
if (options.length >= 0) {
let text = `${this[method]()}`;
while (text.length <= options?.length) {
text = `${text} ${this[method]()}`;
}

return options.length
? `${text.substring(0, options.length - 1)}.`
: '';
}

throw new FakerError(
`Length ${options.length} should be a non-negative integer.`
);
}

if (options.length?.max != null && options.length?.max < 0) {
throw new FakerError(
`Max ${options.length.max} should be a non-negative integer.`
);
}

if (options.length?.min != null && options.length?.min < 0) {
throw new FakerError(
`Min ${options.length.min} should be a non-negative integer.`
);
}

if (options.length?.min > options.length?.max) {
throw new FakerError(
`Max ${options.length.max} should be greater than min ${options.length.min}.`
);
}

const min = options.length?.min ?? 0;
const max = options.length?.max ?? 2 * min;
const randomLength = this.faker.number.int({
min: min,
max: max,
});

return this.faker.lorem.text({ length: randomLength });
}

/**
Expand Down
20 changes: 6 additions & 14 deletions test/__snapshots__/lorem.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ exports[`lorem > 42 > slug > with length range 1`] = `"minus-a-qui-tempore-quo-p

exports[`lorem > 42 > text > noArgs 1`] = `"Qui tempore quo perspiciatis perspiciatis quia in quia architecto ipsa. Pariatur veniam unde fugit facilis mollitia. Ipsa reiciendis nam. Earum dolorem alias qui asperiores qui natus aliquam iste. Consequatur eum sit dolore rem iste aliquid."`;

exports[`lorem > 42 > text > with length 1`] = `"Qui tempore quo perspiciatis perspiciatis quia in quia architecto ipsa. Pariatur veniam unde fugit facilis mollitia. Ipsa reiciendis nam. Earum dolorem alias qui asperiores qui natus aliquam iste. Consequatur eum sit dolore rem iste aliquid."`;
exports[`lorem > 42 > text > with length 1`] = `"Qui tempo."`;

exports[`lorem > 42 > text > with length range 1`] = `"Qui tempore quo perspiciatis perspiciatis quia in quia architecto ipsa. Pariatur veniam unde fugit facilis mollitia. Ipsa reiciendis nam. Earum dolorem alias qui asperiores qui natus aliquam iste. Consequatur eum sit dolore rem iste aliquid."`;
exports[`lorem > 42 > text > with length range 1`] = `""`;

exports[`lorem > 42 > word > noArgs 1`] = `"voluptas"`;

Expand Down Expand Up @@ -233,17 +233,9 @@ Ullam consequuntur quidem nobis placeat aut.
Est animi libero repellendus recusandae iure magnam pariatur doloribus."
`;

exports[`lorem > 1211 > text > with length 1`] = `
"Quo amet explicabo repellat accusantium laborum quia fuga dolorem cumque.
Ullam consequuntur quidem nobis placeat aut.
Est animi libero repellendus recusandae iure magnam pariatur doloribus."
`;
exports[`lorem > 1211 > text > with length 1`] = `"Quo amet ."`;

exports[`lorem > 1211 > text > with length range 1`] = `
"Quo amet explicabo repellat accusantium laborum quia fuga dolorem cumque.
Ullam consequuntur quidem nobis placeat aut.
Est animi libero repellendus recusandae iure magnam pariatur doloribus."
`;
exports[`lorem > 1211 > text > with length range 1`] = `""`;

exports[`lorem > 1211 > word > noArgs 1`] = `"recusandae"`;

Expand Down Expand Up @@ -354,9 +346,9 @@ exports[`lorem > 1337 > slug > with length range 1`] = `"molestias-quia-dolorem-

exports[`lorem > 1337 > text > noArgs 1`] = `"Dolorem incidunt atque esse. Ad consectetur totam. Eius veniam voluptatibus tempora tempore aliquam vitae. Laboriosam quasi similique nemo sunt cum doloribus consequuntur reprehenderit."`;

exports[`lorem > 1337 > text > with length 1`] = `"Dolorem incidunt atque esse. Ad consectetur totam. Eius veniam voluptatibus tempora tempore aliquam vitae. Laboriosam quasi similique nemo sunt cum doloribus consequuntur reprehenderit."`;
exports[`lorem > 1337 > text > with length 1`] = `"Dolorem i."`;

exports[`lorem > 1337 > text > with length range 1`] = `"Dolorem incidunt atque esse. Ad consectetur totam. Eius veniam voluptatibus tempora tempore aliquam vitae. Laboriosam quasi similique nemo sunt cum doloribus consequuntur reprehenderit."`;
exports[`lorem > 1337 > text > with length range 1`] = `""`;

exports[`lorem > 1337 > word > noArgs 1`] = `"eius"`;

Expand Down
111 changes: 111 additions & 0 deletions test/lorem.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,117 @@ describe('lorem', () => {
expect(actual).toBeTruthy();
expect(actual).toBeTypeOf('string');
});

it('should return text of length 15 for the options object', () => {
const actual = faker.lorem.text({ length: 15 });

expect(actual).toBeTruthy();
expect(actual).toBeTypeOf('string');
expect(actual).toHaveLength(15);
expect(actual.trim()).toHaveLength(15);
});

it('should return text of length 15 for the quick primitive argument', () => {
const actual = faker.lorem.text(15);

expect(actual).toBeTruthy();
expect(actual).toBeTypeOf('string');
expect(actual).toHaveLength(15);
expect(actual.trim()).toHaveLength(15);
});

it('should return text with a length in range [25, 40]', () => {
const actual = faker.lorem.text({ length: { min: 25, max: 40 } });

expect(actual).toBeTruthy();
expect(actual).toBeTypeOf('string');
expect(actual.length).toBeGreaterThanOrEqual(25);
expect(actual.length).toBeLessThanOrEqual(40);
});

it('should throw an error for a negative length in the options object', () => {
expect(() => faker.lorem.text({ length: -3 })).toThrowError(
/^Length -3 should be a non-negative integer.$/
);
});

it('should throw an error for a negative length in the quick primitive argument', () => {
expect(() => faker.lorem.text(-3)).toThrowError(
/^Length -3 should be a non-negative integer.$/
);
});

it('should throw an error if min is negative', () => {
expect(() => faker.lorem.text({ length: { min: -11 } })).toThrowError(
/^Min -11 should be a non-negative integer.$/
);
});

it('should throw an error if min is greater than max', () => {
expect(() =>
faker.lorem.text({ length: { min: 3, max: 1 } })
).toThrowError(/^Max 1 should be greater than min 3.$/);
});

it('should throw an error if max is negative', () => {
expect(() => faker.lorem.text({ length: { max: -11 } })).toThrowError(
/^Max -11 should be a non-negative integer.$/
);
});

it('should return an empty string for length zero', () => {
const actual = faker.lorem.text({ length: 0 });

expect(actual).toBeTypeOf('string');
expect(actual).toBe('');
});

it('should return a dot for length one', () => {
const actual = faker.lorem.text({ length: 1 });

expect(actual).toBeTypeOf('string');
expect(actual).toBe('.');
});

it('should return a string of length 0 or length 1 for [0, 1]', () => {
const actual = faker.lorem.text({ length: { min: 0, max: 1 } });

expect(actual).toBeTypeOf('string');
expect(actual.length).toBeGreaterThanOrEqual(0);
expect(actual.length).toBeLessThanOrEqual(1);
});

it('should return text with length between 0 and 32 for [0, 32]', () => {
const actual = faker.lorem.text({ length: { min: 0, max: 32 } });

expect(actual).toBeTypeOf('string');
expect(actual.length).toBeGreaterThanOrEqual(0);
expect(actual.length).toBeLessThanOrEqual(32);
});

it('should return a string of length 77 for [77, 77]', () => {
const actual = faker.lorem.text({ length: { min: 77, max: 77 } });

expect(actual).toBeTruthy();
expect(actual).toBeTypeOf('string');
expect(actual).toHaveLength(77);
});

it('should return a string with length in [33, 66] for min = 33', () => {
const actual = faker.lorem.text({ length: { min: 33 } });

expect(actual).toBeTypeOf('string');
expect(actual.length).toBeGreaterThanOrEqual(33);
expect(actual.length).toBeLessThanOrEqual(66);
});

it('should return a string with length between 0 and 51 for max = 51', () => {
const actual = faker.lorem.text({ length: { max: 51 } });

expect(actual).toBeTypeOf('string');
expect(actual.length).toBeGreaterThanOrEqual(0);
expect(actual.length).toBeLessThanOrEqual(51);
});
});

describe('lines()', () => {
Expand Down