diff --git a/src/system.ts b/src/system.ts index c5b052c0a80..7b285084934 100644 --- a/src/system.ts +++ b/src/system.ts @@ -1,4 +1,5 @@ import type { Faker } from '.'; +import { FakerError } from '.'; const commonFileTypes = ['video', 'audio', 'image', 'text', 'application']; @@ -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'); + + 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}`; + } + } } /** diff --git a/test/system.spec.ts b/test/system.spec.ts index 26b5730fbdb..298c99f621d 100644 --- a/test/system.spec.ts +++ b/test/system.spec.ts @@ -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 = [ { @@ -98,10 +98,13 @@ describe('system', () => { 'htm', 'html', 'jpeg', + 'm1v', 'm2a', 'm2v', 'mp2', 'mp3', + 'm2v', + 'mp2a', 'mp4', 'mp4v', 'mpeg', @@ -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') + ); }); });