The lodash method _.parseInt exported as a standalone module with enhanced ES5-compliant string-to-integer conversion.
npx @tessl/cli install tessl/npm-lodash.parseint@4.0.0lodash.parseint provides the lodash method _.parseInt as a standalone Node.js module. It offers an enhanced, ES5-compliant implementation of parseInt with better whitespace handling, automatic hexadecimal detection, and improved behavior compared to the native JavaScript parseInt function.
npm install lodash.parseintconst parseInt = require('lodash.parseint');For ES6 modules:
import parseInt from 'lodash.parseint';const parseInt = require('lodash.parseint');
// Basic usage - enhanced ES5-compliant behavior
parseInt('08'); // => 8 (no octal interpretation)
parseInt('10', 2); // => 2 (binary conversion)
parseInt('FF', 16); // => 255 (hexadecimal)
parseInt('0xFF'); // => 255 (auto-detected hex)
// Better whitespace handling (including BOM)
parseInt(' 42 '); // => 42 (trims whitespace)
// Safer for use as iteratee
['6', '08', '10'].map(parseInt); // => [6, 8, 10] (not [6, NaN, 2])Converts a string to an integer of the specified radix with enhanced ES5-compliant behavior.
/**
* Converts string to an integer of the specified radix. If radix is
* undefined or 0, a radix of 10 is used unless value is a
* hexadecimal, in which case a radix of 16 is used.
*
* Note: This method aligns with the ES5 implementation of parseInt.
*
* @param {string} string - The string to convert
* @param {number} [radix=10] - The radix to interpret value by
* @param {Object} [guard] - Enables use as an iteratee for methods like _.map
* @returns {number} Returns the converted integer
*/
function parseInt(string, radix, guard);Key Features:
parseInt, avoiding legacy octal interpretationguard parameter prevents issues when used with Array.map()Parameters:
string (string): The string to convert to an integer. Null and undefined are converted to empty string first.radix (number, optional): The radix (base) to interpret the value by. Defaults to 10, or 16 for hexadecimal strings. Must be between 2 and 36.guard (Object, optional): Internal parameter that enables safe use as an iteratee function. When truthy, forces radix to 0.Returns:
NaN if the string cannot be parsed.Usage Examples:
const parseInt = require('lodash.parseint');
// Standard decimal conversion
parseInt('42'); // => 42
parseInt('42.8'); // => 42 (stops at decimal)
// Radix-specific conversions
parseInt('1010', 2); // => 10 (binary)
parseInt('FF', 16); // => 255 (hexadecimal)
parseInt('77', 8); // => 63 (octal)
// Automatic hexadecimal detection
parseInt('0xFF'); // => 255 (auto-detected as hex)
parseInt('0x10'); // => 16 (auto-detected as hex)
// Enhanced whitespace handling (including BOM characters)
parseInt(' 123 '); // => 123 (trims spaces)
parseInt('\uFEFF456'); // => 456 (handles BOM character - fixes Chrome issue)
// Safe iteratee usage (avoids Array.map index issue)
['6', '08', '10'].map(parseInt); // => [6, 8, 10]
['6', '08', '10'].map((x) => parseInt(x)); // => [6, 8, 10]
// Edge cases
parseInt(''); // => NaN
parseInt('abc'); // => NaN
parseInt('123abc'); // => 123 (stops at non-numeric)
parseInt(null); // => NaN
parseInt(undefined); // => NaNError Handling:
The function returns NaN for invalid inputs rather than throwing errors:
parseInt('not-a-number'); // => NaN
parseInt({}); // => NaN
parseInt([]); // => NaN