diff options
| author | Leyla Jähnig <[email protected]> | 2022-07-19 11:46:07 +0200 |
|---|---|---|
| committer | GitHub <[email protected]> | 2022-07-19 11:46:07 +0200 |
| commit | 968134c398a11b698b489a492179080aa7ca8c73 (patch) | |
| tree | e1fa1633e54dfa9072b8ff0b1b4bc7d93b8d4f02 /src/modules | |
| parent | 316f61fdc4c9feaeebb9696982da92ae05608919 (diff) | |
| download | faker-968134c398a11b698b489a492179080aa7ca8c73.tar.xz faker-968134c398a11b698b489a492179080aa7ca8c73.zip | |
feat(system.fileName): file extension count (#1101)
Co-authored-by: Piotr Kuczynski <[email protected]>
Diffstat (limited to 'src/modules')
| -rw-r--r-- | src/modules/system/index.ts | 32 |
1 files changed, 28 insertions, 4 deletions
diff --git a/src/modules/system/index.ts b/src/modules/system/index.ts index 200deafe..77be5a82 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()}`; } |
