Socket
Socket
Sign inDemoInstall

unused-filename

Package Overview
Dependencies
2
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 3.0.1 to 4.0.0

207

index.d.ts

@@ -1,150 +0,127 @@

declare namespace unusedFilename {
interface Options {
/**
Custom function to increment a filename.
export interface Options {
/**
A function that accepts a file path, and increments its index.
Default: Parentheses incrementer: `file.txt` → `file (1).txt`
*/
readonly incrementer?: Incrementer;
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 max number of attempts to find an unused filename.
When the limit is reached, the function will throw `unusedFilename.MaxTryError`.
@default Infinity
*/
readonly maxTries?: number;
}
/**
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.
@param filename - The filename of the file path.
@param extension - The extension of the file path.
Default: Parentheses incrementer: `file.txt` → `file (1).txt`
@returns A tuple of original filename, and new incremented filename, including extension.
@example
```
// Example incrementer that inserts a new index as a prefix.
import {unusedFilename} from 'unused-filename';
import unusedFilename = require('unused-filename');
// Incrementer that inserts a new index as a prefix.
const prefixIncrementer = (filename, extension) => {
const match = filename.match(/^(?<index>\d+)_(?<originalFilename>.*)$/);
let {originalFilename, index} = match ? match.groups : {originalFilename: filename, index: 0};
let originalFilename = originalFilename.trim();
return [`${originalFilename}${extension}`, `${++index}_${originalFilename}${extension}`;
};
(async () => {
const filename = await unusedFilename('rainbow.txt', {
incrementer(filename, extension) => {
const match = filename.match(/^(?<index>\d+)_(?<originalFilename>.*)$/);
let {originalFilename, index} = match ? match.groups : {originalFilename: filename, index: 0};
let originalFilename = originalFilename.trim();
return [`${originalFilename}${extension}`, `${++index}_${originalFilename}${extension}`;
}
});
console.log(filename);
//=> '1_rainbow.txt'
})();
console.log(await unusedFilename('rainbow.txt', {incrementer: prefixIncrementer}));
//=> '1_rainbow.txt'
```
*/
type Incrementer = (filename: string, extension: string) => [string, string];
readonly incrementer?: Incrementer;
/**
The error thrown when `maxTries` limit is reached without finding an unused filename.
The maximum number of attempts to find an unused filename.
@param originalPath - Path without the incrementation sequence.
@param lastTriedPath - The last tested incremented path.
When the limit is reached, the function will throw `MaxTryError`.
@example
```
import unusedFilename = require('unused-filename');
@default Infinity
*/
readonly maxTries?: number;
}
const {MaxTryError} = unusedFilename;
/**
@param filename - The filename of the file path.
@param extension - The extension of the file path.
try {
const path = await unusedFilename('rainbow (1).txt', {maxTries: 0});
} catch (error) {
if (error instanceof MaxTryError) {
console.log(error.originalPath); // 'rainbow.txt'
console.log(error.lastTriedPath); // 'rainbow (1).txt'
}
@returns A tuple of original filename, and new incremented filename, including extension.
*/
export type Incrementer = (filename: string, extension: string) => [string, string];
/**
The error thrown when `maxTries` limit is reached without finding an unused filename.
@param originalPath - Path without the incrementation sequence.
@param lastTriedPath - The last tested incremented path.
@example
```
import {unusedFilename, MaxTryError} from 'unused-filename';
try {
const path = await unusedFilename('rainbow (1).txt', {maxTries: 0});
} catch (error) {
if (error instanceof MaxTryError) {
console.log(error.originalPath); // 'rainbow.txt'
console.log(error.lastTriedPath); // 'rainbow (1).txt'
}
```
*/
/* eslint-disable @typescript-eslint/no-misused-new */
interface MaxTryError extends Error {
originalPath: string;
lastTriedPath: string;
new (originalPath: string, lastTriedPath: string): MaxTryError;
}
/* eslint-enable @typescript-eslint/no-misused-new */
}
```
*/
export class MaxTryError extends Error {
originalPath: string;
lastTriedPath: string;
/* eslint-disable no-redeclare */
declare const unusedFilename: {
/**
Get an unused filename by appending a number if it exists: `file.txt` → `file (1).txt`.
constructor(originalPath: string, lastTriedPath: string);
}
@param filePath - The path to check for filename collision.
@returns Either the original `filename` or the `filename` appended with a number (or modified by `option.incrementer` if specified).
/**
Get an unused filename by appending a number if it exists: `file.txt` → `file (1).txt`.
If an already incremented `filePath` is passed, `unusedFilename` will simply increment and replace the already existing index:
@param filePath - The path to check for filename collision.
@returns Either the original `filename` or the `filename` appended with a number (or modified by `option.incrementer` if specified).
@example
```
import unusedFilename = require('unused-filename');
If an already incremented `filePath` is passed, `unusedFilename` will simply increment and replace the already existing index:
(async () => {
console.log(await unusedFilename('rainbow (1).txt'));
//=> 'rainbow (2).txt'
})();
```
*/
(filePath: string, options?: unusedFilename.Options): Promise<string>;
@example
```
import {unusedFilename} from 'unused-filename';
MaxTryError: unusedFilename.MaxTryError;
console.log(await unusedFilename('rainbow (1).txt'));
//=> 'rainbow (2).txt'
```
*/
export function unusedFilename(filePath: string, options?: Options): Promise<string>;
// TODO: Remove this for the next major release
default: typeof unusedFilename;
/**
Synchronously get an unused filename by appending a number if it exists: `file.txt` → `file (1).txt`.
/**
Synchronously get an unused filename by appending a number if it exists: `file.txt` → `file (1).txt`.
@param filePath - The path to check for filename collision.
@returns Either the original `filename` or the `filename` appended with a number (or modified by `option.incrementer` if specified).
@param filePath - The path to check for filename collision.
@returns Either the original `filename` or the `filename` appended with a number (or modified by `option.incrementer` if specified).
If an already incremented `filePath` is passed, `unusedFilename` will simply increment and replace the already existing index:
If an already incremented `filePath` is passed, `unusedFilename` will simply increment and replace the already existing index:
@example
```
import {unusedFilenameSync} from 'unused-filename';
@example
```
import unusedFilename = require('unused-filename');
console.log(unusedFilenameSync('rainbow (1).txt'));
//=> 'rainbow (2).txt'
```
*/
export function unusedFilenameSync(filePath: string, options?: Options): string;
console.log(unusedFilename.sync('rainbow (1).txt'));
//=> 'rainbow (2).txt'
```
*/
sync(filePath: string, options?: unusedFilename.Options): string;
/**
Creates an incrementer that appends a number after a separator.
/**
Creates an incrementer that appends a number after a separator.
`separatorIncrementer('_')` will increment `file.txt` → `file_1.txt`.
`separatorIncrementer('_')` will increment `file.txt` → `file_1.txt`.
Not all characters can be used as separators:
- On Unix-like systems, `/` is reserved.
- On Windows, `<>:"/|?*` along with trailing periods are reserved.
Not all characters can be used as separators:
- On Unix-like systems, `/` is reserved.
- On Windows, `<>:"/|?*` along with trailing periods are reserved.
@example
```
import {unusedFilename, separatorIncrementer} from 'unused-filename';
@example
```
import unusedFilename = require('unused-filename');
console.log(await unusedFilename('rainbow.txt', {incrementer: unusedFilename.separatorIncrementer('_')}));
//=> 'rainbow_1.txt'
```
*/
separatorIncrementer(separator: string): unusedFilename.Incrementer;
};
/* eslint-enable no-redeclare */
export = unusedFilename;
console.log(await unusedFilename('rainbow.txt', {incrementer: separatorIncrementer('_')}));
//=> 'rainbow_1.txt'
```
*/
export function separatorIncrementer(separator: string): Incrementer;

@@ -1,7 +0,6 @@

'use strict';
const path = require('path');
const pathExists = require('path-exists');
const escapeStringRegexp = require('escape-string-regexp');
import path from 'node:path';
import {pathExists, pathExistsSync} from 'path-exists';
import escapeStringRegexp from 'escape-string-regexp';
class MaxTryError extends Error {
export class MaxTryError extends Error {
constructor(originalPath, lastTriedPath) {

@@ -21,3 +20,10 @@ super('Max tries reached.');

const separatorIncrementer = separator => {
const incrementPath = (filePath, incrementer) => {
const ext = path.extname(filePath);
const dirname = path.dirname(filePath);
const [originalFilename, incrementedFilename] = incrementer(path.basename(filePath, ext), ext);
return [path.join(dirname, originalFilename), path.join(dirname, incrementedFilename)];
};
export const separatorIncrementer = separator => {
const escapedSeparator = escapeStringRegexp(separator);

@@ -32,10 +38,3 @@

const incrementPath = (filePath, incrementer) => {
const ext = path.extname(filePath);
const dirname = path.dirname(filePath);
const [originalFilename, incrementedFilename] = incrementer(path.basename(filePath, ext), ext);
return [path.join(dirname, originalFilename), path.join(dirname, incrementedFilename)];
};
const unusedFilename = async (filePath, {incrementer = parenthesesIncrementer, maxTries = Number.POSITIVE_INFINITY} = {}) => {
export async function unusedFilename(filePath, {incrementer = parenthesesIncrementer, maxTries = Number.POSITIVE_INFINITY} = {}) {
let tries = 0;

@@ -58,9 +57,5 @@ let [originalPath] = incrementPath(filePath, incrementer);

/* eslint-enable no-await-in-loop, no-constant-condition */
};
}
module.exports = unusedFilename;
// TODO: Remove this for the next major release
module.exports.default = unusedFilename;
module.exports.sync = (filePath, {incrementer = parenthesesIncrementer, maxTries = Number.POSITIVE_INFINITY} = {}) => {
export function unusedFilenameSync(filePath, {incrementer = parenthesesIncrementer, maxTries = Number.POSITIVE_INFINITY} = {}) {
let tries = 0;

@@ -72,3 +67,3 @@ let [originalPath] = incrementPath(filePath, incrementer);

while (true) {
if (!pathExists.sync(unusedPath)) {
if (!pathExistsSync(unusedPath)) {
return unusedPath;

@@ -84,5 +79,2 @@ }

/* eslint-enable no-constant-condition */
};
module.exports.MaxTryError = MaxTryError;
module.exports.separatorIncrementer = separatorIncrementer;
}
{
"name": "unused-filename",
"version": "3.0.1",
"version": "4.0.0",
"description": "Get an unused filename by appending a number if it exists: `file.txt` → `file (1).txt`",

@@ -13,4 +13,6 @@ "license": "MIT",

},
"type": "module",
"exports": "./index.js",
"engines": {
"node": ">=12"
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
},

@@ -40,10 +42,10 @@ "scripts": {

"dependencies": {
"escape-string-regexp": "^4.0.0",
"path-exists": "^4.0.0"
"escape-string-regexp": "^5.0.0",
"path-exists": "^5.0.0"
},
"devDependencies": {
"ava": "^2.4.0",
"tsd": "^0.15.0",
"xo": "^0.39.0"
"ava": "^3.15.0",
"tsd": "^0.19.0",
"xo": "^0.46.4"
}
}

@@ -9,5 +9,5 @@ # unused-filename

```sh
npm install unused-filename
```
$ npm install unused-filename
```

@@ -24,8 +24,6 @@ ## Usage

```js
const unusedFilename = require('unused-filename');
import {unusedFilename} from 'unused-filename';
(async () => {
console.log(await unusedFilename('rainbow.txt'));
//=> 'rainbow (2).txt'
})();
console.log(await unusedFilename('rainbow.txt'));
//=> 'rainbow (2).txt'
```

@@ -42,11 +40,9 @@

```js
const unusedFilename = require('unused-filename');
import {unusedFilename} from 'unused-filename';
(async () => {
console.log(await unusedFilename('rainbow (1).txt'));
//=> 'rainbow (2).txt'
})();
console.log(await unusedFilename('rainbow (1).txt'));
//=> 'rainbow (2).txt'
```
### unusedFilename.sync(filePath, options?)
### unusedFilenameSync(filePath, options?)

@@ -70,11 +66,12 @@ Synchronous version of `unusedFilename`.

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.
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.
Example incrementer that inserts a new index as a prefix:
```js
const unusedFilename = require('unused-filename');
import {unusedFilename} from 'unused-filename';
// Incrementer that inserts a new index as a prefix.
const prefixIncrementer = (filename, extension) => {

@@ -96,7 +93,7 @@ const match = filename.match(/^(?<index>\d+)_(?<originalFilename>.*)$/);

The max number of attempts to find an unused filename.
The maximum number of attempts to find an unused filename.
When the limit is reached, the function will throw `unusedFilename.MaxTryError`.
When the limit is reached, the function will throw `MaxTryError`.
### unusedFilename.separatorIncrementer
### separatorIncrementer

@@ -112,9 +109,9 @@ Creates an incrementer that appends a number after a separator.

```js
const unusedFilename = require('unused-filename');
import {unusedFilename, separatorIncrementer} from 'unused-filename';
console.log(await unusedFilename('rainbow.txt', {incrementer: unusedFilename.separatorIncrementer('_')}));
console.log(await unusedFilename('rainbow.txt', {incrementer: separatorIncrementer('_')}));
//=> 'rainbow_1.txt'
```
### unusedFilename.MaxTryError
### MaxTryError

@@ -131,6 +128,4 @@ The error thrown when `maxTries` limit is reached without finding an unused filename.

```js
const unusedFilename = require('unused-filename');
import {unusedFilename, MaxTryError} from 'unused-filename';
const {MaxTryError} = unusedFilename;
try {

@@ -137,0 +132,0 @@ const path = await unusedFilename('rainbow (1).txt', {maxTries: 0});

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc