This package matches a file name of type string
against an Array of objects of type IFile
and return a unique file name of type string
.
Learn about the latest improvements.
npm i duplicate-filename-resolver --save
IFile
- Represents an Object containing file name of typestring
and the file object of typeFile
.
{
name: string;
file: File;
}
getUniqueFileName(allFiles: IFile[], newFileName: string)
- Iterates through passed arrayallFiles
of typeIFile[]
matching passednewFileName
of typestring
. If duplicate is found, return a new unique file name. Else, returns same name.
Import the package.
import { getUniqueFileName, IFile } from "duplicate-filename-resolver";
Declare a variable allFiles
of type IFile
.
var allFiles: IFile[] = [{
name: "file.txt"
}];
allFiles
is a array of typeIFile[]
.
Call method getUniqueFileName(allFiles: IFile[], newFileName: string)
and pass alFiles
array and a string
file name.
var fileName string = getUniqueFileName(this.allFiles, "file.txt");
Output - "file (1).txt"
var allFiles: IFile[] = [{
name: "file (1).txt"
}];
var fileName string = getUniqueFileName(this.allFiles, "file (1).txt");
Output - "file (2).txt"
var allFiles: IFile[] = [{
name: "file (1).txt"
}];
var fileName string = getUniqueFileName(this.allFiles, "file(1).txt");
Output - "file(1).txt"
var allFiles: IFile[] = [{
name: "file(1).txt"
}];
var fileName string = getUniqueFileName(this.allFiles, "file(1).txt");
Output - "file(1) (1).txt"
var allFiles: IFile[] = [{
name: "file.(1).txt"
}];
var fileName string = getUniqueFileName(this.allFiles, "file.(1).txt");
Output - "file.(1) (1).txt"
var allFiles: IFile[] = [{
name: "file (1)(2).txt"
}];
var fileName string = getUniqueFileName(this.allFiles, "file (1)(2).txt");
Output - "file (1)(2) (1).txt"