Package Exports
- bigint-conversion
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (bigint-conversion) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
bigint-conversion
Convert to/from non-negative integers represented with ES-2020 native JS implementation of BigInt from/to:
Buffer
(node.js) orArrayBuffer|TypedArray
(native js),- hex
string
, - utf8-encoded text
string
.
It provides a common interface for the conversions that works for both node.js and native javascript.
Note that there is not a directly visible
TypedArray()
constructor, but a set of typed array ones:Int8Array()
,Uint8Array()
,Uint8ClampedArray()
,Int16Array()
,Uint16Array()
,Int32Array()
,Uint32Array()
,Float32Array()
,Float64Array()
,BigInt64Array()
,BigUint64Array()
.
Installation
bigint-conversion is distributed for web browsers and/or webviews supporting BigInt as an ES6 module or an IIFE file; and for Node.js as a CJS module.
bigint-conversion can be imported to your project with npm
:
npm install bigint-conversion
NPM installation defaults to the ES6 module for browsers and the CJS one for Node.js. For web browsers, you can also directly download the IIFE bundle or the ESM bundle from the repository.
Import your module as :
- Node.js
const bigintConversion = require('bigint-conversion') ... // your code here
- JavaScript native or TypeScript project
import * as bigintConversion from 'bigint-conversion' ... // your code here
BigInt is ES-2020. In order to use it with TypeScript you should set
lib
(and probably alsotarget
andmodule
) toesnext
intsconfig.json
. - JavaScript native browser ES6 mod
<script type="module"> import * as bigintConversion from 'lib/index.browser.bundle.mod.js' // Use you actual path to the broser mod bundle ... // your code here </script>
- JavaScript native browser IIFE
<script src="../../lib/index.browser.bundle.iife.js"></script> <!-- Use you actual path to the browser bundle --> <script> ... // your code here </script>
API reference documentation
bigintToBuf(a, [returnArrayBuffer]) ⇒ ArrayBuffer
| Buffer
Converts an arbitrary-size non-negative bigint to an ArrayBuffer or a Buffer (default for Node.js)
Kind: global function
Returns: ArrayBuffer
| Buffer
- An ArrayBuffer or a Buffer with a binary representation of the input bigint
Throws:
RangeError
a should be a non-negative integer. Negative values are not supported
Param | Type | Default | Description |
---|---|---|---|
a | bigint |
||
[returnArrayBuffer] | boolean |
false |
In Node JS forces the output to be an ArrayBuffer instead of a Buffer (default). |
bufToBigint(buf) ⇒ bigint
Converts an ArrayBuffer, TypedArray or Buffer (node.js) to a bigint
Kind: global function
Returns: bigint
- A BigInt
Param | Type |
---|---|
buf | ArrayBuffer | TypedArray | Buffer |
bigintToHex(a) ⇒ str
Converts a non-negative bigint to a hexadecimal string
Kind: global function
Returns: str
- A hexadecimal representation of the input bigint
Throws:
RangeError
a should be a non-negative integer. Negative values are not supported
Param | Type |
---|---|
a | bigint |
hexToBigint(hexStr) ⇒ bigint
Converts a hexadecimal string to a bigint
Kind: global function
Returns: bigint
- A BigInt
Param | Type |
---|---|
hexStr | string |
bigintToText(a) ⇒ string
Converts a non-negative bigint representing a binary array of utf-8 encoded text to a string of utf-8 text
Kind: global function
Returns: string
- A string text with utf-8 encoding
Throws:
RangeError
a should be a non-negative integer. Negative values are not supported
Param | Type | Description |
---|---|---|
a | bigint |
A non-negative bigint representing a binary array of utf-8 encoded text. |
textToBigint(text) ⇒ bigint
Converts a utf-8 string to a bigint (from its binary representaion)
Kind: global function
Returns: bigint
- A bigint representing a binary array of the input utf-8 encoded text
Param | Type | Description |
---|---|---|
text | string |
A string text with utf-8 encoding |
bufToText(buf) ⇒ string
Converts an ArrayBuffer, TypedArray or Buffer (in Node.js) containing utf-8 encoded text to a string of utf-8 text
Kind: global function
Returns: string
- A string text with utf-8 encoding
Param | Type | Description |
---|---|---|
buf | ArrayBuffer | TypedArray | Buffer |
A buffer containing utf-8 encoded text |
textToBuf(str, [returnArrayBuffer]) ⇒ ArrayBuffer
| Buffer
Converts a string of utf-8 encoded text to an ArrayBuffer or a Buffer (default in Node.js)
Kind: global function
Returns: ArrayBuffer
| Buffer
- An ArrayBuffer or a Buffer containing the utf-8 encoded text
Param | Type | Default | Description |
---|---|---|---|
str | string |
A string of text (with utf-8 encoding) | |
[returnArrayBuffer] | boolean |
false |
In Node JS forces the output to be an ArrayBuffer instead of a Buffer (default). |
bufToHex(buf) ⇒ string
Returns the hexadecimal representation of a buffer.
Kind: global function
Returns: string
- A string with a hexadecimal representation of the input buffer
Param | Type |
---|---|
buf | ArrayBuffer | TypedArray | Buffer |
hexToBuf(hexStr, [returnArrayBuffer]) ⇒ ArrayBuffer
| Buffer
Converts a hexadecimal string to a buffer
Kind: global function
Returns: ArrayBuffer
| Buffer
- An ArrayBuffer or a Buffer
Param | Type | Default | Description |
---|---|---|---|
hexStr | string |
A string representing a number with hexadecimal notation | |
[returnArrayBuffer] | boolean |
false |
In Node JS forces the output to be an ArrayBuffer instead of a Buffer (default). |
TypedArray : Int8Array
| Uint8Array
| Uint8ClampedArray
| Int16Array
| Uint16Array
| Int32Array
| Uint32Array
| Float32Array
| Float64Array
| BigInt64Array
| BigUint64Array
A TypedArray object describes an array-like view of an underlying binary data buffer.
Kind: global typedef