Skip to content

Commit

Permalink
Move the extension parameter to an options-object
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Feb 12, 2023
1 parent 64d0b46 commit 371a2a3
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 16 deletions.
25 changes: 19 additions & 6 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,30 @@
export type Options = {
/**
A file extension to append to the path.
@example
```
import tempfile from 'tempfile';
tempfile();
//=> '/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/6271e235-13b9-4138-8b9b-ee2f26c09ce3'
tempfile({extension: 'png'});
//=> '/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/4049f192-43e7-43b2-98d9-094e6760861b.png'
```
*/
readonly extension?: string;
};

/**
Get a random temporary file path.
@param extension - Extension to append to the path.
@example
```
import tempfile from 'tempfile';
tempfile('.png');
//=> '/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/4049f192-43e7-43b2-98d9-094e6760861b.png'
tempfile();
//=> '/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/6271e235-13b9-4138-8b9b-ee2f26c09ce3'
```
*/
export default function tempfile(extension?: string): string;
export default function tempfile(options?: Options): string;
15 changes: 13 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@ import path from 'node:path';
import {randomUUID} from 'node:crypto';
import tempDirectory from 'temp-dir';

export default function tempfile(extension = '') {
return path.join(tempDirectory, randomUUID() + extension);
export default function tempfile(options = {}) {
// TODO: Remove this for v6.
if (typeof options === 'string') {
throw new TypeError('You must now pass in the file extension as an object.');
}

let {extension} = options;

if (typeof extension === 'string') {
extension = extension.startsWith('.') ? extension : `.${extension}`;
}

return path.join(tempDirectory, randomUUID() + (extension ?? ''));
}
2 changes: 1 addition & 1 deletion index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ import {expectType} from 'tsd';
import tempfile from './index.js';

expectType<string>(tempfile());
expectType<string>(tempfile('.png'));
expectType<string>(tempfile({extension: 'png'}));
23 changes: 17 additions & 6 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,33 @@ npm install tempfile
```js
import tempfile from 'tempfile';

tempfile('.png');
//=> '/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/4049f192-43e7-43b2-98d9-094e6760861b.png'

tempfile();
//=> '/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/6271e235-13b9-4138-8b9b-ee2f26c09ce3'
```

## API

### tempfile(extension?)
### tempfile(options?)

#### options

Type: `object`

#### extension
##### extension

Type: `string`

Extension to append to the path.
A file extension to append to the path.

```js
import tempfile from 'tempfile';

tempfile();
//=> '/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/6271e235-13b9-4138-8b9b-ee2f26c09ce3'

tempfile({extension: 'png'});
//=> '/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/4049f192-43e7-43b2-98d9-094e6760861b.png'
```

## Related

Expand Down
3 changes: 2 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ import tempfile from './index.js';

test('main', t => {
t.true(tempfile().includes(tmpdir()));
t.true(tempfile('.png').endsWith('.png'));
t.true(tempfile({extension: 'png'}).endsWith('.png'));
t.true(tempfile({extension: '.png'}).endsWith('.png'));
});

0 comments on commit 371a2a3

Please sign in to comment.