Skip to content

Commit

Permalink
refactor(datatype): standardize arguments (#1804)
Browse files Browse the repository at this point in the history
  • Loading branch information
xDivisionByZerox authored Feb 4, 2023
1 parent 667599d commit 1b9ca01
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
23 changes: 21 additions & 2 deletions src/modules/datatype/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,25 +206,44 @@ export class DatatypeModule {
/**
* Returns a string containing UTF-16 chars between 33 and 125 (`!` to `}`).
*
* @param length Length of the generated string. Max length is `2^20`. Defaults to `10`.
* @param options Length of the generated string or an options object. Defaults to `{}`.
* @param options.length Length of the generated string. Max length is `2^20`. Defaults to `10`.
*
* @see faker.string.sample()
*
* @example
* faker.datatype.string() // 'Zo!.:*e>wR'
* faker.datatype.string(5) // '6Bye8'
* faker.datatype.string({ length: 7 }) // 'dzOT00e'
*
* @since 5.5.0
*
* @deprecated Use faker.string.sample() instead.
*/
string(length = 10): string {
string(
options:
| number
| {
/**
* Length of the generated string. Max length is `2^20`.
*
* @default 10
*/
length?: number;
} = {}
): string {
deprecated({
deprecated: 'faker.datatype.string()',
proposed: 'faker.string.sample()',
since: '8.0',
until: '9.0',
});
if (typeof options === 'number') {
options = { length: options };
}

const { length = 10 } = options;

return this.faker.string.sample(length);
}

Expand Down
12 changes: 9 additions & 3 deletions test/__snapshots__/datatype.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@ exports[`datatype > 42 > number > with min, max and precision 1`] = `-0.43`;

exports[`datatype > 42 > string > noArgs 1`] = `"Cky2eiXX/J"`;

exports[`datatype > 42 > string > with length 1`] = `"Cky2eiXX/J/*&[email protected]]\\"&{dnx4!1}2Z=YQ!I#<QYF"`;
exports[`datatype > 42 > string > with length option 1`] = `"Cky2eiXX/J/*&[email protected]]\\"&"`;

exports[`datatype > 42 > string > with number 1`] = `"Cky2eiXX/J/*&[email protected]]\\"&{dnx4!1}2Z=YQ!I#<QYF"`;

exports[`datatype > 42 > uuid 1`] = `"5cf2bc99-2721-407d-992b-a00fbdf302f2"`;

Expand Down Expand Up @@ -247,7 +249,9 @@ exports[`datatype > 1211 > number > with min, max and precision 1`] = `61.07`;

exports[`datatype > 1211 > string > noArgs 1`] = `"wKti5-}$_/"`;

exports[`datatype > 1211 > string > with length 1`] = `"wKti5-}$_/\`4hHA0afl\\"h^]dnwI<q|p|5KWu3/CZ|J"`;
exports[`datatype > 1211 > string > with length option 1`] = `"wKti5-}$_/\`4hHA0afl\\"h^"`;

exports[`datatype > 1211 > string > with number 1`] = `"wKti5-}$_/\`4hHA0afl\\"h^]dnwI<q|p|5KWu3/CZ|J"`;

exports[`datatype > 1211 > uuid 1`] = `"e7ec32f0-a2a3-4c65-abbd-0caabde64dfd"`;

Expand Down Expand Up @@ -375,7 +379,9 @@ exports[`datatype > 1337 > number > with min, max and precision 1`] = `-12.92`;
exports[`datatype > 1337 > string > noArgs 1`] = `"9U/4:SK$>6"`;
exports[`datatype > 1337 > string > with length 1`] = `"9U/4:SK$>6QX9@{:e=+kD)[B,e|/Jqjjj!BLGDWQgC"`;
exports[`datatype > 1337 > string > with length option 1`] = `"9U/4:SK$>6QX9@{:e=+kD)"`;
exports[`datatype > 1337 > string > with number 1`] = `"9U/4:SK$>6QX9@{:e=+kD)[B,e|/Jqjjj!BLGDWQgC"`;
exports[`datatype > 1337 > uuid 1`] = `"48234870-5389-445f-8b41-c61a52bf27dc"`;
Expand Down
4 changes: 3 additions & 1 deletion test/datatype.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ describe('datatype', () => {
});

t.describe('string', (t) => {
t.it('noArgs').it('with length', 42);
t.it('noArgs')
.it('with number', 42)
.it('with length option', { length: 22 });
});

t.itRepeated('uuid', 5);
Expand Down

0 comments on commit 1b9ca01

Please sign in to comment.