npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

uniquefilename

v1.1.2

Published

Finds a unique filename using incremental values

Downloads

1,268

Readme

uniquefilename

npm version Travis bitHound Score Coverage Status

A module to get a filename - that doesn't already exist on the filesystem - using incremental values.

Install

npm install uniquefilename

Usage

uniquefilename.get(filepath, [options], [callback])

Basic example

var uniquefilename = require('uniquefilename');
options = {};
uniquefilename.get('/path/to/dir/file.jpg', options, function(filename) {
  // filename might be "/path/to/dir/file.jpg", 
  // "/path/to/dir/file-2.jpg", "/path/to/dir/file-3.jpg", etc...
  // depending on the files that exist on your filesystem
});

Example with promises

If a callback is not supplied, a Promise is returned (using bluebird).

var uniquefilename = require('uniquefilename');
uniquefilename.get('/path/to/dir/file.jpg', {}).then(function (filename) {
  console.log(filename);
});

Advanced example

var uniquefilename = require('uniquefilename');
options = {separator: '~', paddingCharacter: '0', paddingSize: 4, mode: 'alphanumeric'};
uniquefilename.get('/path/to/dir/file.jpg', options, function(filename) {
  // filename might be "/path/to/dir/file.jpg", 
  // "/path/to/dir/file~000h.jpg", "/path/to/dir/file~00h9.jpg", etc...
  // depending on the files that exist on your filesystem
});

Options

separator

If the specified filename exists, the separator will be added before the incremental value such as: file{separator}2.jpg

The default value is '-'.

mode

The mode allows you to specify which characters to use to generate the incremental value (the string after the separator)

The default value is 'numeric'.

  • 'numeric' Using the following characters: 1234567890
  • 'alpha' Using the following characters: abcdefghijklmnopqrstuvwxyz
  • 'ALPHA' Using the following characters: ABCDEFGHIJKLMNOPQRSTUVWXYZ
  • 'alphanumeric' Using the following characters: 0123456789abcdefghijklmnopqrstuvwxyz
  • 'ALPHANUMERIC' Using the following characters: 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ
  • 'charset' You must specify the characters you wish to use in the charset option

paddingCharacter && paddingSize

If you wish to left-pad the incremental values with a character, use this option. Here's an example :

var uniquefilename = require('uniquefilename');
options = {mode: 'alpha', paddingCharacter: '0', paddingSize: 3};
uniquefilename.get('/path/to/dir/file.jpg', options, function(filename) {
  // filename might be "/path/to/dir/file.jpg", 
  // "/path/to/dir/file-002.jpg", "/path/to/dir/file-045.jpg", etc...
  // depending on the files that exist on your filesystem
});

Using it with multer

Multer is a node.js middleware for handling multipart/form-data, which is primarily used for uploading files. The following example shows you how to use multer along with this module to move an uploaded file to a unique filename :

var multer      = require('multer');
var path      = require('path');
var uniquefilename  = require('uniquefilename');

router.use(multer({
  storage: multer.diskStorage({
    destination: function (req, file, cb) {
      cb(null, './public/uploads/')
    },
    filename: function (req, file, cb) {
      originalname = path.resolve('./public/uploads/' + file.originalname);
      options = {};
      uniquefilename.get(originalname, options, function(filename) {
         cb(null, path.basename(filename));
      });
    }
  })
}).single('thumbnail'));