I want to write a function that looks like this:
function getValuesGreaterThan(num, values);
The input comes from a system call to find . -maxdepth 1 -type f -printf '%C@\n'
, an array of strings like [..., "1643764147.7500000000", "1643764147.7600000000", "1643764147.7700000000", ...]
. The function will return an array of the values.
Although the input is an array of strings, that doesn't mean it has to stay that way: if the easiest solution to this is to convert it to a different type (e.g., a number
), that's fine. The same goes for the return type--all that matters is the correct subset is returned in the same order.
But, I want to make sure the output of getValuesGreaterThan
isn't affected by precision issues or coercion quirks.
I've learned that JavaScript has a BigInt type, but I can't find anything about a BigReal
or a BigDecimal
, etc. I'm not even sure if I need to use these; perhaps the localeCompare
will work as expected on these strings?
let getValuesGreaterThan = function (num, values) {
return values.filter((v) => v.localeCompare(num) >= 0)
}
console.log(getValuesGreaterThan("1643764147.7600000000", ["1643764147.7500000000", "1643764147.7600000000", "1643764147.7700000000"]))
It seems to work, but no matter how many experiments I think of, there could be inputs that break it.
How do I compare unix timestamps in JavaScript?