Skip to content

Commit 1a5cb44

Browse files
committed
chore: merge next
2 parents 83c614e + c4b7ce8 commit 1a5cb44

File tree

9 files changed

+47
-39
lines changed

9 files changed

+47
-39
lines changed

docs/guide/upgrading.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,7 @@ For the old `faker.datatype.number` method you should replace with `faker.number
113113
| `faker.datatype.number` | `faker.number.int` or `faker.number.float` |
114114
| `faker.datatype.float` | `faker.number.float` |
115115
| `faker.datatype.bigInt` | `faker.number.bigInt` |
116+
117+
### `allowLeadingZeros` behavior change in `faker.string.numeric`
118+
119+
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.

src/modules/helpers/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ export class HelpersModule {
539539
* faker.helpers.fake('Good Morning {{person.firstName}}!') // 'Good Morning Estelle!'
540540
* faker.helpers.fake('You can call me at {{phone.number(!## ### #####!)}}.') // 'You can call me at 202 555 973722.'
541541
* faker.helpers.fake('I flipped the coin and got: {{helpers.arrayElement(["heads", "tails"])}}') // 'I flipped the coin and got: tails'
542-
* faker.helpers.fake('I rolled the dice and got: {{string.numeric(1, {"allowLeadingZeros": true})}}') // 'I rolled the dice and got: 6'
542+
* faker.helpers.fake('Your PIN number is: {{string.numeric(4, {"exclude": ["0"]})}}') // 'Your PIN number is: 4834'
543543
*
544544
* @since 7.4.0
545545
*/

src/modules/number/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ export class NumberModule {
2626
*
2727
* @throws When options define `max < min`.
2828
*
29+
* @see faker.string.numeric() If you would like to generate a `string` of digits with a given length (range).
30+
*
2931
* @example
3032
* faker.number.int() // 55422
3133
* faker.number.int(100) // 52

src/modules/random/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,15 +257,15 @@ export class RandomModule {
257257
*
258258
* @param length The number of digits to generate. Defaults to `1`.
259259
* @param options The options to use. Defaults to `{}`.
260-
* @param options.allowLeadingZeros If true, leading zeros will be allowed. Defaults to `false`.
260+
* @param options.allowLeadingZeros Whether leading zeros are allowed or not. Defaults to `true`.
261261
* @param options.bannedDigits An array of digits which should be banned in the generated string. Defaults to `[]`.
262262
*
263263
* @see faker.string.numeric()
264264
*
265265
* @example
266266
* faker.random.numeric() // '2'
267267
* faker.random.numeric(5) // '31507'
268-
* faker.random.numeric(42) // '56434563150765416546479875435481513188548'
268+
* faker.random.numeric(42) // '00434563150765416546479875435481513188548'
269269
* faker.random.numeric(42, { allowLeadingZeros: true }) // '00564846278453876543517840713421451546115'
270270
* faker.random.numeric(6, { bannedDigits: ['0'] }) // '943228'
271271
*

src/modules/string/index.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -317,15 +317,17 @@ export class StringModule {
317317
*
318318
* @param options Either the number of characters or the options to use.
319319
* @param options.length The number or range of digits to generate. Defaults to `1`.
320-
* @param options.allowLeadingZeros If true, leading zeros will be allowed. Defaults to `false`.
320+
* @param options.allowLeadingZeros Whether leading zeros are allowed or not. Defaults to `true`.
321321
* @param options.exclude An array of digits which should be excluded in the generated string. Defaults to `[]`.
322322
*
323+
* @see faker.number.int() If you would like to generate a `number` (within a range).
324+
*
323325
* @example
324326
* faker.string.numeric() // '2'
325327
* faker.string.numeric(5) // '31507'
326-
* faker.string.numeric(42) // '56434563150765416546479875435481513188548'
328+
* faker.string.numeric(42) // '06434563150765416546479875435481513188548'
327329
* faker.string.numeric({ length: { min: 5, max: 10 } }) // '197089478'
328-
* faker.string.numeric({ length: 42, allowLeadingZeros: true }) // '00564846278453876543517840713421451546115'
330+
* faker.string.numeric({ length: 42, allowLeadingZeros: false }) // '72564846278453876543517840713421451546115'
329331
* faker.string.numeric({ length: 6, exclude: ['0'] }) // '943228'
330332
*
331333
* @since 8.0.0
@@ -350,7 +352,7 @@ export class StringModule {
350352
return '';
351353
}
352354

353-
const { allowLeadingZeros = false } = options;
355+
const { allowLeadingZeros = true } = options;
354356
let { exclude = [] } = options;
355357

356358
if (typeof exclude === 'string') {

test/__snapshots__/random.spec.ts.snap

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ exports[`random > 42 > alphaNumeric > with length 1`] = `"nNWbJ"`;
1010

1111
exports[`random > 42 > locale 1`] = `"es"`;
1212

13-
exports[`random > 42 > numeric > noArgs 1`] = `"4"`;
13+
exports[`random > 42 > numeric > noArgs 1`] = `"3"`;
1414

15-
exports[`random > 42 > numeric > with length 1`] = `"47917"`;
15+
exports[`random > 42 > numeric > with length 1`] = `"37917"`;
1616

1717
exports[`random > 42 > word 1`] = `"responsive"`;
1818

@@ -54,9 +54,9 @@ exports[`random > 1337 > alphaNumeric > with length 1`] = `"gy9dh"`;
5454

5555
exports[`random > 1337 > locale 1`] = `"en_GH"`;
5656

57-
exports[`random > 1337 > numeric > noArgs 1`] = `"3"`;
57+
exports[`random > 1337 > numeric > noArgs 1`] = `"2"`;
5858

59-
exports[`random > 1337 > numeric > with length 1`] = `"35122"`;
59+
exports[`random > 1337 > numeric > with length 1`] = `"25122"`;
6060

6161
exports[`random > 1337 > word 1`] = `"Bespoke"`;
6262

test/__snapshots__/string.spec.ts.snap

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -68,25 +68,25 @@ exports[`string > 42 > hexadecimal > with length range 1`] = `"0xBE4ABdd39321aD"
6868

6969
exports[`string > 42 > hexadecimal > with length, casing and empty prefix 1`] = `"8be4abd"`;
7070

71-
exports[`string > 42 > numeric > noArgs 1`] = `"4"`;
71+
exports[`string > 42 > numeric > noArgs 1`] = `"3"`;
7272

73-
exports[`string > 42 > numeric > with allowLeadingZeros 1`] = `"3"`;
73+
exports[`string > 42 > numeric > with allowLeadingZeros 1`] = `"4"`;
7474

75-
exports[`string > 42 > numeric > with exclude 1`] = `"7"`;
75+
exports[`string > 42 > numeric > with exclude 1`] = `"6"`;
7676

77-
exports[`string > 42 > numeric > with length 1`] = `"479177"`;
77+
exports[`string > 42 > numeric > with length 1`] = `"379177"`;
7878

79-
exports[`string > 42 > numeric > with length parameter 1`] = `"47917"`;
79+
exports[`string > 42 > numeric > with length parameter 1`] = `"37917"`;
8080

81-
exports[`string > 42 > numeric > with length parameter 2`] = `"85514"`;
81+
exports[`string > 42 > numeric > with length parameter 2`] = `"75514"`;
8282

83-
exports[`string > 42 > numeric > with length parameter 3`] = `"20048"`;
83+
exports[`string > 42 > numeric > with length parameter 3`] = `"10048"`;
8484

85-
exports[`string > 42 > numeric > with length parameter 4`] = `"46176"`;
85+
exports[`string > 42 > numeric > with length parameter 4`] = `"36176"`;
8686

87-
exports[`string > 42 > numeric > with length parameter 5`] = `"10978"`;
87+
exports[`string > 42 > numeric > with length parameter 5`] = `"00978"`;
8888

89-
exports[`string > 42 > numeric > with length range 1`] = `"89177551410048"`;
89+
exports[`string > 42 > numeric > with length range 1`] = `"79177551410048"`;
9090

9191
exports[`string > 42 > numeric > with length, allowLeadingZeros and exclude 1`] = `"6890887"`;
9292

@@ -192,15 +192,15 @@ exports[`string > 1211 > numeric > with length 1`] = `"948721"`;
192192
193193
exports[`string > 1211 > numeric > with length parameter 1`] = `"94872"`;
194194
195-
exports[`string > 1211 > numeric > with length parameter 2`] = `"29061"`;
195+
exports[`string > 1211 > numeric > with length parameter 2`] = `"19061"`;
196196
197-
exports[`string > 1211 > numeric > with length parameter 3`] = `"72743"`;
197+
exports[`string > 1211 > numeric > with length parameter 3`] = `"62743"`;
198198
199-
exports[`string > 1211 > numeric > with length parameter 4`] = `"26780"`;
199+
exports[`string > 1211 > numeric > with length parameter 4`] = `"16780"`;
200200
201201
exports[`string > 1211 > numeric > with length parameter 5`] = `"76678"`;
202202
203-
exports[`string > 1211 > numeric > with length range 1`] = `"58721906162743167807"`;
203+
exports[`string > 1211 > numeric > with length range 1`] = `"48721906162743167807"`;
204204
205205
exports[`string > 1211 > numeric > with length, allowLeadingZeros and exclude 1`] = `"9798609"`;
206206
@@ -296,25 +296,25 @@ exports[`string > 1337 > hexadecimal > with length range 1`] = `"0xc346ba075bd5"
296296
297297
exports[`string > 1337 > hexadecimal > with length, casing and empty prefix 1`] = `"5c346ba"`;
298298
299-
exports[`string > 1337 > numeric > noArgs 1`] = `"3"`;
299+
exports[`string > 1337 > numeric > noArgs 1`] = `"2"`;
300300
301-
exports[`string > 1337 > numeric > with allowLeadingZeros 1`] = `"2"`;
301+
exports[`string > 1337 > numeric > with allowLeadingZeros 1`] = `"3"`;
302302
303-
exports[`string > 1337 > numeric > with exclude 1`] = `"7"`;
303+
exports[`string > 1337 > numeric > with exclude 1`] = `"6"`;
304304
305-
exports[`string > 1337 > numeric > with length 1`] = `"351225"`;
305+
exports[`string > 1337 > numeric > with length 1`] = `"251225"`;
306306
307-
exports[`string > 1337 > numeric > with length parameter 1`] = `"35122"`;
307+
exports[`string > 1337 > numeric > with length parameter 1`] = `"25122"`;
308308
309309
exports[`string > 1337 > numeric > with length parameter 2`] = `"54032"`;
310310
311311
exports[`string > 1337 > numeric > with length parameter 3`] = `"55239"`;
312312
313-
exports[`string > 1337 > numeric > with length parameter 4`] = `"37318"`;
313+
exports[`string > 1337 > numeric > with length parameter 4`] = `"27318"`;
314314
315-
exports[`string > 1337 > numeric > with length parameter 5`] = `"40631"`;
315+
exports[`string > 1337 > numeric > with length parameter 5`] = `"30631"`;
316316
317-
exports[`string > 1337 > numeric > with length range 1`] = `"612254032552"`;
317+
exports[`string > 1337 > numeric > with length range 1`] = `"512254032552"`;
318318
319319
exports[`string > 1337 > numeric > with length, allowLeadingZeros and exclude 1`] = `"6706677"`;
320320

test/random.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ describe('random', () => {
364364
const actual = faker.random.numeric();
365365

366366
expect(actual).toHaveLength(1);
367-
expect(actual).toMatch(/^[1-9]$/);
367+
expect(actual).toMatch(/^[0-9]$/);
368368
});
369369

370370
it.each(times(100))(
@@ -373,7 +373,7 @@ describe('random', () => {
373373
const actual = faker.random.numeric(length);
374374

375375
expect(actual).toHaveLength(length);
376-
expect(actual).toMatch(/^[1-9][0-9]*$/);
376+
expect(actual).toMatch(/^[0-9]*$/);
377377
}
378378
);
379379

@@ -394,7 +394,7 @@ describe('random', () => {
394394

395395
expect(actual).toBeTypeOf('string');
396396
expect(actual).toHaveLength(1000);
397-
expect(actual).toMatch(/^[1-9][0-9]+$/);
397+
expect(actual).toMatch(/^[0-9]+$/);
398398
});
399399

400400
it('should allow leading zeros via option', () => {

test/string.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ describe('string', () => {
5959
.itRepeated('with length parameter', 5, 5)
6060
.it('with length', { length: 6 })
6161
.it('with length range', { length: { min: 10, max: 20 } })
62-
.it('with allowLeadingZeros', { allowLeadingZeros: true })
62+
.it('with allowLeadingZeros', { allowLeadingZeros: false })
6363
.it('with exclude', { exclude: '12345' })
6464
.it('with length, allowLeadingZeros and exclude', {
6565
length: 7,
@@ -382,7 +382,7 @@ describe('string', () => {
382382
const actual = faker.string.numeric();
383383

384384
expect(actual).toHaveLength(1);
385-
expect(actual).toMatch(/^[1-9]$/);
385+
expect(actual).toMatch(/^[0-9]$/);
386386
});
387387

388388
it.each(times(100))(
@@ -391,7 +391,7 @@ describe('string', () => {
391391
const actual = faker.string.numeric(length);
392392

393393
expect(actual).toHaveLength(length);
394-
expect(actual).toMatch(/^[1-9][0-9]*$/);
394+
expect(actual).toMatch(/^[0-9]*$/);
395395
}
396396
);
397397

0 commit comments

Comments
 (0)