Skip to content

Commit 6360d94

Browse files
authored
Merge branch 'next' into remove-deprecated-date-methods
2 parents cf53240 + 2b15f2e commit 6360d94

File tree

4 files changed

+97
-14
lines changed

4 files changed

+97
-14
lines changed

docs/guide/upgrading_v9/2563.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
### Stricter checking for function signature passed to `faker.helpers.multiple` method
2+
3+
The `faker.helpers.multiple` method takes a function reference as its first parameter. Previously you may have written code like this to generate multiple values.
4+
5+
```ts
6+
faker.helpers.multiple(faker.date.past, { count: 2 });
7+
```
8+
9+
However this code has a bug - `faker.helpers.multiple` passes the loop index as the second parameter to the method, which in this case would set the `refDate` of the `faker.date.past()` call to 0, making all dates before 1970.
10+
11+
Instead you should generally use a lambda function like
12+
13+
```ts
14+
faker.helpers.multiple(() => faker.date.past(), { count: 2 });
15+
```
16+
17+
to get the desired behavior. In v9.0, we use stricter type-checking in Typescript to detect when a function is called which is not compatible with `(v: unknown, index: number)` which can cause compile-time errors in places where previously there were potential runtime errors.
18+
19+
**Bad**
20+
21+
```ts
22+
faker.helpers.multiple(faker.person.firstName, ...); //
23+
// In Typescript, this is now a compile time error
24+
// Argument of type '(sex?: "female" | "male" | undefined) => string'
25+
// is not assignable to parameter of type '(v: unknown, index: number) => unknown'.
26+
```
27+
28+
**Good**
29+
30+
```ts
31+
faker.helpers.multiple(() => faker.person.firstName(), ...); //
32+
```
33+
34+
The new types also allow for easier use-cases where the index is part of the generated data e.g. as id.
35+
36+
```ts
37+
faker.helpers.multiple((_, index) => ({ id: index, ...}), ...); // [{id: 0, ...}, ...]
38+
```

src/modules/helpers/index.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1129,17 +1129,19 @@ export class SimpleHelpersModule extends SimpleModuleBase {
11291129
* @template TResult The type of elements.
11301130
*
11311131
* @param method The method used to generate the values.
1132+
* The method will be called with `(_, index)`, to allow using the index in the generated value e.g. as id.
11321133
* @param options The optional options object.
11331134
* @param options.count The number or range of elements to generate. Defaults to `3`.
11341135
*
11351136
* @example
1136-
* faker.helpers.multiple(faker.person.firstName) // [ 'Aniya', 'Norval', 'Dallin' ]
1137-
* faker.helpers.multiple(faker.person.firstName, { count: 3 }) // [ 'Santos', 'Lavinia', 'Lavinia' ]
1137+
* faker.helpers.multiple(() => faker.person.firstName()) // [ 'Aniya', 'Norval', 'Dallin' ]
1138+
* faker.helpers.multiple(() => faker.person.firstName(), { count: 3 }) // [ 'Santos', 'Lavinia', 'Lavinia' ]
1139+
* faker.helpers.multiple((_, i) => `${faker.color.human()}-${i + 1}`) // [ 'orange-1', 'orchid-2', 'sky blue-3' ]
11381140
*
11391141
* @since 8.0.0
11401142
*/
11411143
multiple<const TResult>(
1142-
method: () => TResult,
1144+
method: (v: unknown, index: number) => TResult,
11431145
options: {
11441146
/**
11451147
* The number or range of elements to generate.

test/modules/__snapshots__/helpers.spec.ts.snap

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,14 @@ exports[`helpers > 42 > multiple > with method and count range 1`] = `
9898
]
9999
`;
100100

101+
exports[`helpers > 42 > multiple > with method using index 1`] = `
102+
[
103+
0,
104+
3,
105+
6,
106+
]
107+
`;
108+
101109
exports[`helpers > 42 > multiple > with only method 1`] = `
102110
[
103111
3373557479352566,
@@ -330,6 +338,14 @@ exports[`helpers > 1211 > multiple > with method and count range 1`] = `
330338
]
331339
`;
332340

341+
exports[`helpers > 1211 > multiple > with method using index 1`] = `
342+
[
343+
0,
344+
3,
345+
6,
346+
]
347+
`;
348+
333349
exports[`helpers > 1211 > multiple > with only method 1`] = `
334350
[
335351
8363366038243348,
@@ -544,6 +560,14 @@ exports[`helpers > 1337 > multiple > with method and count range 1`] = `
544560
]
545561
`;
546562
563+
exports[`helpers > 1337 > multiple > with method using index 1`] = `
564+
[
565+
0,
566+
3,
567+
6,
568+
]
569+
`;
570+
547571
exports[`helpers > 1337 > multiple > with only method 1`] = `
548572
[
549573
2360108457524098,

test/modules/helpers.spec.ts

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -173,11 +173,14 @@ describe('helpers', () => {
173173
});
174174

175175
t.describe('multiple', (t) => {
176-
t.it('with only method', faker.number.int)
177-
.it('with method and count', faker.number.int, { count: 5 })
178-
.it('with method and count range', faker.number.int, {
176+
t.it('with only method', () => faker.number.int())
177+
.it('with method and count', () => faker.number.int(), {
178+
count: 5,
179+
})
180+
.it('with method and count range', () => faker.number.int(), {
179181
count: { min: 1, max: 10 },
180-
});
182+
})
183+
.it('with method using index', (_, i) => i * 3);
181184
});
182185
});
183186

@@ -1144,30 +1147,46 @@ describe('helpers', () => {
11441147

11451148
describe('multiple()', () => {
11461149
it('should generate values from the function with a default length of 3', () => {
1147-
const result = faker.helpers.multiple(faker.person.firstName);
1150+
const result = faker.helpers.multiple(() => faker.person.firstName());
11481151
expect(result).toBeTypeOf('object');
11491152
expect(Array.isArray(result)).toBe(true);
11501153
expect(result.length).toBe(3);
11511154
});
11521155

11531156
it('should generate the given amount of values from the function', () => {
1154-
const result = faker.helpers.multiple(faker.person.firstName, {
1155-
count: 5,
1156-
});
1157+
const result = faker.helpers.multiple(
1158+
() => faker.person.firstName(),
1159+
{
1160+
count: 5,
1161+
}
1162+
);
11571163
expect(result).toBeTypeOf('object');
11581164
expect(Array.isArray(result)).toBe(true);
11591165
expect(result.length).toBe(5);
11601166
});
11611167

11621168
it('should generate a ranged number of values from the function', () => {
1163-
const result = faker.helpers.multiple(faker.person.firstName, {
1164-
count: { min: 1, max: 10 },
1165-
});
1169+
const result = faker.helpers.multiple(
1170+
() => faker.person.firstName(),
1171+
{
1172+
count: { min: 1, max: 10 },
1173+
}
1174+
);
11661175
expect(result).toBeTypeOf('object');
11671176
expect(Array.isArray(result)).toBe(true);
11681177
expect(result.length).toBeGreaterThanOrEqual(1);
11691178
expect(result.length).toBeLessThanOrEqual(10);
11701179
});
1180+
1181+
it('should generate values using index of created value', () => {
1182+
const result = faker.helpers.multiple((_, i) => i * 2, {
1183+
count: 3,
1184+
});
1185+
expect(result).toBeTypeOf('object');
1186+
expect(Array.isArray(result)).toBe(true);
1187+
expect(result.length).toBe(3);
1188+
expect(result).toStrictEqual([0, 2, 4]);
1189+
});
11711190
});
11721191
}
11731192
);

0 commit comments

Comments
 (0)