It seems that I've never had the use case where I exceeded the maximum or minimum integer count. I figure there's a good amount of JavaScript developers that have the same experience.
But good article nonetheless.
It seems that I've never had the use case where I exceeded the maximum or minimum integer count. I figure there's a good amount of JavaScript developers that have the same experience.
But good article nonetheless.
Their JSON API works around that limitation by returning IDs as both "id" (a number that may not be representable in Javascript) and "id_str" (the same number in JS-friendly string form).
String is the best choice in JSON but ultimately only somewhat less wrong (since string transformations won’t yield meaningful results for an ID either). That’s why GraphQL defines a discrete ID scalar.
In terms of identity an ID is never a number, always a unique value. It should be its own type.
Does it note that in the spec? I can't find it.
And after seeing a hundred systems break when IDs started to overflow the 32 bit max, or struggle to handle a change to guids, or alphanumeric security codes, or a harmonization with another systems IDs, or freak out when their numbers get exposed through a URL and it turns out keeping leading-0 formatting has something to say: ... use a damned string.
"00123" and 123 are different. Just use a string.
If the numbers are random (like IDs) you’ll hit it about every 1000th ID. Solution: IDs are strings not numbers.
I can imagine this is an especially important advancement for server side JS. Not something I personally care about tho.
For example, this is an incorrect way to compare numbers:
> 0.1 + 0.2 === 0.3
false
This is the proper way to compare numbers: a = 0.1 + 0.2
b = 0.3
> Math.abs(a - b) < Number.EPSILON
true
This means the difference is smaller than the smallest quantity that can be represented in floating point number, and every JS number is a floating point number.Not knowing about this can cause many issues if you deal with values representing currency.
That kind of equality test should be:
abs( a-b ) < Number.EPSILON * max( abs(a), abs(b) )
In practice I judge the scale of the quantization noise that may accrue and compare the difference to it.It may also be possible to kind of caste precision away by adding a trick value. eg.
a + t == b + t
If a and b are positive, that is somewhat similar to: abs( a-b ) < Number.EPSILON * tA more popular definition is "the smallest number that yields a result different to 1 when added to 1".
This version of adding a rounding value seems to though:
( a-b + rounder ) == rounder
My apologies, be careful out there :)