From 4191c656205327f0b5398c8e844664caa86ee8ce Mon Sep 17 00:00:00 2001 From: xDivisionByZerox Date: Wed, 22 Jun 2022 14:18:39 +0200 Subject: [PATCH 01/12] feat(system.fileName): extensionCount option Co-authored-by: Piotr Kuczynski Original idea and implementation from Piotr Kuczynski --- src/modules/system/index.ts | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/src/modules/system/index.ts b/src/modules/system/index.ts index 200deafe49c..565d80337ee 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' */ - 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 { + if (options == null) { + options = { + extensionCount: 1, + }; + } + const baseName = this.faker.random + .words() + .toLowerCase() + .replace(/\W/g, '_'); + + const extCount = options.extensionCount; + if (options.extensionCount <= 0) { + return baseName; + } + + const extensionsStr = Array.from({ length: extCount }) + .map(() => this.fileExt()) + .join('.'); - return `${str}.${this.fileExt()}`; + return `${baseName}.${extensionsStr}`; } /** From e5a354bb43bd6aa153b7eda5c23e03c2f609dbe6 Mon Sep 17 00:00:00 2001 From: xDivisionByZerox Date: Wed, 22 Jun 2022 14:19:20 +0200 Subject: [PATCH 02/12] refactor(system.commonFileName): remove code duplication --- src/modules/system/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/modules/system/index.ts b/src/modules/system/index.ts index 565d80337ee..d09cef6eb46 100644 --- a/src/modules/system/index.ts +++ b/src/modules/system/index.ts @@ -73,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()}`; } From 33ffb5d90dbd187d0d394d20dcc795867cb3ffb8 Mon Sep 17 00:00:00 2001 From: xDivisionByZerox Date: Wed, 22 Jun 2022 14:30:15 +0200 Subject: [PATCH 03/12] docs(system.fileName): example for extensionCount usage --- src/modules/system/index.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/modules/system/index.ts b/src/modules/system/index.ts index d09cef6eb46..aac3f416894 100644 --- a/src/modules/system/index.ts +++ b/src/modules/system/index.ts @@ -35,6 +35,7 @@ export class System { * @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(options?: { /** From baa0bd2b28729e8157c55865f31af7446a469a25 Mon Sep 17 00:00:00 2001 From: xDivisionByZerox Date: Wed, 22 Jun 2022 16:26:18 +0200 Subject: [PATCH 04/12] fix: default extensionCount option --- src/modules/system/index.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/modules/system/index.ts b/src/modules/system/index.ts index aac3f416894..7defdb9317d 100644 --- a/src/modules/system/index.ts +++ b/src/modules/system/index.ts @@ -48,17 +48,19 @@ export class System { extensionCount: 1, }; } + + const extensionCount = options.extensionCount ?? 1; + const baseName = this.faker.random .words() .toLowerCase() .replace(/\W/g, '_'); - const extCount = options.extensionCount; - if (options.extensionCount <= 0) { + if (extensionCount <= 0) { return baseName; } - const extensionsStr = Array.from({ length: extCount }) + const extensionsStr = Array.from({ length: extensionCount }) .map(() => this.fileExt()) .join('.'); From 2692e6994747d1038ec2c85dc7e21a962edc8de8 Mon Sep 17 00:00:00 2001 From: xDivisionByZerox Date: Wed, 22 Jun 2022 21:30:54 +0200 Subject: [PATCH 05/12] test(system.fileName): add cases for options object --- test/system.spec.ts | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/test/system.spec.ts b/test/system.spec.ts index 559d7bed7c2..cbde00fd72f 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; @@ -180,6 +181,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 a extension when extensionCount is 0', () => { + const fileName = faker.system.fileName({ + extensionCount: 0, + }); + + expect(fileName).not.toContain('.'); + }); + + it('should return filenames without a extension when extensionCount is negative', () => { + const fileName = faker.system.fileName({ + extensionCount: -1, + }); + + expect(fileName).not.toContain('.'); + }); + + it.each(times(10))( + 'should return filenames with a %s extensions', + (extCount) => { + const fileName = faker.system.fileName({ + extensionCount: extCount, + }); + const parts = fileName.split('.'); + + expect(parts).length(extCount + 1); + } + ); }); describe('filePath()', () => { From 390fe6f7c8884b7a92f985b3c3de70a9963c4368 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leyla=20J=C3=A4hnig?= Date: Thu, 23 Jun 2022 15:25:04 +0200 Subject: [PATCH 06/12] docs(system.fileName): space JSDOC params Co-authored-by: ST-DDT --- src/modules/system/index.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/modules/system/index.ts b/src/modules/system/index.ts index 7defdb9317d..5bfaed744bb 100644 --- a/src/modules/system/index.ts +++ b/src/modules/system/index.ts @@ -33,6 +33,7 @@ export class System { * * @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' From 5de74236e3e57c980bf21d6f840b904b2387d21d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leyla=20J=C3=A4hnig?= Date: Thu, 23 Jun 2022 15:32:10 +0200 Subject: [PATCH 07/12] refactor(system.fileName): default agument to empty object Co-authored-by: ST-DDT --- src/modules/system/index.ts | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/modules/system/index.ts b/src/modules/system/index.ts index 5bfaed744bb..72f712677fd 100644 --- a/src/modules/system/index.ts +++ b/src/modules/system/index.ts @@ -38,17 +38,12 @@ export class System { * faker.system.fileName() // 'self_enabling_accountability_toys.kpt' * faker.system.fileName({ extensionCount: 2 }) // 'bike_table.res.vcs' */ - fileName(options?: { + 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 { - if (options == null) { - options = { - extensionCount: 1, - }; - } + } = {}): string { const extensionCount = options.extensionCount ?? 1; From 3ccd2a248abd3807e23a28299f72ed53cf2c4a36 Mon Sep 17 00:00:00 2001 From: xDivisionByZerox Date: Thu, 23 Jun 2022 15:33:15 +0200 Subject: [PATCH 08/12] chore: code formatting --- src/modules/system/index.ts | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/modules/system/index.ts b/src/modules/system/index.ts index 72f712677fd..6c1a74e176a 100644 --- a/src/modules/system/index.ts +++ b/src/modules/system/index.ts @@ -38,13 +38,14 @@ export class System { * faker.system.fileName() // 'self_enabling_accountability_toys.kpt' * faker.system.fileName({ extensionCount: 2 }) // 'bike_table.res.vcs' */ - 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 { - + 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 = options.extensionCount ?? 1; const baseName = this.faker.random From c84908295b5a0321204a521ed25cef69d115cfb9 Mon Sep 17 00:00:00 2001 From: xDivisionByZerox Date: Thu, 23 Jun 2022 20:38:50 +0200 Subject: [PATCH 09/12] chore(system.fileName): options destruction according to coding standards --- src/modules/system/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/modules/system/index.ts b/src/modules/system/index.ts index 6c1a74e176a..77be5a8267c 100644 --- a/src/modules/system/index.ts +++ b/src/modules/system/index.ts @@ -46,7 +46,7 @@ export class System { extensionCount?: number; } = {} ): string { - const extensionCount = options.extensionCount ?? 1; + const { extensionCount = 1 } = options; const baseName = this.faker.random .words() From 6e33f78c46cda22bccab831dd773e75b050a9d74 Mon Sep 17 00:00:00 2001 From: xDivisionByZerox Date: Fri, 24 Jun 2022 11:38:08 +0200 Subject: [PATCH 10/12] chore(system): fix grammar in test descriptions --- test/system.spec.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/system.spec.ts b/test/system.spec.ts index cbde00fd72f..55cf5ce7157 100644 --- a/test/system.spec.ts +++ b/test/system.spec.ts @@ -189,7 +189,7 @@ describe('system', () => { expect(parts).length(2); }); - it('should return filenames without a extension when extensionCount is 0', () => { + it('should return filenames without an extension when extensionCount is 0', () => { const fileName = faker.system.fileName({ extensionCount: 0, }); @@ -197,7 +197,7 @@ describe('system', () => { expect(fileName).not.toContain('.'); }); - it('should return filenames without a extension when extensionCount is negative', () => { + it('should return filenames without an extension when extensionCount is negative', () => { const fileName = faker.system.fileName({ extensionCount: -1, }); @@ -206,7 +206,7 @@ describe('system', () => { }); it.each(times(10))( - 'should return filenames with a %s extensions', + 'should return filenames with %s extensions', (extCount) => { const fileName = faker.system.fileName({ extensionCount: extCount, From 8bda5f0d3dfb290f6e96787ce20a9cf0e9c206e4 Mon Sep 17 00:00:00 2001 From: xDivisionByZerox Date: Fri, 24 Jun 2022 11:39:16 +0200 Subject: [PATCH 11/12] chore(system): rename variable --- test/system.spec.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/system.spec.ts b/test/system.spec.ts index 55cf5ce7157..0d7050672a8 100644 --- a/test/system.spec.ts +++ b/test/system.spec.ts @@ -207,13 +207,13 @@ describe('system', () => { it.each(times(10))( 'should return filenames with %s extensions', - (extCount) => { + (extensionCount) => { const fileName = faker.system.fileName({ - extensionCount: extCount, + extensionCount, }); const parts = fileName.split('.'); - expect(parts).length(extCount + 1); + expect(parts).length(extensionCount + 1); } ); }); From eb94851a788f3edf509793139be56fcd310f3d50 Mon Sep 17 00:00:00 2001 From: xDivisionByZerox Date: Tue, 12 Jul 2022 18:21:09 +0200 Subject: [PATCH 12/12] test(system.commonFileExt): update expected extension list --- test/system.spec.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/system.spec.ts b/test/system.spec.ts index 0d7050672a8..f795abfbaaa 100644 --- a/test/system.spec.ts +++ b/test/system.spec.ts @@ -49,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',