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

Fix assertion message for .all and .any #132

Merged

Conversation

scraggo
Copy link
Contributor

@scraggo scraggo commented Apr 11, 2021

This PR fixes #131

Approach: Add an options object to assertType. This allows a custom failure message from the calling code. It's a function that returns a string and passed in as getValuesMessage to the options object.

What do you think of the approach?

source/index.ts Outdated
@@ -388,9 +388,11 @@ is.any = (predicate: Predicate | Predicate[], ...values: unknown[]): boolean =>

is.all = (predicate: Predicate, ...values: unknown[]): boolean => predicateOnArray(Array.prototype.every, predicate, values);

const assertType = (condition: boolean, description: string, value: unknown): asserts condition => {
const assertType = (condition: boolean, description: string, value: unknown, options: { getValuesMessage?: () => string } = {}): asserts condition => {
Copy link
Owner

Choose a reason for hiding this comment

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

I don't see the point in nesting it in an options object. We're only using this internally. Can just be a normal argument.

@sindresorhus
Copy link
Owner

Does the issue also apply to .all? If so, we should fix it there too.

@scraggo
Copy link
Contributor Author

scraggo commented Apr 21, 2021

@sindresorhus I tried a slightly different approach in my latest push and applied it to .all as well.

I decided to keep the options object for this approach because it's now a boolean flag. {multipleValues: true} is more readable in my opinion than a bare boolean. This isn't a strong opinion and it's easily changed if you disagree 😄
(I could also make a new function to handle multiple values, named assertTypes or assertMultipleTypes)

@sindresorhus sindresorhus changed the title Add getValuesMessage option for assertType Fix assertion message for .all and .any Apr 22, 2021
@sindresorhus sindresorhus merged commit b748ab7 into sindresorhus:main Apr 22, 2021
@sindresorhus
Copy link
Owner

Thanks :)

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.

Falsy values are treated as Array type
2 participants