or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-lodash.parseint

The lodash method _.parseInt exported as a standalone module with enhanced ES5-compliant string-to-integer conversion.

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/lodash.parseint@4.0.x

To install, run

npx @tessl/cli install tessl/npm-lodash.parseint@4.0.0

index.mddocs/

lodash.parseint

lodash.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.

Package Information

  • Package Name: lodash.parseint
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install lodash.parseint

Core Imports

const parseInt = require('lodash.parseint');

For ES6 modules:

import parseInt from 'lodash.parseint';

Basic Usage

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])

Capabilities

parseInt Function

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:

  • ES5 Compliance: Follows ES5 specification for parseInt, avoiding legacy octal interpretation
  • Automatic Hex Detection: Automatically detects hexadecimal strings (0x prefix) and uses radix 16
  • Enhanced Whitespace Trimming: Removes leading/trailing whitespace including BOM (Byte Order Mark) characters that can cause issues in some JavaScript engines
  • Safe Iterator Usage: Third guard parameter prevents issues when used with Array.map()
  • Cross-Environment: Works consistently across different JavaScript engines

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:

  • (number): The converted integer. 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);      // => NaN

Error Handling:

The function returns NaN for invalid inputs rather than throwing errors:

parseInt('not-a-number');   // => NaN
parseInt({});              // => NaN
parseInt([]);              // => NaN