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

Updated _.isFinite to match ES5 and ES6 spec #830

Merged
merged 1 commit into from Oct 20, 2012
Merged

Updated _.isFinite to match ES5 and ES6 spec #830

merged 1 commit into from Oct 20, 2012

Conversation

TalAter
Copy link
Contributor

@TalAter TalAter commented Oct 20, 2012

The EcmaScript 5 and 6 specs define isFinite as returning false only if ToNumber(object) is NaN, Infinity or -Infinity (a very short and clear read: http://es5.github.com/#x15.1.2.5)

The current implementation of _.isFinite acts as if ToNumber('42') === NaN, and returns false on _.isFinite('42').

This was originally done to combat an "issue" with EcmaScript's ToNumber, which returns true for empty strings (or strings full of nothing but spaces). The problem is that the fix was overzealous, and negates all argument coercion - which is the very heart of isFinite - and acts exactly the opposite of the spec and of the browsers' implementation.

This commit fixes it, while also catching the special gotcha in the spec for empty strings.

// before :
isFinite('12') !== _.isFinite('12') // true !== false
// now:
isFinite('12') === _.isFinite('12') // true === true
// didn't change:
isFinite('')   !== _.isFinite('')   // true !== false

It's also much faster than the current implementation.

The EcmaScript 5 and 6 spec define isFinite as returning false only if
ToNumber(number) is NaN, Infinity, or −Infinity.

Current implementation of _.isFinite acts as if ToNumber('42') is NaN.
This commit fixes it while also catching the special gotcha in the spec
where isFinite(' ') returns true.

It's also much faster than current implementation.
jashkenas added a commit that referenced this pull request Oct 20, 2012
Updated _.isFinite to match ES5 and ES6 spec
@jashkenas jashkenas merged commit 6dc54a5 into jashkenas:master Oct 20, 2012
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