Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/hypersoftllc/qc-to_int
A simple JavaScript utility to convert various value to an integer.
https://github.com/hypersoftllc/qc-to_int
javascript-utility
Last synced: 28 days ago
JSON representation
A simple JavaScript utility to convert various value to an integer.
- Host: GitHub
- URL: https://github.com/hypersoftllc/qc-to_int
- Owner: hypersoftllc
- License: isc
- Created: 2017-05-03T22:14:11.000Z (almost 8 years ago)
- Default Branch: develop
- Last Pushed: 2017-06-02T19:54:10.000Z (over 7 years ago)
- Last Synced: 2024-03-24T11:22:01.384Z (11 months ago)
- Topics: javascript-utility
- Language: TypeScript
- Size: 17.6 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# qc-to_int
[![Build Status][travis-svg]][travis-url]
[![Coverage Status][coverage-image]][coverage-url]
[![License][license-image]][license-url]
[![Downloads][downloads-image]][downloads-url][![npm badge][npm-badge-png]][package-url]
A simple JavaScript utility to convert various values to an integer.
**What it does that `parseInt` doesn't**
* Allow a default value to be set instead of returning `NaN`.
* Convert `'-0'` to `0` instead of `-0`.
* Convert strings in scientific notation to the correct value.
- `'1e-4'` is converted to `0` instead of `1`.
* Convert very large numbers written in scientific notation to the correct
value.
- `6.022e23` is converted to `6.022e23` instead of `6`.
* Convert `Number.MIN_VALUE` to `0` instead of `5`.
* Convert `Number.MAX_VALUE` to `Number.MAX_VALUE` instead of `1`.
* Convert `Infinity` to `Infinity` instead of `NaN`.## Installation
```sh
npm install --save qc-to_int
```## Example Usage
```js
import { toInt, toIntOrNull } from 'qc-to_int';toInt('+3.1459'); // 3
toInt('1e4'); // 10000
toInt('-2.6'); // -3
toInt(-2.6); // -3
toInt(); // `undefined`
toIntOrNull(); // `null`
toInt(''); // `''`
toIntOrNull(''); // `null`
toInt('', 0); // `0`
toInt('', { def: 0 }); // `0`
toInt(NaN); // `NaN`
toIntOrNull(NaN); // `null`
toInt(null); // `null`
toIntOrNull(null); // `null`
toInt(undefined); // `undefined`
toIntOrNull(undefined); // `null`
```[coverage-image]: https://coveralls.io/repos/github/hypersoftllc/qc-to_int/badge.svg?branch=master
[coverage-url]: https://coveralls.io/github/hypersoftllc/qc-to_int?branch=master
[downloads-image]: http://img.shields.io/npm/dm/qc-to_int.svg
[downloads-url]: http://npm-stat.com/charts.html?package=qc-to_int
[license-image]: http://img.shields.io/npm/l/qc-to_int.svg
[license-url]: LICENSE
[package-url]: https://npmjs.org/package/qc-to_int
[npm-badge-png]: https://nodei.co/npm/qc-to_int.png?downloads=true&stars=true
[travis-svg]: https://travis-ci.org/hypersoftllc/qc-to_int.svg?branch=master
[travis-url]: https://travis-ci.org/hypersoftllc/qc-to_int