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

toFinite #1260

Open
char0n opened this issue Jan 7, 2020 · 4 comments
Open

toFinite #1260

char0n opened this issue Jan 7, 2020 · 4 comments

Comments

@char0n
Copy link
Owner

char0n commented Jan 7, 2020

Is your feature request related to a problem? Please describe.

Converts value to a finite number.

Describe the solution you'd like

toFinite(3.2); // => 3.2
toFinite(Number.MIN_VALUE); // => 5e-324
toFinite(Infinity); // => 1.7976931348623157e+308
toFinite('3.2'); // => 3.2

Possible implementation:

const toFinite = curry1((value) {
  if (!value) return value === 0 ? value : 0;

  const result = toNumber(value);

  if (!isFinite(result)) {
    const sign = (value < 0 ? -1 : 1);
    return sign * Number.MAX_SAFE_INTEGER;
  }
  
  return result === result ? result : 0;
});

Describe alternatives you've considered

--

Additional context

Currently blocked by toNumber issue.

@mellero
Copy link

mellero commented Sep 16, 2020

Working on this now, just waiting on #788. Question about the functionality: Should Infinity values return MAX_VALUE /MIN_VALUE or MAX_SAFE_INTEGER/MIN_SAFE_INTEGER? Should all NaN values be converted to the max value, or to 0?

Thanks

@char0n
Copy link
Owner Author

char0n commented Sep 20, 2020

Code review for #788 has been provided, will take probably couple of days to merge it. Regarding your questions:

Should Infinity values return MAX_VALUE /MIN_VALUE or MAX_SAFE_INTEGER/MIN_SAFE_INTEGER?

we need to use MAX_VALUE/MIN_VALUE, proof lies in:

Number.isFinite(Number.MAX_VALUE);
isFinite(Number.MAX_VALUE);

Should all NaN values be converted to the max value, or to 0?

to 0 as proposed in possible implementation

@char0n
Copy link
Owner Author

char0n commented Oct 1, 2020

@mellero I'd say that #1454 seems like a dead PR (no response from author), so to unblock this issue feel free to issue another PR for #788 and take a look at my code review comments on current PR #1454

@mellero
Copy link

mellero commented Oct 1, 2020

Sure I'll get on that asap

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

No branches or pull requests

2 participants