Gathering detailed insights and metrics for unused-filename
Gathering detailed insights and metrics for unused-filename
Get an unused filename by appending a number if it exists: `file.txt` → `file (1).txt`
npm install unused-filename
Typescript
Module System
Min. Node Version
Node Version
NPM Version
99.5
Supply Chain
99.5
Quality
75.5
Maintenance
100
Vulnerability
100
License
JavaScript (93.84%)
TypeScript (6.16%)
Total Downloads
11,321,679
Last Day
12,163
Last Week
57,804
Last Month
360,421
Last Year
2,862,327
141 Stars
21 Commits
9 Forks
5 Watching
1 Branches
6 Contributors
Minified
Minified + Gzipped
Latest Version
4.0.1
Package Id
unused-filename@4.0.1
Unpacked Size
12.33 kB
Size
3.57 kB
File Count
5
NPM Version
8.3.2
Node Version
16.14.0
Cumulative downloads
Total Downloads
Last day
-63.6%
12,163
Compared to previous day
Last week
-58.1%
57,804
Compared to previous week
Last month
76.2%
360,421
Compared to previous month
Last year
-4.2%
2,862,327
Compared to previous year
2
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.
1npm install unused-filename
.
├── rainbow (1).txt
├── rainbow.txt
└── unicorn.txt
1import {unusedFilename} from 'unused-filename'; 2 3console.log(await unusedFilename('rainbow.txt')); 4//=> 'rainbow (2).txt'
Returns a Promise<string>
containing either the original filename
or the filename
increment by options.incrementer
.
If an already incremented filePath
is passed, unusedFilename
will simply increment and replace the already existing index:
1import {unusedFilename} from 'unused-filename'; 2 3console.log(await unusedFilename('rainbow (1).txt')); 4//=> 'rainbow (2).txt'
Synchronous version of unusedFilename
.
Type: string
The path to check for filename collision.
Type: object
Type: (filePath: string) => [string, string]
Default: Parentheses incrementer: file.txt
→ file (1).txt
A function that accepts a file path, and increments its index.
It's the incrementer's responsibility to extract an already existing index from the given file path so that it picks up and continues incrementing an already present index instead of appending a second one.
The incrementer has to return a tuple of [originalFilename, incrementedFilename]
, where originalFilename
is the filename without the index, and incrementedFilename
is a filename with input's index bumped by one.
1import {unusedFilename} from 'unused-filename'; 2 3// Incrementer that inserts a new index as a prefix. 4const prefixIncrementer = (filename, extension) => { 5 const match = filename.match(/^(?<index>\d+)_(?<originalFilename>.*)$/); 6 let {originalFilename, index} = match ? match.groups : {originalFilename: filename, index: 0}; 7 originalFilename = originalFilename.trim(); 8 return [`${originalFilename}${extension}`, `${++index}_${originalFilename}${extension}`]; 9}; 10 11console.log(await unusedFilename('rainbow.txt', {incrementer: prefixIncrementer})); 12//=> '1_rainbow.txt'
Type: number
Default: Infinity
The maximum number of attempts to find an unused filename.
When the limit is reached, the function will throw MaxTryError
.
Creates an incrementer that appends a number after a separator.
separatorIncrementer('_')
will increment file.txt
→ file_1.txt
.
Not all characters can be used as separators:
/
is reserved.<>:"/|?*
along with trailing periods are reserved.1import {unusedFilename, separatorIncrementer} from 'unused-filename'; 2 3console.log(await unusedFilename('rainbow.txt', {incrementer: separatorIncrementer('_')})); 4//=> 'rainbow_1.txt'
The error thrown when maxTries
limit is reached without finding an unused filename.
It comes with 2 custom properties:
originalPath
- Path without incrementation sequence.lastTriedPath
- The last tested incremented path.Example:
1import {unusedFilename, MaxTryError} from 'unused-filename'; 2 3try { 4 const path = await unusedFilename('rainbow (1).txt', {maxTries: 0}); 5} catch (error) { 6 if (error instanceof MaxTryError) { 7 console.log(error.originalPath); // 'rainbow.txt' 8 console.log(error.lastTriedPath); // 'rainbow (1).txt' 9 } 10}
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
license file detected
Details
Reason
0 existing vulnerabilities detected
Reason
Found 5/21 approved changesets -- score normalized to 2
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
security policy file not detected
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2025-01-27
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More