unused-filename
Advanced tools
Comparing version 1.0.0 to 2.0.0
25
index.js
@@ -5,17 +5,20 @@ 'use strict'; | ||
const incrementer = fp => { | ||
let i = 0; | ||
return () => modifyFilename(fp, (filename, ext) => `${filename} (${++i})${ext}`); | ||
const incrementer = filePath => { | ||
let counter = 0; | ||
return () => modifyFilename(filePath, (filename, extension) => `${filename} (${++counter})${extension}`); | ||
}; | ||
module.exports = fp => { | ||
const getFp = incrementer(fp); | ||
const find = newFp => pathExists(newFp).then(x => x ? find(getFp()) : newFp); | ||
return find(fp); | ||
const unusedFilename = filePath => { | ||
const getFilePath = incrementer(filePath); | ||
const find = async newFilePath => await pathExists(newFilePath) ? find(getFilePath()) : newFilePath; | ||
return find(filePath); | ||
}; | ||
module.exports.sync = fp => { | ||
const getFp = incrementer(fp); | ||
const find = newFp => pathExists.sync(newFp) ? find(getFp()) : newFp; | ||
return find(fp); | ||
module.exports = unusedFilename; | ||
module.exports.default = unusedFilename; | ||
module.exports.sync = filePath => { | ||
const getFilePath = incrementer(filePath); | ||
const find = newFilePath => pathExists.sync(newFilePath) ? find(getFilePath()) : newFilePath; | ||
return find(filePath); | ||
}; |
{ | ||
"name": "unused-filename", | ||
"version": "1.0.0", | ||
"description": "Get an unused filename by appending a number if it exists: `file.txt` → `file (1).txt`", | ||
"license": "MIT", | ||
"repository": "sindresorhus/unused-filename", | ||
"author": { | ||
"name": "Sindre Sorhus", | ||
"email": "sindresorhus@gmail.com", | ||
"url": "sindresorhus.com" | ||
}, | ||
"engines": { | ||
"node": ">=4" | ||
}, | ||
"scripts": { | ||
"test": "xo && ava" | ||
}, | ||
"files": [ | ||
"index.js" | ||
], | ||
"keywords": [ | ||
"unused", | ||
"filename", | ||
"filepath", | ||
"file", | ||
"name", | ||
"available", | ||
"safe", | ||
"unique", | ||
"usable", | ||
"filesystem", | ||
"fs", | ||
"exists", | ||
"path" | ||
], | ||
"dependencies": { | ||
"modify-filename": "^1.1.0", | ||
"path-exists": "^3.0.0" | ||
}, | ||
"devDependencies": { | ||
"ava": "*", | ||
"xo": "*" | ||
} | ||
"name": "unused-filename", | ||
"version": "2.0.0", | ||
"description": "Get an unused filename by appending a number if it exists: `file.txt` → `file (1).txt`", | ||
"license": "MIT", | ||
"repository": "sindresorhus/unused-filename", | ||
"author": { | ||
"name": "Sindre Sorhus", | ||
"email": "sindresorhus@gmail.com", | ||
"url": "sindresorhus.com" | ||
}, | ||
"engines": { | ||
"node": ">=8" | ||
}, | ||
"scripts": { | ||
"test": "xo && ava && tsd-check" | ||
}, | ||
"files": [ | ||
"index.js", | ||
"index.d.ts" | ||
], | ||
"keywords": [ | ||
"unused", | ||
"filename", | ||
"filepath", | ||
"file", | ||
"name", | ||
"available", | ||
"safe", | ||
"unique", | ||
"usable", | ||
"filesystem", | ||
"fs", | ||
"exists", | ||
"path" | ||
], | ||
"dependencies": { | ||
"modify-filename": "^1.1.0", | ||
"path-exists": "^3.0.0" | ||
}, | ||
"devDependencies": { | ||
"ava": "^1.3.1", | ||
"tsd-check": "^0.3.0", | ||
"xo": "^0.24.0" | ||
} | ||
} |
@@ -11,3 +11,3 @@ # unused-filename [![Build Status](https://travis-ci.org/sindresorhus/unused-filename.svg?branch=master)](https://travis-ci.org/sindresorhus/unused-filename) | ||
``` | ||
$ npm install --save unused-filename | ||
$ npm install unused-filename | ||
``` | ||
@@ -28,6 +28,6 @@ | ||
unusedFilename('rainbow.txt').then(filename => { | ||
console.log(filename); | ||
(async () => { | ||
console.log(await unusedFilename('rainbow.txt')); | ||
//=> 'rainbow (2).txt' | ||
}); | ||
})(); | ||
``` | ||
@@ -38,15 +38,17 @@ | ||
### unusedFilename(filepath) | ||
### unusedFilename(filePath) | ||
Returns a `Promise<string>`. | ||
Returns a `Promise<string>` containing either the original `filename` or the `filename` appended with a number. | ||
### unusedFilename.sync(filepath) | ||
### unusedFilename.sync(filePath) | ||
Returns a `string`. | ||
Returns a `string` containing either the original `filename` or the `filename` appended with a number. | ||
#### filepath | ||
#### filePath | ||
Type: `string` | ||
The path to check for filename collision. | ||
## Related | ||
@@ -53,0 +55,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
4529
5
36
59
3