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

Add incrementer option #8

Closed
wants to merge 11 commits into from
Closed

Conversation

fisker
Copy link

@fisker fisker commented May 28, 2019

TODO:

update index.d.ts
tests fail on windows, because of different path separator
check incrementer type and result to avoid an infinite loop

Fixes #7

readme.md Outdated Show resolved Hide resolved
test.js Outdated Show resolved Hide resolved
@sindresorhus sindresorhus changed the title Add optional incrementer, fix #7 Add incrementer option Jun 15, 2019
@sindresorhus
Copy link
Owner

@fisker
Copy link
Author

fisker commented Jul 6, 2019

@sindresorhus could you update typescript-definition, not sure how to do it

index.d.ts Outdated
declare namespace unusedFilename {
interface Options {
/**
Filename increase function
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should better explain what it does and some use-cases for it. Also add a code example. Same in the readme.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added some text, not too many experiences on docs

test.js Outdated Show resolved Hide resolved
},
result: 'fixtures/rainbow - copy - copy - copy.txt'
}
].map(incrementerTest)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

separate test?

  √ sync
  √ `incrementer` should be a function
  √ `incrementer` should return a string
  √ `incrementer` should return unique names
  √ custom incrementer
  √ async

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

@sindresorhus
Copy link
Owner

// @fabiospampinato


const checkIsTried = newFilePath => {
if (triedFileNames.has(newFilePath)) {
throw new Error('`incrementer` should return unique names');
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know, should it?

If I'm generating a random 4-characters name (like U1HA), but it's already taken and my incrementer generates another random 4-characters name it may happen that it generates U1HA again, I don't think the app should crash in this scenario.

Also maybe a name we tried earlier on it's free to use now, I don't think this check is necessary.

Maybe a maxTries option should be added instead, so if for some reason there are no available names the library throws an error after maxTries number of tries rather than being busy forever.

Copy link
Author

@fisker fisker Jul 9, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree we should have a maxTries, but I am not sure about the default value. Infinity or a specific number?

I think unique name check is necessary too.

What do you think? @sindresorhus

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, it should not throw. As for maxTries, I cannot think of a sensible default, so I would go with Infinity (Unless @fabiospampinato can argument for something better).

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm setting it to 128, but it's just a number I pulled out of nowhere, my reasoning against Infinity was that if it's impossible to find an unused path for whatever reason I don't want the app to hang forever, you don't want it to give up trying too early either though, so I'm setting it to 128 by default which feels about right to me.

index.js Outdated Show resolved Hide resolved
@fisker
Copy link
Author

fisker commented Jul 9, 2019

Should this option named incrementer or incrementor?

@fisker
Copy link
Author

fisker commented Jul 9, 2019

I have another idea, how about Generator support?

{
  * incrementer(filename, extension) {
    let i = 0
    while(true) {
      yield `${filename}(${++i})${extension}`
    }
  }
}

useful case, If I hava a few options,

without Generator

{
  incrementer(filename, extension, counter) {
    return ['a.js','b.js','c.js'][counter - 1]
  }
}

with Generator

{
  * incrementer() {
    yield 'a.js'
    yield 'b.js'
    yield 'c.js'
  }
}


Type: `function`

filename increase function.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The readme should be in sync with index.d.ts


@param filePath - The path to check for filename collision.
@returns Either the original `filename` or the `filename` appended with a number.
@returns Either the original `filename` or the `filename` increment by `option.incrementer`.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
@returns Either the original `filename` or the `filename` increment by `option.incrementer`.
@returns Either the original `filename` or the `filename` appended with a number (or modified by `option.incrementer` if specified).


@param filename - The filename of the file path.
@param extension - The extension of the file path.
@param counter - Tried count.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tried count is not really a good description.

@sindresorhus
Copy link
Owner

Should this option named incrementer or incrementor?

incrementer

@sindresorhus
Copy link
Owner

I like the use of generator for this.

@fisker
Copy link
Author

fisker commented Nov 16, 2019

totally forgot this PR...

@sindresorhus
Copy link
Owner

Bump :)

@fisker
Copy link
Author

fisker commented Feb 28, 2020

Sorry for the late response, but I'll be busy for a while, no time to work on this.

Base automatically changed from master to main January 24, 2021 05:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Custom incrementer function
3 participants