locutus
Version:
Locutus other languages' standard libraries to JavaScript for fun and educational purposes
26 lines (25 loc) • 1.01 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Atoi = Atoi;
function Atoi(s) {
// discuss at: https://locutus.io/golang/strconv/Atoi
// parity verified: Go 1.23
// original by: Kevin van Zonneveld (https://kvz.io)
// note 1: Parses a decimal string and returns the integer value.
// note 1: Returns [value, null] on success, [0, error] on failure.
// example 1: Atoi('42')[0]
// returns 1: 42
// example 2: Atoi('-123')[0]
// returns 2: -123
// example 3: Atoi('abc')[0]
// returns 3: 0
const normalized = String(s).trim();
if (!/^-?\d+$/.test(normalized)) {
return [0, new Error('strconv.Atoi: parsing "' + normalized + '": invalid syntax')];
}
const result = parseInt(normalized, 10);
if (isNaN(result)) {
return [0, new Error('strconv.Atoi: parsing "' + normalized + '": invalid syntax')];
}
return [result, null];
}