letIntOrNone function
Converts input to int, returning None on failure.
Returns None for NaN, Infinity, -Infinity, and any value outside
the signed 64-bit integer range — converting these via num.toInt()
would throw UnsupportedError or silently saturate.
Supported types:
Implementation
Option<int> letIntOrNone(dynamic input) {
return letNumOrNone(input).flatMap((n) {
if (n is int) {
// dart2js quirk: every integer-valued double satisfies `is int`,
// including `double.infinity` and out-of-range doubles. On the VM and
// WASM the two types are disjoint, but on dart2js they collapse into a
// single JS-Number — so we apply the same finite + int64-range guards
// we use on a raw double whenever the runtime additionally reports the
// value as a double (true only on dart2js).
if (n is double) {
if (!n.isFinite) return const None();
if (n >= 9223372036854775808.0 || n < -9223372036854775808.0) {
return const None();
}
}
return Some(n);
}
final d = n.toDouble();
if (!d.isFinite) return const None();
// int64 bounds. 9.223372036854776e18 is the smallest double > int64.max,
// and -9.223372036854776e18 is the largest double < int64.min, so we
// reject values at or beyond those magnitudes.
if (d >= 9223372036854775808.0 || d < -9223372036854775808.0) {
return const None();
}
return Some(d.toInt());
});
}