What is unique-filename?
The unique-filename npm package is a simple utility that generates a unique filename with an optional file path and extension. It can be used to ensure that files written to a directory do not clash with existing filenames.
What are unique-filename's main functionalities?
Generate a unique filename
This feature generates a unique filename within the specified directory. The result is a random filename that is unlikely to clash with existing files.
const uniqueFilename = require('unique-filename');
const randomFilePath = uniqueFilename('/some/path');
console.log(randomFilePath);
Generate a unique filename with a specific extension
This feature generates a unique filename with a specified file extension, ensuring that the file fits the desired format.
const uniqueFilename = require('unique-filename');
const randomFilePath = uniqueFilename('/some/path', '.txt');
console.log(randomFilePath);
Generate a unique filename with a custom random string generator
This feature allows for a custom random string generator to be used, providing control over the randomness and format of the generated filename.
const uniqueFilename = require('unique-filename');
const customRandom = () => 'custom-string';
const randomFilePath = uniqueFilename('/some/path', '.txt', customRandom);
console.log(randomFilePath);
Other packages similar to unique-filename
tmp
The 'tmp' package offers the creation of temporary files and directories. It can generate unique file names, similar to unique-filename, but also provides additional features such as automatic cleanup of the temporary files.
tempfile
The 'tempfile' package is another utility for generating unique file paths, but it focuses on temporary files. It automatically appends a random string to a given file prefix, which is a simpler approach compared to unique-filename's more flexible API.
mktemp
The 'mktemp' package creates unique temporary filenames or directories, similar to the Unix command of the same name. It is more complex than unique-filename, as it includes template-based filename generation and directory creation.
unique-filename
Generate a unique filename for use in temporary directories or caches.
const uniqueFilename = require('unique-filename')
const randomTmpfile = uniqueFilename(os.tmpdir())
const randomPrefixedTmpfile = uniqueFilename(os.tmpdir(), 'my-test')
const uniqueTmpfile = uniqueFilename('/my-tmp-dir', 'testing', '/my/thing/to/uniq/on')
uniqueFilename(dir, fileprefix, uniqstr) → String
Returns the full path of a unique filename that looks like:
dir/prefix-7ddd44c0
or dir/7ddd44c0
dir – The path you want the filename in. os.tmpdir()
is a good choice for this.
fileprefix – A string to append prior to the unique part of the filename.
The parameter is required if uniqstr is also passed in but is otherwise
optional and can be undefined
/null
/''
. If present and not empty
then this string plus a hyphen are prepended to the unique part.
uniqstr – Optional, if not passed the unique part of the resulting
filename will be random. If passed in it will be generated from this string
in a reproducible way.