Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/compat/math/parseInt.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,23 @@ describe('parseInt', () => {
expect(parseInt('0x20', object)).toBe(32);
});

it('should coerce `string` to a string before parsing', () => {
// `null`/`undefined` coerce to `''`, not to `'null'`/`'undefined'`.
// @ts-expect-error - unusual usage
expect(parseInt(null)).toBeNaN();
// @ts-expect-error - unusual usage
expect(parseInt(null, 25)).toBeNaN();
// @ts-expect-error - unusual usage
expect(parseInt(undefined)).toBeNaN();
// @ts-expect-error - unusual usage
expect(parseInt(undefined, 16)).toBeNaN();
});

it('should not throw for symbol input and return `NaN`', () => {
// @ts-expect-error - unusual usage
expect(parseInt(Symbol('s'))).toBeNaN();
});

it('should work as an iteratee for methods like `map`', () => {
const strings = ['6', '08', '10'].map(Object);
let actual = strings.map(parseInt);
Expand Down
4 changes: 3 additions & 1 deletion src/compat/math/parseInt.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { toString } from '../util/toString.ts';

/**
* Converts `string` to an integer of the specified radix. If `radix` is undefined or 0, a `radix` of 10 is used unless `string` is a hexadecimal, in which case a `radix` of 16 is used.
*
Expand Down Expand Up @@ -39,5 +41,5 @@ export function parseInt(string: string, radix = 0, guard?: unknown): number {
radix = 0;
}

return Number.parseInt(string, radix);
return Number.parseInt(toString(string), radix);
}