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 all 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
49 changes: 47 additions & 2 deletions src/modules/lorem/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,9 @@ 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 of text to generate as number or range.
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
* @param options.length The length of text to generate as number or range.
* @param options.length The length of text (in characters) to generate as number or range.

*
* @example
* faker.lorem.text() // 'Doloribus autem non quis vero quia.'
* faker.lorem.text()
Expand All @@ -318,10 +321,33 @@ 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.'
* faker.lorem.text({length: 14}) // 'Doloribus aut.'
* faker.lorem.text({length: {min: 10, max: 15}}) // 'autem non quis.'
*
* @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 @@ -332,7 +358,26 @@ export class LoremModule {

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

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

if (options.length == null) {
return `${this[method]()}`;
}

const length = this.faker.helpers.rangeToNumber(options.length);
Comment on lines +365 to +369
Copy link
Member

Choose a reason for hiding this comment

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

Please use our standard way of decstructuring options:

Suggested change
if (options.length == null) {
return `${this[method]()}`;
}
const length = this.faker.helpers.rangeToNumber(options.length);
const { length: lengthOrRange } = options;
if (lengthOrRange == null) {
return `${this[method]()}`;
}
const length = this.faker.helpers.rangeToNumber(lengthOrRange);


if (length <= 0) {
return '';
}

let text = '';
do {
text = `${text}${this[method]()} `;
Copy link
Member

@ST-DDT ST-DDT Mar 28, 2024

Choose a reason for hiding this comment

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

Please only use this.sentence() here and maybe randomly (e.g. faker.datatype.boolean()) add a newline to simulate paragraphs/lines.

} while (text.length < length);

return `${text.substring(0, length).replace(/.$/, '.')}`;
Copy link
Member

Choose a reason for hiding this comment

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

Please just return the value without dot at the end.

Suggested change
return `${text.substring(0, length).replace(/.$/, '.')}`;
return text.substring(0, length);

}

/**
Expand Down
10 changes: 3 additions & 7 deletions test/modules/__snapshots__/lorem.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ exports[`lorem > 42 > slug > with length range 1`] = `"theca-virga-auctus-synago

exports[`lorem > 42 > text > noArgs 1`] = `"Auctus synagoga tergum patruus patria armarium decumbo armarium alveus adsum. Usitas concido paulatim aranea suggero soleo. Adstringo volubilis suppono. Vigilo benevolentia a attonbitus vulgivagus auctus provident cognomen perspiciatis. Absens custodia acervus clamo esse perspiciatis cubo."`;

exports[`lorem > 42 > text > with length 1`] = `"Auctus synagoga tergum patruus patria armarium decumbo armarium alveus adsum. Usitas concido paulatim aranea suggero soleo. Adstringo volubilis suppono. Vigilo benevolentia a attonbitus vulgivagus auctus provident cognomen perspiciatis. Absens custodia acervus clamo esse perspiciatis cubo."`;
exports[`lorem > 42 > text > with length 1`] = `"Auctus sy."`;

exports[`lorem > 42 > text > with length range 1`] = `"Auctus synagoga tergum patruus patria armarium decumbo armarium alveus adsum. Usitas concido paulatim aranea suggero soleo. Adstringo volubilis suppono. Vigilo benevolentia a attonbitus vulgivagus auctus provident cognomen perspiciatis. Absens custodia acervus clamo esse perspiciatis cubo."`;

Expand Down Expand Up @@ -233,11 +233,7 @@ Considero ascit suasoria tamisium tonsor accendo.
Sophismata sollers sursum tripudio vester dapifer coaegresco usque vorax."
`;

exports[`lorem > 1211 > text > with length 1`] = `
"Tergo caelum aperio vulpes adficio spectaculum articulus stillicidium bene tendo.
Considero ascit suasoria tamisium tonsor accendo.
Sophismata sollers sursum tripudio vester dapifer coaegresco usque vorax."
`;
exports[`lorem > 1211 > text > with length 1`] = `"Tergo cae."`;

exports[`lorem > 1211 > text > with length range 1`] = `
"Tergo caelum aperio vulpes adficio spectaculum articulus stillicidium bene tendo.
Expand Down Expand Up @@ -354,7 +350,7 @@ exports[`lorem > 1337 > slug > with length range 1`] = `"laborum-articulus-benev

exports[`lorem > 1337 > text > noArgs 1`] = `"Benevolentia chirographum illo degenero. Commemoro canto eaque. Cedo conculco voluptatibus cerno tabella cohors ancilla. Creptio allatus quisquam conventus ante talio vorago artificiose decipio."`;

exports[`lorem > 1337 > text > with length 1`] = `"Benevolentia chirographum illo degenero. Commemoro canto eaque. Cedo conculco voluptatibus cerno tabella cohors ancilla. Creptio allatus quisquam conventus ante talio vorago artificiose decipio."`;
exports[`lorem > 1337 > text > with length 1`] = `"Benevolen."`;

exports[`lorem > 1337 > text > with length range 1`] = `"Benevolentia chirographum illo degenero. Commemoro canto eaque. Cedo conculco voluptatibus cerno tabella cohors ancilla. Creptio allatus quisquam conventus ante talio vorago artificiose decipio."`;

Expand Down
47 changes: 47 additions & 0 deletions test/modules/lorem.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,53 @@ 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 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 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('.');
});
Comment on lines +369 to +374
Copy link
Member

Choose a reason for hiding this comment

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

This test is no longer needed,

});

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