Skip to content

Commit

Permalink
Add support for BigInt (closes dcodeIO#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
trevorah committed Jan 7, 2022
1 parent 088e44e commit e8caf6d
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 83 deletions.
46 changes: 26 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ API
* Long.**fromInt**(value: `number`, unsigned?: `boolean`): `Long`<br />
Returns a Long representing the given 32 bit integer value.

* Long.**fromBigInt**(value: `bigint`, unsigned?: `boolean`): `Long`<br />
Returns a Long representing the given BigInt.

* Long.**fromNumber**(value: `number`, unsigned?: `boolean`): `Long`<br />
Returns a Long representing the given value, provided that it is a finite number. Otherwise, zero is returned.

Expand All @@ -133,19 +136,19 @@ API

### Methods

* Long#**add**(addend: `Long | number | string`): `Long`<br />
* Long#**add**(addend: `Long | number | string | bigint`): `Long`<br />
Returns the sum of this and the specified Long.

* Long#**and**(other: `Long | number | string`): `Long`<br />
* Long#**and**(other: `Long | number | string | bigint`): `Long`<br />
Returns the bitwise AND of this Long and the specified.

* Long#**compare**/**comp**(other: `Long | number | string`): `number`<br />
* Long#**compare**/**comp**(other: `Long | number | string | bigint`): `number`<br />
Compares this Long's value with the specified's. Returns `0` if they are the same, `1` if the this is greater and `-1` if the given one is greater.

* Long#**divide**/**div**(divisor: `Long | number | string`): `Long`<br />
* Long#**divide**/**div**(divisor: `Long | number | string | bigint`): `Long`<br />
Returns this Long divided by the specified.

* Long#**equals**/**eq**(other: `Long | number | string`): `boolean`<br />
* Long#**equals**/**eq**(other: `Long | number | string | bigint`): `boolean`<br />
Tests if this Long's value equals the specified's.

* Long#**getHighBits**(): `number`<br />
Expand All @@ -163,10 +166,10 @@ API
* Long#**getNumBitsAbs**(): `number`<br />
Gets the number of bits needed to represent the absolute value of this Long.

* Long#**greaterThan**/**gt**(other: `Long | number | string`): `boolean`<br />
* Long#**greaterThan**/**gt**(other: `Long | number | string | bigint`): `boolean`<br />
Tests if this Long's value is greater than the specified's.

* Long#**greaterThanOrEqual**/**gte**/**ge**(other: `Long | number | string`): `boolean`<br />
* Long#**greaterThanOrEqual**/**gte**/**ge**(other: `Long | number | string | bigint`): `boolean`<br />
Tests if this Long's value is greater than or equal the specified's.

* Long#**isEven**(): `boolean`<br />
Expand All @@ -184,16 +187,16 @@ API
* Long#**isZero**/**eqz**(): `boolean`<br />
Tests if this Long's value equals zero.

* Long#**lessThan**/**lt**(other: `Long | number | string`): `boolean`<br />
* Long#**lessThan**/**lt**(other: `Long | number | string | bigint`): `boolean`<br />
Tests if this Long's value is less than the specified's.

* Long#**lessThanOrEqual**/**lte**/**le**(other: `Long | number | string`): `boolean`<br />
* Long#**lessThanOrEqual**/**lte**/**le**(other: `Long | number | string | bigint`): `boolean`<br />
Tests if this Long's value is less than or equal the specified's.

* Long#**modulo**/**mod**/**rem**(divisor: `Long | number | string`): `Long`<br />
* Long#**modulo**/**mod**/**rem**(divisor: `Long | number | string | bigint`): `Long`<br />
Returns this Long modulo the specified.

* Long#**multiply**/**mul**(multiplier: `Long | number | string`): `Long`<br />
* Long#**multiply**/**mul**(multiplier: `Long | number | string | bigint`): `Long`<br />
Returns the product of this and the specified Long.

* Long#**negate**/**neg**(): `Long`<br />
Expand All @@ -208,28 +211,28 @@ API
* Long#**countTrailingZeros**/**ctz**(): `number`<br />
Returns count trailing zeros of this Long.

* Long#**notEquals**/**neq**/**ne**(other: `Long | number | string`): `boolean`<br />
* Long#**notEquals**/**neq**/**ne**(other: `Long | number | string | bigint`): `boolean`<br />
Tests if this Long's value differs from the specified's.

* Long#**or**(other: `Long | number | string`): `Long`<br />
* Long#**or**(other: `Long | number | string | bigint`): `Long`<br />
Returns the bitwise OR of this Long and the specified.

* Long#**shiftLeft**/**shl**(numBits: `Long | number | string`): `Long`<br />
* Long#**shiftLeft**/**shl**(numBits: `Long | number | string | bigint`): `Long`<br />
Returns this Long with bits shifted to the left by the given amount.

* Long#**shiftRight**/**shr**(numBits: `Long | number | string`): `Long`<br />
* Long#**shiftRight**/**shr**(numBits: `Long | number | string | bigint`): `Long`<br />
Returns this Long with bits arithmetically shifted to the right by the given amount.

* Long#**shiftRightUnsigned**/**shru**/**shr_u**(numBits: `Long | number | string`): `Long`<br />
* Long#**shiftRightUnsigned**/**shru**/**shr_u**(numBits: `Long | number | string | bigint`): `Long`<br />
Returns this Long with bits logically shifted to the right by the given amount.

* Long#**rotateLeft**/**rotl**(numBits: `Long | number | string`): `Long`<br />
* Long#**rotateLeft**/**rotl**(numBits: `Long | number | string | bigint`): `Long`<br />
Returns this Long with bits rotated to the left by the given amount.

* Long#**rotateRight**/**rotr**(numBits: `Long | number | string`): `Long`<br />
* Long#**rotateRight**/**rotr**(numBits: `Long | number | string | bigint`): `Long`<br />
Returns this Long with bits rotated to the right by the given amount.

* Long#**subtract**/**sub**(subtrahend: `Long | number | string`): `Long`<br />
* Long#**subtract**/**sub**(subtrahend: `Long | number | string | bigint`): `Long`<br />
Returns the difference of this and the specified Long.

* Long#**toBytes**(le?: `boolean`): `number[]`<br />
Expand All @@ -244,6 +247,9 @@ API
* Long#**toInt**(): `number`<br />
Converts the Long to a 32 bit integer, assuming it is a 32 bit integer.

* Long#**toBigInt**(): `bigint`<br />
Converts the Long to a BigInt.

* Long#**toNumber**(): `number`<br />
Converts the Long to a the nearest floating-point representation of this value (double, 53 bit mantissa).

Expand All @@ -256,7 +262,7 @@ API
* Long#**toUnsigned**(): `Long`<br />
Converts this Long to unsigned.

* Long#**xor**(other: `Long | number | string`): `Long`<br />
* Long#**xor**(other: `Long | number | string | bigint`): `Long`<br />
Returns the bitwise XOR of this Long and the given one.

WebAssembly support
Expand Down
72 changes: 41 additions & 31 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ declare class Long {
*/
static fromInt(value: number, unsigned?: boolean): Long;

/**
* Returns a Long representing the given BigInt.
*/
static fromBigInt(value: bigint, unsigned?: boolean): Long;

/**
* Returns a Long representing the given value, provided that it is a finite number. Otherwise, zero is returned.
*/
Expand Down Expand Up @@ -102,47 +107,47 @@ declare class Long {
/**
* Converts the specified value to a Long.
*/
static fromValue(val: Long | number | string | { low: number, high: number, unsigned: boolean }, unsigned?: boolean): Long;
static fromValue(val: Long | number | string | bigint | { low: number, high: number, unsigned: boolean }, unsigned?: boolean): Long;

/**
* Returns the sum of this and the specified Long.
*/
add(addend: number | Long | string): Long;
add(addend: number | Long | string | bigint): Long;

/**
* Returns the bitwise AND of this Long and the specified.
*/
and(other: Long | number | string): Long;
and(other: Long | number | string | bigint): Long;

/**
* Compares this Long's value with the specified's.
*/
compare(other: Long | number | string): number;
compare(other: Long | number | string | bigint): number;

/**
* Compares this Long's value with the specified's.
*/
comp(other: Long | number | string): number;
comp(other: Long | number | string | bigint): number;

/**
* Returns this Long divided by the specified.
*/
divide(divisor: Long | number | string): Long;
divide(divisor: Long | number | string | bigint): Long;

/**
* Returns this Long divided by the specified.
*/
div(divisor: Long | number | string): Long;
div(divisor: Long | number | string | bigint): Long;

/**
* Tests if this Long's value equals the specified's.
*/
equals(other: Long | number | string): boolean;
equals(other: Long | number | string | bigint): boolean;

/**
* Tests if this Long's value equals the specified's.
*/
eq(other: Long | number | string): boolean;
eq(other: Long | number | string | bigint): boolean;

/**
* Gets the high 32 bits as a signed integer.
Expand Down Expand Up @@ -172,27 +177,27 @@ declare class Long {
/**
* Tests if this Long's value is greater than the specified's.
*/
greaterThan(other: Long | number | string): boolean;
greaterThan(other: Long | number | string | bigint): boolean;

/**
* Tests if this Long's value is greater than the specified's.
*/
gt(other: Long | number | string): boolean;
gt(other: Long | number | string | bigint): boolean;

/**
* Tests if this Long's value is greater than or equal the specified's.
*/
greaterThanOrEqual(other: Long | number | string): boolean;
greaterThanOrEqual(other: Long | number | string | bigint): boolean;

/**
* Tests if this Long's value is greater than or equal the specified's.
*/
gte(other: Long | number | string): boolean;
gte(other: Long | number | string | bigint): boolean;

/**
* Tests if this Long's value is greater than or equal the specified's.
*/
ge(other: Long | number | string): boolean;
ge(other: Long | number | string | bigint): boolean;

/**
* Tests if this Long's value is even.
Expand Down Expand Up @@ -227,52 +232,52 @@ declare class Long {
/**
* Tests if this Long's value is less than the specified's.
*/
lessThan(other: Long | number | string): boolean;
lessThan(other: Long | number | string | bigint): boolean;

/**
* Tests if this Long's value is less than the specified's.
*/
lt(other: Long | number | string): boolean;
lt(other: Long | number | string | bigint): boolean;

/**
* Tests if this Long's value is less than or equal the specified's.
*/
lessThanOrEqual(other: Long | number | string): boolean;
lessThanOrEqual(other: Long | number | string | bigint): boolean;

/**
* Tests if this Long's value is less than or equal the specified's.
*/
lte(other: Long | number | string): boolean;
lte(other: Long | number | string | bigint): boolean;

/**
* Tests if this Long's value is less than or equal the specified's.
*/
le(other: Long | number | string): boolean;
le(other: Long | number | string | bigint): boolean;

/**
* Returns this Long modulo the specified.
*/
modulo(other: Long | number | string): Long;
modulo(other: Long | number | string | bigint): Long;

/**
* Returns this Long modulo the specified.
*/
mod(other: Long | number | string): Long;
mod(other: Long | number | string | bigint): Long;

/**
* Returns this Long modulo the specified.
*/
rem(other: Long | number | string): Long;
rem(other: Long | number | string | bigint): Long;

/**
* Returns the product of this and the specified Long.
*/
multiply(multiplier: Long | number | string): Long;
multiply(multiplier: Long | number | string | bigint): Long;

/**
* Returns the product of this and the specified Long.
*/
mul(multiplier: Long | number | string): Long;
mul(multiplier: Long | number | string | bigint): Long;

/**
* Negates this Long's value.
Expand Down Expand Up @@ -312,22 +317,22 @@ declare class Long {
/**
* Tests if this Long's value differs from the specified's.
*/
notEquals(other: Long | number | string): boolean;
notEquals(other: Long | number | string | bigint): boolean;

/**
* Tests if this Long's value differs from the specified's.
*/
neq(other: Long | number | string): boolean;
neq(other: Long | number | string | bigint): boolean;

/**
* Tests if this Long's value differs from the specified's.
*/
ne(other: Long | number | string): boolean;
ne(other: Long | number | string | bigint): boolean;

/**
* Returns the bitwise OR of this Long and the specified.
*/
or(other: Long | number | string): Long;
or(other: Long | number | string | bigint): Long;

/**
* Returns this Long with bits shifted to the left by the given amount.
Expand Down Expand Up @@ -387,18 +392,23 @@ declare class Long {
/**
* Returns the difference of this and the specified Long.
*/
subtract(subtrahend: number | Long | string): Long;
subtract(subtrahend: number | Long | string | bigint): Long;

/**
* Returns the difference of this and the specified Long.
*/
sub(subtrahend: number | Long | string): Long;
sub(subtrahend: number | Long | string | bigint): Long;

/**
* Converts the Long to a 32 bit integer, assuming it is a 32 bit integer.
*/
toInt(): number;

/**
* Converts the Long to a BigInt.
*/
toBigInt(): bigint;

/**
* Converts the Long to a the nearest floating-point representation of this value (double, 53 bit mantissa).
*/
Expand Down Expand Up @@ -440,7 +450,7 @@ declare class Long {
/**
* Returns the bitwise XOR of this Long and the given one.
*/
xor(other: Long | number | string): Long;
xor(other: Long | number | string | bigint): Long;
}

export = Long; // compatible with `import Long from "long"`
Loading

0 comments on commit e8caf6d

Please sign in to comment.