Skip to content

Commit

Permalink
Fix tempy.file() always adds extension-dot . even if no extension…
Browse files Browse the repository at this point in the history
… is given (#19)

Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
KSR-Yasuda and sindresorhus committed Feb 12, 2020
1 parent c5aa7ee commit 4ecbd80
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
5 changes: 2 additions & 3 deletions index.js
Expand Up @@ -8,19 +8,18 @@ const getPath = () => path.join(tempDir, uniqueString());

module.exports.file = options => {
options = {
extension: '',
...options
};

if (options.name) {
if (options.extension) {
if (options.extension !== undefined && options.extension !== null) {
throw new Error('The `name` and `extension` options are mutually exclusive');
}

return path.join(module.exports.directory(), options.name);
}

return getPath() + '.' + options.extension.replace(/^\./, '');
return getPath() + (options.extension === undefined || options.extension === null ? '' : '.' + options.extension.replace(/^\./, ''));
};

module.exports.directory = () => {
Expand Down
19 changes: 19 additions & 0 deletions test.js
Expand Up @@ -5,10 +5,29 @@ import tempy from '.';

test('.file()', t => {
t.true(tempy.file().includes(tmpdir()));
t.false(tempy.file().endsWith('.'));
t.false(tempy.file({extension: undefined}).endsWith('.'));
t.false(tempy.file({extension: null}).endsWith('.'));
t.true(tempy.file({extension: 'png'}).endsWith('.png'));
t.true(tempy.file({extension: '.png'}).endsWith('.png'));
t.false(tempy.file({extension: '.png'}).endsWith('..png'));
t.true(tempy.file({name: 'custom-name.md'}).endsWith('custom-name.md'));

t.throws(() => {
tempy.file({name: 'custom-name.md', extension: '.ext'});
});

t.throws(() => {
tempy.file({name: 'custom-name.md', extension: ''});
});

t.notThrows(() => {
tempy.file({name: 'custom-name.md', extension: undefined});
});

t.notThrows(() => {
tempy.file({name: 'custom-name.md', extension: null});
});
});

test('.directory()', t => {
Expand Down

0 comments on commit 4ecbd80

Please sign in to comment.