parseIntJs static method
num
parseIntJs(
- dynamic value, [
- int? radix
])
Implementation
static num parseIntJs(dynamic value, [int? radix]) {
if (value is int && radix == null) return value;
var text = value?.toString().trimLeft() ?? '';
if (text.isEmpty) return double.nan;
var sign = 1;
if (text.startsWith('-') || text.startsWith('+')) {
if (text.startsWith('-')) sign = -1;
text = text.substring(1);
}
var effectiveRadix = radix ?? 0;
if (effectiveRadix != 0 && (effectiveRadix < 2 || effectiveRadix > 36)) {
return double.nan;
}
if ((effectiveRadix == 0 || effectiveRadix == 16) &&
(text.startsWith('0x') || text.startsWith('0X'))) {
effectiveRadix = 16;
text = text.substring(2);
}
if (effectiveRadix == 0) effectiveRadix = 10;
var result = 0;
var consumed = false;
for (var i = 0; i < text.length; i++) {
final digit = _digitValue(text.codeUnitAt(i));
if (digit < 0 || digit >= effectiveRadix) break;
consumed = true;
result = result * effectiveRadix + digit;
}
return consumed ? result * sign : double.nan;
}