narze/baht.js

Return `NaN` instead of `false`

Closed this issue · 4 comments

narze commented

As stated in #17, should we respect what Number() did?

image

By the way I think we can return NaN on converting [], too...

Throwing a proper error is better in my opinion, but returning NaN is fine as well. We can ignore the [] case since there is no case in the source code that the argument will be an array. Also, iirc, [] ~= "" so +[] = +"" = 0.

I think returning NaN for the function that returns string is inappropriate. Number() returns NaN because NaN is a reserved number that indicate an invalid number, but it still has number type. It is some sort of in-band signaling. If you want your function to be consistent like Number(), you can use "" (zero-length string) as an error value. By this way, it has the same string type, has a value that can't occur from a valid input and you can detect it with a simple condition. Anyway, JavaScript is a dynamic-typed language, so you can return anything of any type you want. For that reason, using false is fine considering nowadays we have === operator to check for it.

narze commented

that's a good point, maybe we can just use false like before 👌

narze commented

For proper error we can make it opt-in like Baht.throwError = true