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

Added possibility to define min and max values for a number option #1278

Merged
merged 8 commits into from Apr 25, 2020
Merged

Added possibility to define min and max values for a number option #1278

merged 8 commits into from Apr 25, 2020

Conversation

krisztianb
Copy link
Contributor

Also added a couple of tests for the new feature.

type: ParameterType.Number,
defaultValue: 1
};
doesNotThrow(() => options.addDeclaration(declaration));
Copy link
Collaborator

Choose a reason for hiding this comment

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

doesNotThrow is redundant. All it does is rethrow an error if one occurs.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm confused. What would be the right solution? I guess you understand what it is trying to assert.

Copy link
Collaborator

Choose a reason for hiding this comment

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

It doesn't hurt anything, I guess there is some semantic value, but that's provided by the test name. It's fine as is.

const numberOption = option as NumberDeclarationOption;
const numValue = parseInt(String(value), 10) || 0;
if (typeof numberOption.minValue === 'number' && numValue < numberOption.minValue) {
return Result.Err(`${numberOption.name} must be >= ${numberOption.minValue}`);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is there a reason you don't include both bounds in the error message? It seems like it would be more useful:

$ typedoc --numOption 10
Error: numOption must be <= 5
$ typedoc --numOption 2
Error: numOption must be >= 3
# vs
$ typedoc --numOption 10
Error: numOption must be between 2 and 10

Probably best to extract the error message creation to a helper function, since there are several possible combinations.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree that if both are defined it would be more useful to include both bounds in the error message.

* @returns True, if the value is a finite number, otherwise false.
*/
function isFiniteNumber(value?: unknown): value is number {
return typeof value === 'number' && isFinite(value);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Well this sent me on a wild goose chase. This is equivalent to Number.isFinite, but the types for that function say they only accept number, so TS would yell... this was fixed, but the fix changed generated files instead of the source files and so was thrown out in the next release.

type: ParameterType.Number,
defaultValue: 1
};
doesNotThrow(() => options.addDeclaration(declaration));
Copy link
Collaborator

Choose a reason for hiding this comment

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

It doesn't hurt anything, I guess there is some semantic value, but that's provided by the test name. It's fine as is.

@Gerrit0 Gerrit0 merged commit 42de2af into TypeStrong:master Apr 25, 2020
if (isFiniteNumber(minValue) && isFiniteNumber(maxValue)) {
return minValue <= value && value <= maxValue;
} else if (isFiniteNumber(minValue)) {
return minValue >= value;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Didn't see this until after I merged, I'll fix it. Should be minValue <= value

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry. You're right. I've probably should have written a couple of more tests and this would have popped up.

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.

None yet

2 participants