Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(system.fileName): file extension count #1101

Merged
merged 12 commits into from Jul 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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