From dc569ba50e05c77c0abe3ce2d8e0c3c45554805c Mon Sep 17 00:00:00 2001 From: Piotr Kuczynski Date: Tue, 25 Jan 2022 21:45:36 +0100 Subject: [PATCH 1/5] fix: remove duplicated extension in system.filePath --- src/system.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/system.ts b/src/system.ts index c5b052c0a80..2feef6bab23 100644 --- a/src/system.ts +++ b/src/system.ts @@ -177,8 +177,7 @@ export class System { * @example * faker.system.filePath() // '/usr/local/src/money.dotx' */ - // TODO @prisis 2022-01-25: add a parameter to have the possibility to have one or two ext on file. - filePath(): string { + filePath() { return `${this.directoryPath()}/${this.fileName()}`; } From d706f1863473ceafbaaeefae529f44c0ebea7345 Mon Sep 17 00:00:00 2001 From: Piotr Kuczynski Date: Tue, 1 Feb 2022 22:07:04 +0100 Subject: [PATCH 2/5] chore: fix tests --- src/system.ts | 3 ++- test/system.spec.ts | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/system.ts b/src/system.ts index 2feef6bab23..c5b052c0a80 100644 --- a/src/system.ts +++ b/src/system.ts @@ -177,7 +177,8 @@ export class System { * @example * faker.system.filePath() // '/usr/local/src/money.dotx' */ - filePath() { + // 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()}`; } diff --git a/test/system.spec.ts b/test/system.spec.ts index 26b5730fbdb..8932e09575b 100644 --- a/test/system.spec.ts +++ b/test/system.spec.ts @@ -102,6 +102,7 @@ describe('system', () => { 'm2v', 'mp2', 'mp3', + 'm2v', 'mp4', 'mp4v', 'mpeg', From a0f790550e9c7dd09bb465106abfbcbd42272fa0 Mon Sep 17 00:00:00 2001 From: Piotr Kuczynski Date: Tue, 1 Feb 2022 22:49:45 +0100 Subject: [PATCH 3/5] feat: add ability to define number of generated extensions in system.filePath --- src/system.ts | 26 +++++++++++++++++++++++--- test/system.spec.ts | 26 ++++++++++++++++++++++---- 2 files changed, 45 insertions(+), 7 deletions(-) diff --git a/src/system.ts b/src/system.ts index c5b052c0a80..e6952c00bcd 100644 --- a/src/system.ts +++ b/src/system.ts @@ -174,12 +174,32 @@ export class System { /** * Returns a file path. * + * @param ext Defines number of generated extensions. Defaults to `1`. + * * @example * faker.system.filePath() // '/usr/local/src/money.dotx' + * faker.system.filePath(0) // '/usr/local/src/money' + * faker.system.filePath(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(ext = 1): string { + const path = `${this.directoryPath()}/${this.fileName()}`; + + switch (true) { + 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 8932e09575b..77ff82c3c57 100644 --- a/test/system.spec.ts +++ b/test/system.spec.ts @@ -98,11 +98,13 @@ describe('system', () => { 'htm', 'html', 'jpeg', + 'm1v', 'm2a', 'm2v', 'mp2', 'mp3', 'm2v', + 'mp2a', 'mp4', 'mp4v', 'mpeg', @@ -235,16 +237,32 @@ 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(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); }); }); From 4edadf2461f441f6ab8156cde67f5a075036ee51 Mon Sep 17 00:00:00 2001 From: Piotr Kuczynski Date: Wed, 6 Apr 2022 13:54:56 +0200 Subject: [PATCH 4/5] chore: ext as option --- src/system.ts | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/system.ts b/src/system.ts index e6952c00bcd..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,17 +175,24 @@ export class System { /** * Returns a file path. * - * @param ext Defines number of generated extensions. Defaults to `1`. + * @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(0) // '/usr/local/src/money' - * faker.system.filePath(2) // '/usr/local/src/money.dotx.zip' + * 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' */ - filePath(ext = 1): string { + 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('.')); From 1cddd1b77b0b1c440ac154d38755d9926d9065fe Mon Sep 17 00:00:00 2001 From: Piotr Kuczynski Date: Wed, 6 Apr 2022 13:58:16 +0200 Subject: [PATCH 5/5] chore: tests --- test/system.spec.ts | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/test/system.spec.ts b/test/system.spec.ts index 77ff82c3c57..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 = [ { @@ -251,7 +251,7 @@ describe('system', () => { }); it('should not have extension when ext=0', () => { - const filePath = faker.system.filePath(0); + const filePath = faker.system.filePath({ ext: 0 }); const file = filePath.split('/').pop(); expect(file.includes('.')).toBeFalsy(); @@ -259,11 +259,17 @@ describe('system', () => { it('should have multiple extension when ext > 1', () => { const ext = 3; - const filePath = faker.system.filePath(ext); + 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') + ); + }); }); describe('mimeType()', () => {