Skip to content

Commit

Permalink
refactor(string)!: swap allowLeadingZeros default to true (#1602)
Browse files Browse the repository at this point in the history
  • Loading branch information
import-brain committed Dec 6, 2022
1 parent 6bac476 commit c4b7ce8
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 38 deletions.
4 changes: 4 additions & 0 deletions docs/guide/upgrading.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
2 changes: 1 addition & 1 deletion src/modules/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
2 changes: 2 additions & 0 deletions src/modules/number/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/modules/random/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,15 +262,15 @@ 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()
*
* @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'
*
Expand Down
10 changes: 6 additions & 4 deletions src/modules/string/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -350,7 +352,7 @@ export class StringModule {
return '';
}

const { allowLeadingZeros = false } = options;
const { allowLeadingZeros = true } = options;
let { exclude = [] } = options;

if (typeof exclude === 'string') {
Expand Down
8 changes: 4 additions & 4 deletions test/__snapshots__/random.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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"`;

Expand Down Expand Up @@ -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"`;

Expand Down
44 changes: 22 additions & 22 deletions test/__snapshots__/string.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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"`;

Expand Down Expand Up @@ -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"`;
Expand Down Expand Up @@ -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"`;
Expand Down
4 changes: 2 additions & 2 deletions test/random.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]*$/);
}
);

Expand All @@ -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', () => {
Expand Down
6 changes: 3 additions & 3 deletions test/string.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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))(
Expand All @@ -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]*$/);
}
);

Expand Down

0 comments on commit c4b7ce8

Please sign in to comment.