Skip to content

Commit fb1a7eb

Browse files
authored
feat: add TypeScript types for bigint.js (#21)
1 parent b89afd6 commit fb1a7eb

File tree

3 files changed

+66
-1
lines changed

3 files changed

+66
-1
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,13 +298,26 @@ That function will have state while `stream = true` is used.
298298

299299
### `@exodus/bytes/bigint.js`
300300

301+
Convert between BigInt and Uint8Array
302+
301303
```js
302304
import { fromBigInt, toBigInt } from '@exodus/bytes/bigint.js'
303305
```
304306

305307
#### `fromBigInt(bigint, { length, format = 'uint8' })`
308+
309+
Convert a BigInt to a Uint8Array or Buffer
310+
311+
The output bytes are in big-endian format.
312+
313+
Throws if the BigInt is negative or cannot fit into the specified length.
314+
306315
#### `toBigInt(arr)`
307316

317+
Convert a Uint8Array or Buffer to a BigInt
318+
319+
The bytes are interpreted as a big-endian unsigned integer.
320+
308321
### `@exodus/bytes/hex.js`
309322

310323
Implements Base16 from [RFC4648](https://datatracker.ietf.org/doc/html/rfc4648)

bigint.d.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* Convert between BigInt and Uint8Array
3+
*
4+
* ```js
5+
* import { fromBigInt, toBigInt } from '@exodus/bytes/bigint.js'
6+
* ```
7+
*
8+
* @module @exodus/bytes/bigint.js
9+
*/
10+
11+
/// <reference types="node" />
12+
13+
import type { OutputFormat, Uint8ArrayBuffer } from './array.js';
14+
15+
/**
16+
* Options for converting BigInt to bytes
17+
*/
18+
export interface FromBigIntOptions {
19+
/** The length in bytes of the output array */
20+
length: number;
21+
/** Output format (default: 'uint8') */
22+
format?: OutputFormat;
23+
}
24+
25+
/**
26+
* Convert a BigInt to a Uint8Array or Buffer
27+
*
28+
* The output bytes are in big-endian format.
29+
*
30+
* Throws if the BigInt is negative or cannot fit into the specified length.
31+
*
32+
* @param x - The BigInt to convert (must be non-negative)
33+
* @param options - Conversion options
34+
* @returns The converted bytes in big-endian format
35+
*/
36+
export function fromBigInt(x: bigint, options: { length: number; format?: 'uint8' }): Uint8ArrayBuffer;
37+
export function fromBigInt(x: bigint, options: { length: number; format: 'buffer' }): Buffer;
38+
export function fromBigInt(x: bigint, options: FromBigIntOptions): Uint8ArrayBuffer | Buffer;
39+
40+
/**
41+
* Convert a Uint8Array or Buffer to a BigInt
42+
*
43+
* The bytes are interpreted as a big-endian unsigned integer.
44+
*
45+
* @param arr - The bytes to convert
46+
* @returns The BigInt representation
47+
*/
48+
export function toBigInt(arr: ArrayBufferView): bigint;

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@
9090
"/base64.d.ts",
9191
"/bech32.js",
9292
"/bigint.js",
93+
"/bigint.d.ts",
9394
"/encoding-browser.js",
9495
"/encoding-browser.browser.js",
9596
"/encoding-browser.native.js",
@@ -140,7 +141,10 @@
140141
"default": "./base64.js"
141142
},
142143
"./bech32.js": "./bech32.js",
143-
"./bigint.js": "./bigint.js",
144+
"./bigint.js": {
145+
"types": "./bigint.d.ts",
146+
"default": "./bigint.js"
147+
},
144148
"./hex.js": {
145149
"types": "./hex.d.ts",
146150
"node": "./hex.node.js",

0 commit comments

Comments
 (0)