blob: 0b75ee9d3038a18b306436b2050b2f691fa97cf4 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
# unused-filename [![Build Status](https://travis-ci.org/sindresorhus/unused-filename.svg?branch=master)](https://travis-ci.org/sindresorhus/unused-filename)
> Get an unused filename by appending a number if it exists: `file.txt` → `file (1).txt`
Useful for safely writing, copying, moving files without overwriting existing files.
## Install
```
$ npm install --save unused-filename
```
## Usage
```
.
├── rainbow (1).txt
├── rainbow.txt
└── unicorn.txt
```
```js
const unusedFilename = require('unused-filename');
unusedFilename('rainbow.txt').then(filename => {
console.log(filename);
//=> 'rainbow (2).txt'
});
```
## API
### unusedFilename(filepath)
Returns a `Promise<string>`.
### unusedFilename.sync(filepath)
Returns a `string`.
#### filepath
Type: `string`
## Related
- [filenamify](https://github.com/sindresorhus/filenamify) - Convert a string to a valid safe filename
## License
MIT © [Sindre Sorhus](https://sindresorhus.com)
|