ramda-adjunct icon indicating copy to clipboard operation
ramda-adjunct copied to clipboard

toFinite

Open char0n opened this issue 5 years ago • 4 comments

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.

char0n avatar Jan 07 '20 20:01 char0n

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

mellero avatar Sep 16 '20 18:09 mellero

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 avatar Sep 20 '20 19:09 char0n

@mellero I'd say that https://github.com/char0n/ramda-adjunct/pull/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 https://github.com/char0n/ramda-adjunct/pull/1454

char0n avatar Oct 01 '20 10:10 char0n

Sure I'll get on that asap

mellero avatar Oct 01 '20 22:10 mellero