FIX: Cast all numerical values in reports (PR #8087)

The backend can return some numerical values in report as strings. That results in unexpected order of values when sorting report tables:

invalid value order

GitHub

You’ve signed the CLA, CvX. Thank you! This pull request is ready for review.

Would there be a downside to running parseFloat on each one without the conditional?

We might want to extract this into an helper method so that we don’t have to repeat it 4 times.

Also we have this:

Not sure if we should use this, or check on type.

@eviltrout There are performance differences, and it looks like typeof + parseFloat is the fastest path: https://jsperf.com/number-vs-typeof-vs-parsefloat#results

I can extract that to a helper as @ZogStriP suggested, and then update the mentioned isNumeric, e.g.

export function toNumber(input) {
  return typeof input === "number" ? input : parseFloat(input);
}

export function isNumeric(input) {
  return !isNaN(toNumber(input)) && isFinite(input);
}

Though isNumeric change would be unrelated to this fix and just a micro-optimization.

I see, that does indeed benchmark faster. It is significantly uglier though. I think @ZogStriP’s suggestion of putting it in a helper is a nice compromise. We should get the perf and the code reads nicer.

1 Like