Skip to content

Commit 968134c

Browse files
feat(system.fileName): file extension count (#1101)
Co-authored-by: Piotr Kuczynski <piotr.kuczynski@gmail.com>
1 parent 316f61f commit 968134c

2 files changed

Lines changed: 72 additions & 4 deletions

File tree

src/modules/system/index.ts

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,37 @@ export class System {
3131
/**
3232
* Returns a random file name with extension.
3333
*
34+
* @param options An options object.
35+
* @param options.extensionCount Define how many extensions the file name should have. A negative number will be treated as `0`. Defaults to `1`.
36+
*
3437
* @example
3538
* faker.system.fileName() // 'self_enabling_accountability_toys.kpt'
39+
* faker.system.fileName({ extensionCount: 2 }) // 'bike_table.res.vcs'
3640
*/
37-
fileName(): string {
38-
const str = this.faker.random.words().toLowerCase().replace(/\W/g, '_');
41+
fileName(
42+
options: {
43+
/**
44+
* Define how many extensions the file name should have. A negative number will be treated as `0`. Defaults to `1`.
45+
*/
46+
extensionCount?: number;
47+
} = {}
48+
): string {
49+
const { extensionCount = 1 } = options;
50+
51+
const baseName = this.faker.random
52+
.words()
53+
.toLowerCase()
54+
.replace(/\W/g, '_');
55+
56+
if (extensionCount <= 0) {
57+
return baseName;
58+
}
59+
60+
const extensionsStr = Array.from({ length: extensionCount })
61+
.map(() => this.fileExt())
62+
.join('.');
3963

40-
return `${str}.${this.fileExt()}`;
64+
return `${baseName}.${extensionsStr}`;
4165
}
4266

4367
/**
@@ -49,7 +73,7 @@ export class System {
4973
* faker.system.commonFileName('txt') // 'global_borders_wyoming.txt'
5074
*/
5175
commonFileName(ext?: string): string {
52-
const str = this.faker.random.words().toLowerCase().replace(/\W/g, '_');
76+
const str = this.fileName({ extensionCount: 0 });
5377

5478
return `${str}.${ext || this.commonFileExt()}`;
5579
}

test/system.spec.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import validator from 'validator';
22
import { afterEach, describe, expect, it } from 'vitest';
33
import { faker } from '../src';
44
import { seededRuns } from './support/seededRuns';
5+
import { times } from './support/times';
56

67
const NON_SEEDED_BASED_RUN = 5;
78

@@ -48,15 +49,23 @@ describe('system', () => {
4849
'gif',
4950
'htm',
5051
'html',
52+
'jpe',
5153
'jpeg',
54+
'jpg',
55+
'm1v',
5256
'm2a',
5357
'm2v',
58+
'm3a',
5459
'mp2',
60+
'mp2a',
5561
'mp3',
5662
'mp4',
5763
'mp4v',
64+
'mpe',
5865
'mpeg',
5966
'mpg',
67+
'mpg4',
68+
'mpga',
6069
'pdf',
6170
'png',
6271
'shtml',
@@ -180,6 +189,41 @@ describe('system', () => {
180189
'generated fileNames should have an extension'
181190
).toContain('.');
182191
});
192+
193+
it('should return filenames with 1 ext per default', () => {
194+
const fileName = faker.system.fileName();
195+
const parts = fileName.split('.');
196+
197+
expect(parts).length(2);
198+
});
199+
200+
it('should return filenames without an extension when extensionCount is 0', () => {
201+
const fileName = faker.system.fileName({
202+
extensionCount: 0,
203+
});
204+
205+
expect(fileName).not.toContain('.');
206+
});
207+
208+
it('should return filenames without an extension when extensionCount is negative', () => {
209+
const fileName = faker.system.fileName({
210+
extensionCount: -1,
211+
});
212+
213+
expect(fileName).not.toContain('.');
214+
});
215+
216+
it.each(times(10))(
217+
'should return filenames with %s extensions',
218+
(extensionCount) => {
219+
const fileName = faker.system.fileName({
220+
extensionCount,
221+
});
222+
const parts = fileName.split('.');
223+
224+
expect(parts).length(extensionCount + 1);
225+
}
226+
);
183227
});
184228

185229
describe('filePath()', () => {

0 commit comments

Comments
 (0)