npm.io
2.0.1 • Published 7 years ago

new-filename

Licence
MIT
Version
2.0.1
Deps
0
Size
8 kB
Vulns
0
Weekly
0

new-filename

Generating a new filename like 'basename (1).ext' without conflicting with existing ones.

Install

npm install new-filename
# or
yarn add new-filename

Usage

/**
 * Generating a new filename to avoid names in the list by adding sequenced number
 * @param list - Filenames already in use
 * @param name - New filenames about to use
 * @returns Generated new filename
 */
getNewFilename(list: string[]]), name: string): string
Example
import getNewFilename from 'new-filename';

const newName = getNewFilename([
  'untitled.txt',
  'untitled (2).txt',
  'file3.txt',
  'file4.java',
], 'untitled.txt');

console.log(newName);
// => 'untitled (3).txt'

Tests

npm test

Similar Packages

unused-filename has the same feature, but it operates on the file system, new-filename gives you a opportunity to get unused filename independent from platform (e.g. on browser).

Keywords