aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEric Cheng <[email protected]>2022-12-06 18:13:51 -0500
committerGitHub <[email protected]>2022-12-07 00:13:51 +0100
commitc4b7ce8648cbd5ac2b224942908bccf9914e08f9 (patch)
tree82c0d99fe5c45cedb542ac05aaad3f2e8921df83
parent6bac476695c146a794b5337785979a7395d7dc89 (diff)
downloadfaker-c4b7ce8648cbd5ac2b224942908bccf9914e08f9.tar.xz
faker-c4b7ce8648cbd5ac2b224942908bccf9914e08f9.zip
refactor(string)!: swap `allowLeadingZeros` default to `true` (#1602)
-rw-r--r--docs/guide/upgrading.md4
-rw-r--r--src/modules/helpers/index.ts2
-rw-r--r--src/modules/number/index.ts2
-rw-r--r--src/modules/random/index.ts4
-rw-r--r--src/modules/string/index.ts10
-rw-r--r--test/__snapshots__/random.spec.ts.snap8
-rw-r--r--test/__snapshots__/string.spec.ts.snap44
-rw-r--r--test/random.spec.ts4
-rw-r--r--test/string.spec.ts6
9 files changed, 46 insertions, 38 deletions
diff --git a/docs/guide/upgrading.md b/docs/guide/upgrading.md
index a5e0dfee..d5833e50 100644
--- a/docs/guide/upgrading.md
+++ b/docs/guide/upgrading.md
@@ -113,3 +113,7 @@ For the old `faker.datatype.number` method you should replace with `faker.number
| `faker.datatype.number` | `faker.number.int` or `faker.number.float` |
| `faker.datatype.float` | `faker.number.float` |
| `faker.datatype.bigInt` | `faker.number.bigInt` |
+
+### `allowLeadingZeros` behavior change in `faker.string.numeric`
+
+The `allowLeadingZeros` boolean parameter in `faker.string.numeric` (in the new `string` module) now defaults to `true`. `faker.string.numeric` will now generate numeric strings that could have leading zeros by default.
diff --git a/src/modules/helpers/index.ts b/src/modules/helpers/index.ts
index 7f2c5898..af6ae4b8 100644
--- a/src/modules/helpers/index.ts
+++ b/src/modules/helpers/index.ts
@@ -539,7 +539,7 @@ export class HelpersModule {
* faker.helpers.fake('Good Morning {{person.firstName}}!') // 'Good Morning Estelle!'
* faker.helpers.fake('You can call me at {{phone.number(!## ### #####!)}}.') // 'You can call me at 202 555 973722.'
* faker.helpers.fake('I flipped the coin and got: {{helpers.arrayElement(["heads", "tails"])}}') // 'I flipped the coin and got: tails'
- * faker.helpers.fake('I rolled the dice and got: {{string.numeric(1, {"allowLeadingZeros": true})}}') // 'I rolled the dice and got: 6'
+ * faker.helpers.fake('Your PIN number is: {{string.numeric(4, {"exclude": ["0"]})}}') // 'Your PIN number is: 4834'
*
* @since 7.4.0
*/
diff --git a/src/modules/number/index.ts b/src/modules/number/index.ts
index c30d5015..1f46cb64 100644
--- a/src/modules/number/index.ts
+++ b/src/modules/number/index.ts
@@ -26,6 +26,8 @@ export class NumberModule {
*
* @throws When options define `max < min`.
*
+ * @see faker.string.numeric() If you would like to generate a `string` of digits with a given length (range).
+ *
* @example
* faker.number.int() // 55422
* faker.number.int(100) // 52
diff --git a/src/modules/random/index.ts b/src/modules/random/index.ts
index 21e46509..f8691198 100644
--- a/src/modules/random/index.ts
+++ b/src/modules/random/index.ts
@@ -262,7 +262,7 @@ export class RandomModule {
*
* @param length The number of digits to generate. Defaults to `1`.
* @param options The options to use. Defaults to `{}`.
- * @param options.allowLeadingZeros If true, leading zeros will be allowed. Defaults to `false`.
+ * @param options.allowLeadingZeros Whether leading zeros are allowed or not. Defaults to `true`.
* @param options.bannedDigits An array of digits which should be banned in the generated string. Defaults to `[]`.
*
* @see faker.string.numeric()
@@ -270,7 +270,7 @@ export class RandomModule {
* @example
* faker.random.numeric() // '2'
* faker.random.numeric(5) // '31507'
- * faker.random.numeric(42) // '56434563150765416546479875435481513188548'
+ * faker.random.numeric(42) // '00434563150765416546479875435481513188548'
* faker.random.numeric(42, { allowLeadingZeros: true }) // '00564846278453876543517840713421451546115'
* faker.random.numeric(6, { bannedDigits: ['0'] }) // '943228'
*
diff --git a/src/modules/string/index.ts b/src/modules/string/index.ts
index de1ec446..827c1f45 100644
--- a/src/modules/string/index.ts
+++ b/src/modules/string/index.ts
@@ -317,15 +317,17 @@ export class StringModule {
*
* @param options Either the number of characters or the options to use.
* @param options.length The number or range of digits to generate. Defaults to `1`.
- * @param options.allowLeadingZeros If true, leading zeros will be allowed. Defaults to `false`.
+ * @param options.allowLeadingZeros Whether leading zeros are allowed or not. Defaults to `true`.
* @param options.exclude An array of digits which should be excluded in the generated string. Defaults to `[]`.
*
+ * @see faker.number.int() If you would like to generate a `number` (within a range).
+ *
* @example
* faker.string.numeric() // '2'
* faker.string.numeric(5) // '31507'
- * faker.string.numeric(42) // '56434563150765416546479875435481513188548'
+ * faker.string.numeric(42) // '06434563150765416546479875435481513188548'
* faker.string.numeric({ length: { min: 5, max: 10 } }) // '197089478'
- * faker.string.numeric({ length: 42, allowLeadingZeros: true }) // '00564846278453876543517840713421451546115'
+ * faker.string.numeric({ length: 42, allowLeadingZeros: false }) // '72564846278453876543517840713421451546115'
* faker.string.numeric({ length: 6, exclude: ['0'] }) // '943228'
*
* @since 8.0.0
@@ -350,7 +352,7 @@ export class StringModule {
return '';
}
- const { allowLeadingZeros = false } = options;
+ const { allowLeadingZeros = true } = options;
let { exclude = [] } = options;
if (typeof exclude === 'string') {
diff --git a/test/__snapshots__/random.spec.ts.snap b/test/__snapshots__/random.spec.ts.snap
index ade199df..c7651925 100644
--- a/test/__snapshots__/random.spec.ts.snap
+++ b/test/__snapshots__/random.spec.ts.snap
@@ -10,9 +10,9 @@ exports[`random > 42 > alphaNumeric > withLength 1`] = `"nNWbJ"`;
exports[`random > 42 > locale 1`] = `"es"`;
-exports[`random > 42 > numeric > noArgs 1`] = `"4"`;
+exports[`random > 42 > numeric > noArgs 1`] = `"3"`;
-exports[`random > 42 > numeric > withLength 1`] = `"47917"`;
+exports[`random > 42 > numeric > withLength 1`] = `"37917"`;
exports[`random > 42 > word 1`] = `"responsive"`;
@@ -50,9 +50,9 @@ exports[`random > 1337 > alphaNumeric > withLength 1`] = `"gy9dh"`;
exports[`random > 1337 > locale 1`] = `"en_GH"`;
-exports[`random > 1337 > numeric > noArgs 1`] = `"3"`;
+exports[`random > 1337 > numeric > noArgs 1`] = `"2"`;
-exports[`random > 1337 > numeric > withLength 1`] = `"35122"`;
+exports[`random > 1337 > numeric > withLength 1`] = `"25122"`;
exports[`random > 1337 > word 1`] = `"Bespoke"`;
diff --git a/test/__snapshots__/string.spec.ts.snap b/test/__snapshots__/string.spec.ts.snap
index 339f7aec..9de8545d 100644
--- a/test/__snapshots__/string.spec.ts.snap
+++ b/test/__snapshots__/string.spec.ts.snap
@@ -68,25 +68,25 @@ exports[`string > 42 > hexadecimal > with length range 1`] = `"0xBE4ABdd39321aD"
exports[`string > 42 > hexadecimal > with length, casing and empty prefix 1`] = `"8be4abd"`;
-exports[`string > 42 > numeric > noArgs 1`] = `"4"`;
+exports[`string > 42 > numeric > noArgs 1`] = `"3"`;
-exports[`string > 42 > numeric > with allowLeadingZeros 1`] = `"3"`;
+exports[`string > 42 > numeric > with allowLeadingZeros 1`] = `"4"`;
-exports[`string > 42 > numeric > with exclude 1`] = `"7"`;
+exports[`string > 42 > numeric > with exclude 1`] = `"6"`;
-exports[`string > 42 > numeric > with length 1`] = `"479177"`;
+exports[`string > 42 > numeric > with length 1`] = `"379177"`;
-exports[`string > 42 > numeric > with length parameter 1`] = `"47917"`;
+exports[`string > 42 > numeric > with length parameter 1`] = `"37917"`;
-exports[`string > 42 > numeric > with length parameter 2`] = `"85514"`;
+exports[`string > 42 > numeric > with length parameter 2`] = `"75514"`;
-exports[`string > 42 > numeric > with length parameter 3`] = `"20048"`;
+exports[`string > 42 > numeric > with length parameter 3`] = `"10048"`;
-exports[`string > 42 > numeric > with length parameter 4`] = `"46176"`;
+exports[`string > 42 > numeric > with length parameter 4`] = `"36176"`;
-exports[`string > 42 > numeric > with length parameter 5`] = `"10978"`;
+exports[`string > 42 > numeric > with length parameter 5`] = `"00978"`;
-exports[`string > 42 > numeric > with length range 1`] = `"89177551410048"`;
+exports[`string > 42 > numeric > with length range 1`] = `"79177551410048"`;
exports[`string > 42 > numeric > with length, allowLeadingZeros and exclude 1`] = `"6890887"`;
@@ -192,15 +192,15 @@ exports[`string > 1211 > numeric > with length 1`] = `"948721"`;
exports[`string > 1211 > numeric > with length parameter 1`] = `"94872"`;
-exports[`string > 1211 > numeric > with length parameter 2`] = `"29061"`;
+exports[`string > 1211 > numeric > with length parameter 2`] = `"19061"`;
-exports[`string > 1211 > numeric > with length parameter 3`] = `"72743"`;
+exports[`string > 1211 > numeric > with length parameter 3`] = `"62743"`;
-exports[`string > 1211 > numeric > with length parameter 4`] = `"26780"`;
+exports[`string > 1211 > numeric > with length parameter 4`] = `"16780"`;
exports[`string > 1211 > numeric > with length parameter 5`] = `"76678"`;
-exports[`string > 1211 > numeric > with length range 1`] = `"58721906162743167807"`;
+exports[`string > 1211 > numeric > with length range 1`] = `"48721906162743167807"`;
exports[`string > 1211 > numeric > with length, allowLeadingZeros and exclude 1`] = `"9798609"`;
@@ -296,25 +296,25 @@ exports[`string > 1337 > hexadecimal > with length range 1`] = `"0xc346ba075bd5"
exports[`string > 1337 > hexadecimal > with length, casing and empty prefix 1`] = `"5c346ba"`;
-exports[`string > 1337 > numeric > noArgs 1`] = `"3"`;
+exports[`string > 1337 > numeric > noArgs 1`] = `"2"`;
-exports[`string > 1337 > numeric > with allowLeadingZeros 1`] = `"2"`;
+exports[`string > 1337 > numeric > with allowLeadingZeros 1`] = `"3"`;
-exports[`string > 1337 > numeric > with exclude 1`] = `"7"`;
+exports[`string > 1337 > numeric > with exclude 1`] = `"6"`;
-exports[`string > 1337 > numeric > with length 1`] = `"351225"`;
+exports[`string > 1337 > numeric > with length 1`] = `"251225"`;
-exports[`string > 1337 > numeric > with length parameter 1`] = `"35122"`;
+exports[`string > 1337 > numeric > with length parameter 1`] = `"25122"`;
exports[`string > 1337 > numeric > with length parameter 2`] = `"54032"`;
exports[`string > 1337 > numeric > with length parameter 3`] = `"55239"`;
-exports[`string > 1337 > numeric > with length parameter 4`] = `"37318"`;
+exports[`string > 1337 > numeric > with length parameter 4`] = `"27318"`;
-exports[`string > 1337 > numeric > with length parameter 5`] = `"40631"`;
+exports[`string > 1337 > numeric > with length parameter 5`] = `"30631"`;
-exports[`string > 1337 > numeric > with length range 1`] = `"612254032552"`;
+exports[`string > 1337 > numeric > with length range 1`] = `"512254032552"`;
exports[`string > 1337 > numeric > with length, allowLeadingZeros and exclude 1`] = `"6706677"`;
diff --git a/test/random.spec.ts b/test/random.spec.ts
index 2e84a713..67a04c6d 100644
--- a/test/random.spec.ts
+++ b/test/random.spec.ts
@@ -357,7 +357,7 @@ describe('random', () => {
const actual = faker.random.numeric(length);
expect(actual).toHaveLength(length);
- expect(actual).toMatch(/^[1-9][0-9]*$/);
+ expect(actual).toMatch(/^[0-9]*$/);
}
);
@@ -378,7 +378,7 @@ describe('random', () => {
expect(actual).toBeTypeOf('string');
expect(actual).toHaveLength(1000);
- expect(actual).toMatch(/^[1-9][0-9]+$/);
+ expect(actual).toMatch(/^[0-9]+$/);
});
it('should allow leading zeros via option', () => {
diff --git a/test/string.spec.ts b/test/string.spec.ts
index f3073c0f..f07f1e16 100644
--- a/test/string.spec.ts
+++ b/test/string.spec.ts
@@ -59,7 +59,7 @@ describe('string', () => {
.itRepeated('with length parameter', 5, 5)
.it('with length', { length: 6 })
.it('with length range', { length: { min: 10, max: 20 } })
- .it('with allowLeadingZeros', { allowLeadingZeros: true })
+ .it('with allowLeadingZeros', { allowLeadingZeros: false })
.it('with exclude', { exclude: '12345' })
.it('with length, allowLeadingZeros and exclude', {
length: 7,
@@ -382,7 +382,7 @@ describe('string', () => {
const actual = faker.string.numeric();
expect(actual).toHaveLength(1);
- expect(actual).toMatch(/^[1-9]$/);
+ expect(actual).toMatch(/^[0-9]$/);
});
it.each(times(100))(
@@ -391,7 +391,7 @@ describe('string', () => {
const actual = faker.string.numeric(length);
expect(actual).toHaveLength(length);
- expect(actual).toMatch(/^[1-9][0-9]*$/);
+ expect(actual).toMatch(/^[0-9]*$/);
}
);