Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Require Node.js 8, add TypeScript definition #4

Merged
merged 3 commits into from
Mar 12, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
* text=auto
*.js text eol=lf
* text=auto eol=lf
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
yarn.lock
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
language: node_js
node_js:
- '10'
- '8'
- '6'
- '4'
21 changes: 21 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
interface UnusedFilename {
/**
* 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.
*/
(filepath: string): Promise<string>;

/**
* Synchronpusly get an unused filename by appending a number if it exists: `file.txt` → `file (1).txt`.
BendingBender marked this conversation as resolved.
Show resolved Hide resolved
*
* @param filepath - The path to check for filename collision.
* @returns Either the original `filename` or the `filename` appended with a number.
*/
sync(filepath: string): string;
}

declare const unusedFilename: UnusedFilename;
BendingBender marked this conversation as resolved.
Show resolved Hide resolved

export default unusedFilename;
15 changes: 9 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@
const pathExists = require('path-exists');
const modifyFilename = require('modify-filename');

const incrementer = fp => {
const incrementer = filepath => {
let i = 0;
return () => modifyFilename(fp, (filename, ext) => `${filename} (${++i})${ext}`);
return () => modifyFilename(filepath, (filename, ext) => `${filename} (${++i})${ext}`);
};

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 getFp = incrementer(filepath);
const find = newFilepath => pathExists(newFilepath).then(x => x ? find(getFp()) : newFilepath);
return find(filepath);
};

module.exports = unusedFilename;
module.exports.default = unusedFilename;

module.exports.sync = fp => {
BendingBender marked this conversation as resolved.
Show resolved Hide resolved
const getFp = incrementer(fp);
const find = newFp => pathExists.sync(newFp) ? find(getFp()) : newFp;
Expand Down
5 changes: 5 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import {expectType} from 'tsd-check';
import unusedFilename from '.';

expectType<Promise<string>>(unusedFilename('rainbow.txt'));
expectType<string>(unusedFilename.sync('rainbow.txt'));
86 changes: 44 additions & 42 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,44 +1,46 @@
{
"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": "[email protected]",
"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": "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": "[email protected]",
"url": "sindresorhus.com"
},
"engines": {
"node": ">=6"
},
"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"
}
}
5 changes: 3 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,17 @@ unusedFilename('rainbow.txt').then(filename => {

BendingBender marked this conversation as resolved.
Show resolved Hide resolved
### 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)

Returns a `string`.
Returns a `string` containing either the original `filename` or the `filename` appended with a number.

#### filepath

Type: `string`

The path to check for filename collision.

## Related

Expand Down
14 changes: 7 additions & 7 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import test from 'ava';
import m from '.';
import unusedFilename from '.';

test('async', async t => {
t.is(await m('fixtures/noop.txt'), 'fixtures/noop.txt');
t.is(await m('fixtures/unicorn.txt'), 'fixtures/unicorn (1).txt');
t.is(await m('fixtures/rainbow.txt'), 'fixtures/rainbow (3).txt');
t.is(await unusedFilename('fixtures/noop.txt'), 'fixtures/noop.txt');
t.is(await unusedFilename('fixtures/unicorn.txt'), 'fixtures/unicorn (1).txt');
t.is(await unusedFilename('fixtures/rainbow.txt'), 'fixtures/rainbow (3).txt');
});

test('sync', t => {
t.is(m.sync('fixtures/noop.txt'), 'fixtures/noop.txt');
t.is(m.sync('fixtures/unicorn.txt'), 'fixtures/unicorn (1).txt');
t.is(m.sync('fixtures/rainbow.txt'), 'fixtures/rainbow (3).txt');
t.is(unusedFilename.sync('fixtures/noop.txt'), 'fixtures/noop.txt');
t.is(unusedFilename.sync('fixtures/unicorn.txt'), 'fixtures/unicorn (1).txt');
t.is(unusedFilename.sync('fixtures/rainbow.txt'), 'fixtures/rainbow (3).txt');
});