-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Description
Hi
I noticed a potential issue in how XML numeric types are parsed. In particular, this section of code:
if (name === 'int' || name === 'integer' || name === 'short' || name === 'long') {
value = parseInt(text, 10);
}While this works fine for short and int (which are within JavaScript's safe integer range), it can lead to loss of precision for long and integer types.
In XML Schema:
long is typically a 64-bit signed integer, with values up to ±9.22e18
integer is unbounded, and can be arbitrarily large
JavaScript’s Number type cannot safely represent integers beyond 2^53 - 1 (Number.MAX_SAFE_INTEGER). Using parseInt here may result in inaccurate values for large numbers.
For example:
parseInt("9223372036854775807", 10) // returns 9223372036854776000which is incorrect
Maybe, consider using BigInt for parsing long and integer values:
if (name === 'long' || name === 'integer') {
value = BigInt(text);
}
xs:integer (unlimited)
xs:long (64 bit)
xs:unsignedLong (0 … 2⁶⁴ − 1)
xs:negativeInteger, xs:positiveInteger, xs:nonNegativeInteger, xs:nonPositiveInteger (all subset of xs:integer)
xs:decimalThis would ensure large values are parsed accurately.
Let me know what you think — happy to help with a PR if this approach sounds good.
Thanks again!