# @leptonite/parse-int-strict

> strict replacement for parseInt

Latest version **1.0.0** (published 2022-06-02) · MIT license · 0 weekly downloads

## Install

```sh
npm install @leptonite/parse-int-strict
pnpm add @leptonite/parse-int-strict
yarn add @leptonite/parse-int-strict
bun add @leptonite/parse-int-strict
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2022-06-02 |
| First published | 2021-09-11 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 8.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Daniel Faber |
| Maintainers | leptonite |

## Links

- npm: https://www.npmjs.com/package/@leptonite/parse-int-strict
- Repository: https://github.com/leptonite/parse-int-strict
- Homepage: https://github.com/leptonite/parse-int-strict#readme
- Issues: https://github.com/leptonite/parse-int-strict/issues
- npm.io page: https://npm.io/package/@leptonite/parse-int-strict

## Recent versions

- 1.0.0 (latest) — 2022-06-02
- 0.1.0 — 2021-09-11

## README

@leptonite/parse-int-strict
===========================

`@leptonite/parse-int-strict` provides the function `parseIntStrict` which parses a string into an integer.
It is much stricter than `parseInt` which means it rejects a lot of inputs that `parseInt` accepts.


Motivation
----------

I used to use `parseInt(someString)` or `parseInt(someString, 10)` to parse strings containing integral numbers.
And I check if parsing has been successful by checking if the result is NaN via `Number.isNaN(result)`.

But some day I was suprised about the behavior of `parseInt`: It happily acceps inputs that do not look like integral numbers to me.
`parseInt(someString)` seems to work like “if `someString` is not exactly an integral number let’s just parse some part of `someString` that is”,
but I want “if `someString` is not exactly an integral number let’s just reject it by returning `NaN`”.

While `Number(someString)` is stricter than `parseInt(someString, 10)` in some cases it’s not an alternative because it parses fractional numbers.

Examples:

|                             |                   `s` |       `parseInt(s)` |   `parseInt(s, 10)` |         `Number(s)` |
|-----------------------------|----------------------:|--------------------:|--------------------:|--------------------:|
| fractional numbers          |               `'0.5'` |                 `0` |                 `0` |               `0.5` |
| trailing non-digits         |                `'1a'` |                 `1` |                 `1` |               `NaN` |
| trailing non-digits / hex   |              `'0xDF'` |               `223` |                 `0` |               `223` |
| scientific notation         |               `'1e2'` |                 `1` |                 `1` |               `100` |
| leading whitespace          |        `'\t \n \r 1'` |                 `1` |                 `1` |                 `1` |
| `> Number.MAX_SAFE_INTEGER` |  `'9007199254740993'` |  `9007199254740992` |  `9007199254740992` |  `9007199254740992` |
| `< Number.MIN_SAFE_INTEGER` | `'-9007199254740993'` | `-9007199254740992` | `-9007199254740992` | `-9007199254740992` |

Although this behavior matches the spec it is just not what I want.


parseIntStrict
--------------

`parseIntStrict(someString)` is a stricter replacement for `parseInt(someString, 10)`.
It returns `NaN` in all examples above.
There are in fact even more cases where it returns `NaN`.

To be more specific:

* `parseIntStrict(someString)` returns an integral number (let’s call it `int`) if and only if all of the following is true:
  * `someString` is exactly equal to `int.toString()`.
  * `int` is within the range [`Number.MIN_SAFE_INTEGER` .. `Number.MAX_SAFE_INTEGER`].
* In all other cases `parseIntStrict(someString)` returns `NaN`.
* If `someString` is not of type `string`, than the behavior of `parseIntStrict(someString)` is not defined.

It follows in particular:

* `parseIntStrict(someString)` always returns either an integral number or `NaN`, it never throws.
* If `parseIntStrict(someString)` returns an integral number, than it returns the same number as `parseInt(someString, 10)`.

---
_Source: https://npm.io/package/@leptonite/parse-int-strict · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
