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

parse() should throw instead of returning undefined #123

Open
stefanpl opened this issue Jul 17, 2019 · 3 comments
Open

parse() should throw instead of returning undefined #123

stefanpl opened this issue Jul 17, 2019 · 3 comments
Labels

Comments

@stefanpl
Copy link

What's the reasoning behind parse() returning undefined when given invalid input (e.g. '1 secnd')? In my opinion, an error will be much easier to detect and feels syntactically more appropriate – passing an invalid string is an erroneous usage of the function, after all.

I'd totally be willing to draft a PR if there's agreement an error should be thrown.

@drazisil
Copy link

It's unclear to me where this occurs.

return NaN;
appears to be the only possible location, is this still an issue?

@meyfa
Copy link

meyfa commented Aug 12, 2022

The handling of failure cases still seems sub-optimal to me. This is re: #184 (comment).

TL;DR: ms() throws for inputs that are neither of type string nor number; additionally, it throws for empty strings, and for non-finite numbers. For strings that aren't empty but that cannot be parsed, ms() will return NaN (as mentioned above). This complicates error handling unnecessarily.

Given that a major-version release is coming up anyway, I'd recommend to remedy this situation by being consistent: either always throwing, or always returning a specific error value. Additionally, there needs to be documentation in the README on how errors will be indicated to the caller. The TypeScript-specific examples there are unsafe since they don't check for NaN.

Side note: NaN is not a good choice for an error value IMHO because it is also of type number. If the error value were undefined, for example, TypeScript (and IDEs) would remind programmers that they should handle this case. This will prevent many non-obvious bugs and security risks especially when ms() is used for parsing user input.

Perhaps someone at Vercel could comment what they want to do? I'd be happy to contribute any required code/docs changes.

@quezak
Copy link

quezak commented Nov 25, 2022

It's especially confusing that it does throw when passed an empty string:

> ms('')
[redacted]/node_modules/ms/index.js:34
  throw new Error(
  ^

Uncaught Error: val is not a non-empty string or a valid number. val=""

And then, despite the error message above, it's not thrown when val is not a valid number:

> ms('wat')
undefined

Could we have a fix to make these cases consistent?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants