Skip to content

Commit

Permalink
feat(system.fileName): file extension count (#1101)
Browse files Browse the repository at this point in the history
Co-authored-by: Piotr Kuczynski <piotr.kuczynski@gmail.com>
  • Loading branch information
xDivisionByZerox and pkuczynski committed Jul 19, 2022
1 parent 316f61f commit 968134c
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 4 deletions.
32 changes: 28 additions & 4 deletions src/modules/system/index.ts
Expand Up @@ -31,13 +31,37 @@ export class System {
/**
* Returns a random file name with extension.
*
* @param options An options object.
* @param options.extensionCount Define how many extensions the file name should have. A negative number will be treated as `0`. Defaults to `1`.
*
* @example
* faker.system.fileName() // 'self_enabling_accountability_toys.kpt'
* faker.system.fileName({ extensionCount: 2 }) // 'bike_table.res.vcs'
*/
fileName(): string {
const str = this.faker.random.words().toLowerCase().replace(/\W/g, '_');
fileName(
options: {
/**
* Define how many extensions the file name should have. A negative number will be treated as `0`. Defaults to `1`.
*/
extensionCount?: number;
} = {}
): string {
const { extensionCount = 1 } = options;

const baseName = this.faker.random
.words()
.toLowerCase()
.replace(/\W/g, '_');

if (extensionCount <= 0) {
return baseName;
}

const extensionsStr = Array.from({ length: extensionCount })
.map(() => this.fileExt())
.join('.');

return `${str}.${this.fileExt()}`;
return `${baseName}.${extensionsStr}`;
}

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

return `${str}.${ext || this.commonFileExt()}`;
}
Expand Down
44 changes: 44 additions & 0 deletions test/system.spec.ts
Expand Up @@ -2,6 +2,7 @@ import validator from 'validator';
import { afterEach, describe, expect, it } from 'vitest';
import { faker } from '../src';
import { seededRuns } from './support/seededRuns';
import { times } from './support/times';

const NON_SEEDED_BASED_RUN = 5;

Expand Down Expand Up @@ -48,15 +49,23 @@ describe('system', () => {
'gif',
'htm',
'html',
'jpe',
'jpeg',
'jpg',
'm1v',
'm2a',
'm2v',
'm3a',
'mp2',
'mp2a',
'mp3',
'mp4',
'mp4v',
'mpe',
'mpeg',
'mpg',
'mpg4',
'mpga',
'pdf',
'png',
'shtml',
Expand Down Expand Up @@ -180,6 +189,41 @@ describe('system', () => {
'generated fileNames should have an extension'
).toContain('.');
});

it('should return filenames with 1 ext per default', () => {
const fileName = faker.system.fileName();
const parts = fileName.split('.');

expect(parts).length(2);
});

it('should return filenames without an extension when extensionCount is 0', () => {
const fileName = faker.system.fileName({
extensionCount: 0,
});

expect(fileName).not.toContain('.');
});

it('should return filenames without an extension when extensionCount is negative', () => {
const fileName = faker.system.fileName({
extensionCount: -1,
});

expect(fileName).not.toContain('.');
});

it.each(times(10))(
'should return filenames with %s extensions',
(extensionCount) => {
const fileName = faker.system.fileName({
extensionCount,
});
const parts = fileName.split('.');

expect(parts).length(extensionCount + 1);
}
);
});

describe('filePath()', () => {
Expand Down

0 comments on commit 968134c

Please sign in to comment.