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.00
# lodash.parseint
1
2
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.
3
4
## Package Information
5
6
- **Package Name**: lodash.parseint
7
- **Package Type**: npm
8
- **Language**: JavaScript
9
- **Installation**: `npm install lodash.parseint`
10
11
## Core Imports
12
13
```javascript
14
const parseInt = require('lodash.parseint');
15
```
16
17
For ES6 modules:
18
19
```javascript
20
import parseInt from 'lodash.parseint';
21
```
22
23
## Basic Usage
24
25
```javascript
26
const parseInt = require('lodash.parseint');
27
28
// Basic usage - enhanced ES5-compliant behavior
29
parseInt('08'); // => 8 (no octal interpretation)
30
parseInt('10', 2); // => 2 (binary conversion)
31
parseInt('FF', 16); // => 255 (hexadecimal)
32
parseInt('0xFF'); // => 255 (auto-detected hex)
33
34
// Better whitespace handling (including BOM)
35
parseInt(' 42 '); // => 42 (trims whitespace)
36
37
// Safer for use as iteratee
38
['6', '08', '10'].map(parseInt); // => [6, 8, 10] (not [6, NaN, 2])
39
```
40
41
## Capabilities
42
43
### parseInt Function
44
45
Converts a string to an integer of the specified radix with enhanced ES5-compliant behavior.
46
47
```javascript { .api }
48
/**
49
* Converts string to an integer of the specified radix. If radix is
50
* undefined or 0, a radix of 10 is used unless value is a
51
* hexadecimal, in which case a radix of 16 is used.
52
*
53
* Note: This method aligns with the ES5 implementation of parseInt.
54
*
55
* @param {string} string - The string to convert
56
* @param {number} [radix=10] - The radix to interpret value by
57
* @param {Object} [guard] - Enables use as an iteratee for methods like _.map
58
* @returns {number} Returns the converted integer
59
*/
60
function parseInt(string, radix, guard);
61
```
62
63
**Key Features:**
64
65
- **ES5 Compliance**: Follows ES5 specification for `parseInt`, avoiding legacy octal interpretation
66
- **Automatic Hex Detection**: Automatically detects hexadecimal strings (0x prefix) and uses radix 16
67
- **Enhanced Whitespace Trimming**: Removes leading/trailing whitespace including BOM (Byte Order Mark) characters that can cause issues in some JavaScript engines
68
- **Safe Iterator Usage**: Third `guard` parameter prevents issues when used with `Array.map()`
69
- **Cross-Environment**: Works consistently across different JavaScript engines
70
71
**Parameters:**
72
73
- `string` (string): The string to convert to an integer. Null and undefined are converted to empty string first.
74
- `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.
75
- `guard` (Object, optional): Internal parameter that enables safe use as an iteratee function. When truthy, forces radix to 0.
76
77
**Returns:**
78
79
- (number): The converted integer. Returns `NaN` if the string cannot be parsed.
80
81
**Usage Examples:**
82
83
```javascript
84
const parseInt = require('lodash.parseint');
85
86
// Standard decimal conversion
87
parseInt('42'); // => 42
88
parseInt('42.8'); // => 42 (stops at decimal)
89
90
// Radix-specific conversions
91
parseInt('1010', 2); // => 10 (binary)
92
parseInt('FF', 16); // => 255 (hexadecimal)
93
parseInt('77', 8); // => 63 (octal)
94
95
// Automatic hexadecimal detection
96
parseInt('0xFF'); // => 255 (auto-detected as hex)
97
parseInt('0x10'); // => 16 (auto-detected as hex)
98
99
// Enhanced whitespace handling (including BOM characters)
100
parseInt(' 123 '); // => 123 (trims spaces)
101
parseInt('\uFEFF456'); // => 456 (handles BOM character - fixes Chrome issue)
102
103
// Safe iteratee usage (avoids Array.map index issue)
104
['6', '08', '10'].map(parseInt); // => [6, 8, 10]
105
['6', '08', '10'].map((x) => parseInt(x)); // => [6, 8, 10]
106
107
// Edge cases
108
parseInt(''); // => NaN
109
parseInt('abc'); // => NaN
110
parseInt('123abc'); // => 123 (stops at non-numeric)
111
parseInt(null); // => NaN
112
parseInt(undefined); // => NaN
113
```
114
115
**Error Handling:**
116
117
The function returns `NaN` for invalid inputs rather than throwing errors:
118
119
```javascript
120
parseInt('not-a-number'); // => NaN
121
parseInt({}); // => NaN
122
parseInt([]); // => NaN
123
```