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: add ability to define number of generated extensions in system.filePath #395

Closed
wants to merge 5 commits into from
Closed
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
36 changes: 32 additions & 4 deletions src/system.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Faker } from '.';
import { FakerError } from '.';

const commonFileTypes = ['video', 'audio', 'image', 'text', 'application'];

Expand Down Expand Up @@ -174,12 +175,39 @@ export class System {
/**
* Returns a file path.
*
* @param options Options object.
* @param options.ext Number of generated extensions. Defaults to `1`.
*
* @throws When `options.ext < 0`
*
* @example
* faker.system.filePath() // '/usr/local/src/money.dotx'
* faker.system.filePath() // '/usr/local/src/money.dotx'
* faker.system.filePath({ ext: 0 }) // '/usr/local/src/money'
* faker.system.filePath({ ext: 2 }) // '/usr/local/src/money.dotx.zip'
*/
// TODO @prisis 2022-01-25: add a parameter to have the possibility to have one or two ext on file.
filePath(): string {
return `${this.directoryPath()}/${this.fileName()}`;
filePath(options?: { ext?: number }): string {
const ext = options.ext || 1;
const path = `${this.directoryPath()}/${this.fileName()}`;

switch (true) {
case ext < 0:
throw new FakerError('Options.ext shall not have negative value');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
throw new FakerError('Options.ext shall not have negative value');
throw new FakerError('Options.ext should not be negative');

I think this phrasing would be more natural


case ext === 0:
return path.slice(0, path.lastIndexOf('.'));

case ext === 1:
return path;

default: {
const extensions = new Array(ext - 1)
.fill('')
.map(() => this.fileExt())
.join('.');

return `${path}.${extensions}`;
}
}
}

/**
Expand Down
35 changes: 30 additions & 5 deletions test/system.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import validator from 'validator';
import { afterEach, describe, expect, it } from 'vitest';
import { faker } from '../src';
import { faker, FakerError } from '../src';

const seededRuns = [
{
Expand Down Expand Up @@ -98,10 +98,13 @@ describe('system', () => {
'htm',
'html',
'jpeg',
'm1v',
'm2a',
'm2v',
'mp2',
'mp3',
'm2v',
'mp2a',
'mp4',
'mp4v',
'mpeg',
Expand Down Expand Up @@ -234,16 +237,38 @@ describe('system', () => {
describe('filePath()', () => {
it('should return unix fs file full path', () => {
const filePath = faker.system.filePath();
const parts = filePath.split('/');
const file = filePath.split('/').pop();

expect(
filePath.startsWith('/'),
'generated filePath should start with /'
).toBeTruthy();

expect(
parts[parts.length - 1],
'generated filePath should have a file extension'
).toMatch(/^\w+\.\w+$/);
file.includes('.'),
'generated filePath should have extension'
).toBeTruthy();
});

it('should not have extension when ext=0', () => {
const filePath = faker.system.filePath({ ext: 0 });
const file = filePath.split('/').pop();

expect(file.includes('.')).toBeFalsy();
});

it('should have multiple extension when ext > 1', () => {
const ext = 3;
const filePath = faker.system.filePath({ ext });
const file = filePath.split('/').pop();

expect(file.split('.').length).toBe(ext + 1);
});

it('should throw error ext < 0', () => {
expect(() => faker.system.filePath({ ext: -1 })).toThrow(
new FakerError('Options.ext shall not have negative value')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
new FakerError('Options.ext shall not have negative value')
new FakerError('Options.ext should not be negative')

);
});
});

Expand Down