Skip to content

Commit

Permalink
feat: add ability to define number of generated extensions in system.…
Browse files Browse the repository at this point in the history
…filePath
  • Loading branch information
pkuczynski committed Apr 5, 2022
1 parent d706f18 commit a0f7905
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 7 deletions.
26 changes: 23 additions & 3 deletions src/system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`;
}
}
}

/**
Expand Down
26 changes: 22 additions & 4 deletions test/system.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,13 @@ describe('system', () => {
'htm',
'html',
'jpeg',
'm1v',
'm2a',
'm2v',
'mp2',
'mp3',
'm2v',
'mp2a',
'mp4',
'mp4v',
'mpeg',
Expand Down Expand Up @@ -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);
});
});

Expand Down

0 comments on commit a0f7905

Please sign in to comment.