Skip to content

Commit 7c5a3b4

Browse files
committed
feat(helpers): fake from array
1 parent 6e1009a commit 7c5a3b4

File tree

3 files changed

+154
-0
lines changed

3 files changed

+154
-0
lines changed

src/modules/helpers/index.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -690,4 +690,33 @@ export class HelpersModule {
690690
currentIterations: 0,
691691
});
692692
}
693+
694+
/**
695+
* Generates an array containing values returned by the given method.
696+
*
697+
* @param method The method used to generate the values.
698+
* @param options The optional options object.
699+
* @param options.count The number of elements to generate. Defaults to `3`.
700+
* @param options.unique Whether the generated elements should be unique per array. Defaults to `false`.
701+
*
702+
* @example
703+
* faker.helpers.multiple(faker.person.firstName) // [ 'Aniya', 'Norval', 'Dallin' ]
704+
* faker.helpers.multiple(faker.person.firstName, { count: 3 }) // [ 'Santos', 'Lavinia', 'Lavinia' ]
705+
* faker.helpers.multiple(faker.person.firstName, { count: 3, unique: true }) // [ 'Santos', 'Nedra', 'Lavinia' ]
706+
*
707+
* @since 8.0.0
708+
*/
709+
multiple<T>(
710+
method: () => T,
711+
options: { count?: number; unique?: boolean } = {}
712+
): T[] {
713+
// TODO @ST-DDT 2022-11-09: Add support for count ranges
714+
const { count = 3, unique = false } = options;
715+
716+
if (unique) {
717+
// TODO @ST-DDT 2022-11-09: Fix unique method signature
718+
}
719+
720+
return Array.from({ length: count }, method);
721+
}
693722
}

test/__snapshots__/helpers.spec.ts.snap

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,34 @@ exports[`helpers > 42 > maybe > with only value 1`] = `"Hello World!"`;
3737

3838
exports[`helpers > 42 > maybe > with value and probability 1`] = `undefined`;
3939

40+
exports[`helpers > 42 > multiple > with method and count 1`] = `
41+
[
42+
37454,
43+
79654,
44+
95071,
45+
18343,
46+
73199,
47+
]
48+
`;
49+
50+
exports[`helpers > 42 > multiple > with method, count and unique 1`] = `
51+
[
52+
37454,
53+
79654,
54+
95071,
55+
18343,
56+
73199,
57+
]
58+
`;
59+
60+
exports[`helpers > 42 > multiple > with only method 1`] = `
61+
[
62+
37454,
63+
79654,
64+
95071,
65+
]
66+
`;
67+
4068
exports[`helpers > 42 > mustache > template with method 1`] = `"Hello John!"`;
4169

4270
exports[`helpers > 42 > mustache > template with string 1`] = `"Hello John!"`;
@@ -189,6 +217,34 @@ exports[`helpers > 1211 > maybe > with only value 1`] = `undefined`;
189217

190218
exports[`helpers > 1211 > maybe > with value and probability 1`] = `undefined`;
191219

220+
exports[`helpers > 1211 > multiple > with method and count 1`] = `
221+
[
222+
92852,
223+
45901,
224+
89347,
225+
77826,
226+
22557,
227+
]
228+
`;
229+
230+
exports[`helpers > 1211 > multiple > with method, count and unique 1`] = `
231+
[
232+
92852,
233+
45901,
234+
89347,
235+
77826,
236+
22557,
237+
]
238+
`;
239+
240+
exports[`helpers > 1211 > multiple > with only method 1`] = `
241+
[
242+
92852,
243+
45901,
244+
89347,
245+
]
246+
`;
247+
192248
exports[`helpers > 1211 > mustache > template with method 1`] = `"Hello John!"`;
193249

194250
exports[`helpers > 1211 > mustache > template with string 1`] = `"Hello John!"`;
@@ -331,6 +387,34 @@ exports[`helpers > 1337 > maybe > with only value 1`] = `"Hello World!"`;
331387

332388
exports[`helpers > 1337 > maybe > with value and probability 1`] = `undefined`;
333389

390+
exports[`helpers > 1337 > multiple > with method and count 1`] = `
391+
[
392+
26202,
393+
56052,
394+
15868,
395+
21258,
396+
27812,
397+
]
398+
`;
399+
400+
exports[`helpers > 1337 > multiple > with method, count and unique 1`] = `
401+
[
402+
26202,
403+
56052,
404+
15868,
405+
21258,
406+
27812,
407+
]
408+
`;
409+
410+
exports[`helpers > 1337 > multiple > with only method 1`] = `
411+
[
412+
26202,
413+
56052,
414+
15868,
415+
]
416+
`;
417+
334418
exports[`helpers > 1337 > mustache > template with method 1`] = `"Hello John!"`;
335419

336420
exports[`helpers > 1337 > mustache > template with string 1`] = `"Hello John!"`;

test/helpers.spec.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,15 @@ describe('helpers', () => {
112112
.it('with () => number', faker.datatype.number)
113113
.it('with () => number and args', faker.datatype.number, [50]);
114114
});
115+
116+
t.describe('multiple', (t) => {
117+
t.it('with only method', faker.datatype.number)
118+
.it('with method and count', faker.datatype.number, { count: 5 })
119+
.it('with method, count and unique', faker.datatype.number, {
120+
count: 5,
121+
unique: true,
122+
});
123+
});
115124
});
116125

117126
describe(`random seeded tests for seed ${faker.seed()}`, () => {
@@ -844,5 +853,37 @@ Try adjusting maxTime or maxRetries parameters for faker.helpers.unique().`)
844853
expect(store).toEqual({ 'with conflict: 0': 'with conflict: 0' });
845854
});
846855
});
856+
857+
describe('multiple()', () => {
858+
it('should generate values from the function', () => {
859+
const result = faker.helpers.multiple(faker.person.firstName);
860+
expect(result).toBeTypeOf('object');
861+
expect(Array.isArray(result)).toBe(true);
862+
expect(result.length).toBe(3);
863+
});
864+
865+
it('should generate the given amount of values from the function', () => {
866+
const result = faker.helpers.multiple(faker.person.firstName, {
867+
count: 5,
868+
});
869+
expect(result).toBeTypeOf('object');
870+
expect(Array.isArray(result)).toBe(true);
871+
expect(result.length).toBe(5);
872+
});
873+
874+
it.todo(
875+
'should generate the given amount of unique values from the function',
876+
() => {
877+
const result = faker.helpers.multiple(faker.person.firstName, {
878+
count: 10,
879+
unique: true,
880+
});
881+
expect(result).toBeTypeOf('object');
882+
expect(Array.isArray(result)).toBe(true);
883+
expect(result.length).toBe(10);
884+
expect(result).not.toContainDuplicates();
885+
}
886+
);
887+
});
847888
});
848889
});

0 commit comments

Comments
 (0)