aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDivisionByZero <[email protected]>2023-02-04 22:13:20 +0100
committerGitHub <[email protected]>2023-02-04 21:13:20 +0000
commit1b9ca0192095b69022b0a4ddc0d73db1e2c3fe17 (patch)
treedb55989ecbb9ca85dd14eceb1402a4cd300c5e20
parent667599d8fb59c31166b897799f30788edc5f54d7 (diff)
downloadfaker-1b9ca0192095b69022b0a4ddc0d73db1e2c3fe17.tar.xz
faker-1b9ca0192095b69022b0a4ddc0d73db1e2c3fe17.zip
refactor(datatype): standardize arguments (#1804)
-rw-r--r--src/modules/datatype/index.ts23
-rw-r--r--test/__snapshots__/datatype.spec.ts.snap12
-rw-r--r--test/datatype.spec.ts4
3 files changed, 33 insertions, 6 deletions
diff --git a/src/modules/datatype/index.ts b/src/modules/datatype/index.ts
index 9a89ba24..25235bb5 100644
--- a/src/modules/datatype/index.ts
+++ b/src/modules/datatype/index.ts
@@ -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);
}
diff --git a/test/__snapshots__/datatype.spec.ts.snap b/test/__snapshots__/datatype.spec.ts.snap
index eece8554..8820e6f6 100644
--- a/test/__snapshots__/datatype.spec.ts.snap
+++ b/test/__snapshots__/datatype.spec.ts.snap
@@ -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"`;
@@ -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"`;
@@ -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"`;
diff --git a/test/datatype.spec.ts b/test/datatype.spec.ts
index 9a8cb1bb..2c09ea05 100644
--- a/test/datatype.spec.ts
+++ b/test/datatype.spec.ts
@@ -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);