From 968134c398a11b698b489a492179080aa7ca8c73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leyla=20J=C3=A4hnig?= Date: Tue, 19 Jul 2022 11:46:07 +0200 Subject: [PATCH] feat(system.fileName): file extension count (#1101) Co-authored-by: Piotr Kuczynski --- src/modules/system/index.ts | 32 +++++++++++++++++++++++---- test/system.spec.ts | 44 +++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 4 deletions(-) diff --git a/src/modules/system/index.ts b/src/modules/system/index.ts index 200deafe49c..77be5a8267c 100644 --- a/src/modules/system/index.ts +++ b/src/modules/system/index.ts @@ -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}`; } /** @@ -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()}`; } diff --git a/test/system.spec.ts b/test/system.spec.ts index 559d7bed7c2..f795abfbaaa 100644 --- a/test/system.spec.ts +++ b/test/system.spec.ts @@ -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; @@ -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', @@ -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()', () => {