Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ljharb/safe-bigint
Safely create a BigInt from a numerical string, even one larger than MAX_SAFE_INTEGER.
https://github.com/ljharb/safe-bigint
bigint ecmascript integer javascript max safe
Last synced: 17 days ago
JSON representation
Safely create a BigInt from a numerical string, even one larger than MAX_SAFE_INTEGER.
- Host: GitHub
- URL: https://github.com/ljharb/safe-bigint
- Owner: ljharb
- License: mit
- Created: 2024-03-01T00:24:59.000Z (11 months ago)
- Default Branch: main
- Last Pushed: 2024-12-18T18:31:14.000Z (about 2 months ago)
- Last Synced: 2025-01-08T17:05:04.570Z (25 days ago)
- Topics: bigint, ecmascript, integer, javascript, max, safe
- Language: JavaScript
- Homepage:
- Size: 45.9 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# safe-bigint [![Version Badge][npm-version-svg]][package-url]
[![github actions][actions-image]][actions-url]
[![coverage][codecov-image]][codecov-url]
[![dependency status][deps-svg]][deps-url]
[![dev dependency status][dev-deps-svg]][dev-deps-url]
[![License][license-image]][license-url]
[![Downloads][downloads-image]][downloads-url][![npm badge][npm-badge-png]][package-url]
Safely create a BigInt from a numerical string, even one larger than `MAX_SAFE_INTEGER`.
Note that the implementation in node versions that lack the `exports` field, or bundlers/tooling that does not understand it, will use `Function`, which in browsers, may not work due to CSP controls.
However, the modern version (which modern node and bundlers/tooling should all detect) just exports the `BigInt` constructor.
Feel free to alias this package with `module.exports = BigInt` or `export default BigInt` in your build process configuration.## Example
```js
const safeBigInt = require('safe-bigint');
const assert = require('assert');
const { satisfies } = require('semver');assert.equal(9007199254740991n, safeBigInt(9007199254740991));
assert.equal(9007199254740992n, safeBigInt(9007199254740992));if (satisfies(process.version, '10.4 - 10.8')) {
assert.throws(
() => BigInt(9007199254740992),
RangeError
);
}
```## Tests
Simply clone the repo, `npm install`, and run `npm test`[package-url]: https://npmjs.org/package/safe-bigint
[npm-version-svg]: https://versionbadg.es/ljharb/safe-bigint.svg
[deps-svg]: https://david-dm.org/ljharb/safe-bigint.svg
[deps-url]: https://david-dm.org/ljharb/safe-bigint
[dev-deps-svg]: https://david-dm.org/ljharb/safe-bigint/dev-status.svg
[dev-deps-url]: https://david-dm.org/ljharb/safe-bigint#info=devDependencies
[npm-badge-png]: https://nodei.co/npm/safe-bigint.png?downloads=true&stars=true
[license-image]: https://img.shields.io/npm/l/safe-bigint.svg
[license-url]: LICENSE
[downloads-image]: https://img.shields.io/npm/dm/safe-bigint.svg
[downloads-url]: https://npm-stat.com/charts.html?package=safe-bigint
[codecov-image]: https://codecov.io/gh/ljharb/safe-bigint/branch/main/graphs/badge.svg
[codecov-url]: https://app.codecov.io/gh/ljharb/safe-bigint/
[actions-image]: https://img.shields.io/endpoint?url=https://github-actions-badge-u3jn4tfpocch.runkit.sh/ljharb/safe-bigint
[actions-url]: https://github.com/ljharb/safe-bigint/actions