Skip to main content
Version: 6.11.0

Namespace: num

References​

BigNumberish​

Re-exports BigNumberish

Functions​

isHex​

â–¸ isHex(hex): boolean

Test if string is hex-string

Parameters​

NameTypeDescription
hexstringhex-string

Returns​

boolean

true if the input string is a hexadecimal string, false otherwise

Example

const hexString1 = '0x2fd23d9182193775423497fc0c472e156c57c69e4089a1967fb288a2d84e914';
const result1 = isHex(hexString1);
// result1 = true

const hexString2 = '2fd23d9182193775423497fc0c472e156c57c69e4089a1967fb288a2d84e914';
const result2 = isHex(hexString2);
// result2 = false

Defined in​

src/utils/num.ts:26


toBigInt​

â–¸ toBigInt(value): bigint

Convert BigNumberish to bigint

Parameters​

NameTypeDescription
valueBigNumberishvalue to convert

Returns​

bigint

converted value

Example

const str = '123';
const result = toBigInt(str);
// result = 123n

Defined in​

src/utils/num.ts:42


isBigInt​

â–¸ isBigInt(value): value is bigint

Test if value is bigint

Parameters​

NameTypeDescription
valueanyvalue to test

Returns​

value is bigint

true if value is bigint, false otherwise

Example

isBigInt(10n); // true
isBigInt(BigInt('10')); // true
isBigInt(10); // false
isBigInt('10'); // false
isBigInt(null); // false

Defined in​

src/utils/num.ts:60


toHex​

â–¸ toHex(value): string

Convert BigNumberish to hex-string

Parameters​

NameTypeDescription
valueBigNumberishvalue to convert

Returns​

string

converted number in hex-string format

Example

toHex(100); // '0x64'
toHex('200'); // '0xc8'

Defined in​

src/utils/num.ts:75


toStorageKey​

â–¸ toStorageKey(number): string

Convert BigNumberish to storage-key-string

Same as toHex but conforming to the STORAGE_KEY pattern ^0x0[0-7]{1}[a-fA-F0-9]{0,62}$.

A storage key is represented as up to 62 hex digits, 3 bits, and 5 leading zeroes: 0x0 + [0-7] + 62 hex = 0x + 64 hex

Parameters​

NameType
numberBigNumberish

Returns​

string

format: storage-key-string

Defined in​

src/utils/num.ts:93


hexToDecimalString​

â–¸ hexToDecimalString(hex): string

Convert hexadecimal string to decimal string

Parameters​

NameTypeDescription
hexstringhex-string to convert

Returns​

string

converted number in decimal string format

Example

hexToDecimalString('64'); // '100'
hexToDecimalString('c8'); // '200'

Defined in​

src/utils/num.ts:108


cleanHex​

â–¸ cleanHex(hex): string

Remove hex-string leading zeroes and lowercase it

Parameters​

NameTypeDescription
hexstringhex-string

Returns​

string

updated string in hex-string format

Example

cleanHex('0x00023AB'); // '0x23ab'

Defined in​

src/utils/num.ts:122


assertInRange​

â–¸ assertInRange(input, lowerBound, upperBound, inputName?): void

Asserts input is equal to or greater then lowerBound and lower then upperBound.

The inputName parameter is used in the assertion message.

Parameters​

NameTypeDefault valueDescription
inputBigNumberishundefinedValue to check
lowerBoundBigNumberishundefinedLower bound value
upperBoundBigNumberishundefinedUpper bound value
inputNamestring''Name of the input for error message

Returns​

void

Throws

Error if input is out of range

Example

const input1: BigNumberish = 10;
assertInRange(input1, 5, 20, 'value');

const input2: BigNumberish = 25;
assertInRange(input2, 5, 20, 'value');
// throws Error: Message not signable, invalid value length.

Defined in​

src/utils/num.ts:145


bigNumberishArrayToDecimalStringArray​

â–¸ bigNumberishArrayToDecimalStringArray(data): string[]

Convert BigNumberish array to decimal string array

Parameters​

NameTypeDescription
dataBigNumberish[]array of big-numberish elements

Returns​

string[]

array of decimal strings

Example

const data = [100, 200n];
const result = bigNumberishArrayToDecimalStringArray(data);
// result = ['100', '200']

Defined in​

src/utils/num.ts:174


bigNumberishArrayToHexadecimalStringArray​

â–¸ bigNumberishArrayToHexadecimalStringArray(data): string[]

Convert BigNumberish array to hexadecimal string array

Parameters​

NameTypeDescription
dataBigNumberish[]array of big-numberish elements

Returns​

string[]

array of hex-strings

Example

const data = [100, 200n];
const result = bigNumberishArrayToHexadecimalStringArray(data);
// result = ['0x64', '0xc8']

Defined in​

src/utils/num.ts:190


isStringWholeNumber​

â–¸ isStringWholeNumber(str): boolean

Test if string is a whole number (0, 1, 2, 3...)

Parameters​

NameTypeDescription
strstringstring to test

Returns​

boolean

: true if string is a whole number, false otherwise

Example

isStringWholeNumber('100'); // true
isStringWholeNumber('10.0'); // false
isStringWholeNumber('test'); // false

Defined in​

src/utils/num.ts:206


getDecimalString​

â–¸ getDecimalString(str): string

Convert string to decimal string

Parameters​

NameTypeDescription
strstringstring to convert

Returns​

string

converted string in decimal format

Throws

str needs to be a number string in hex or whole number format

Example

const result = getDecimalString('0x1a');
// result = "26"

const result2 = getDecimalString('Hello');
// throws Error: "Hello needs to be a hex-string or whole-number-string"

Defined in​

src/utils/num.ts:225


getHexString​

â–¸ getHexString(str): string

Convert string to hexadecimal string

Parameters​

NameTypeDescription
strstringstring to convert

Returns​

string

converted hex-string

Throws

str needs to be a number string in hex or whole number format

Example

const result = getHexString('123');
// result = "0x7b"

const result2 = getHexString('Hello');
// throws Error: Hello needs to be a hex-string or whole-number-string

Defined in​

src/utils/num.ts:250


getHexStringArray​

â–¸ getHexStringArray(array): string[]

Convert string array to hex-string array

Parameters​

NameTypeDescription
arraystring[]array of string elements

Returns​

string[]

array of converted elements in hex-string format

Example

const data = ['100', '200', '0xaa'];
const result = getHexStringArray(data);
// result = ['0x64', '0xc8', '0xaa']

Defined in​

src/utils/num.ts:272


toCairoBool​

â–¸ toCairoBool(value): string

Convert boolean to "0" or "1"

Parameters​

NameTypeDescription
valuebooleanThe boolean value to be converted.

Returns​

string

Returns true if the value is a number, otherwise returns false.

Example

const result = toCairoBool(true);
// result ="1"

const result2 = toCairoBool(false);
// result2 = "0"

Defined in​

src/utils/num.ts:290


hexToBytes​

â–¸ hexToBytes(str): Uint8Array

Convert hex-string to an array of Bytes (Uint8Array)

Parameters​

NameTypeDescription
strstringhex-string

Returns​

Uint8Array

array containing the converted elements

Throws

str must be a hex-string

Example

let result;

result = hexToBytes('0x64');
// result = [100]

result = hexToBytes('test');
// throws Error: test needs to be a hex-string

Defined in​

src/utils/num.ts:311


addPercent​

â–¸ addPercent(number, percent): bigint

Adds a percentage amount to the value

Parameters​

NameTypeDescription
numberBigNumberishvalue to be modified
percentnumberinteger as percent ex. 50 for 50%

Returns​

bigint

modified value

Example

addPercent(100, 50); // 150n
addPercent(100, 100); // 200n
addPercent(200, 50); // 300n
addPercent(200, -50); // 100n
addPercent(200, -100); // 0n
addPercent(200, -150); // -100n

Defined in​

src/utils/num.ts:337


isNumber​

â–¸ isNumber(value): value is number

Check if a value is a number.

Parameters​

NameTypeDescription
valueunknownThe value to check.

Returns​

value is number

Returns true if the value is a number, otherwise returns false.

Returns true if the value is a number, otherwise returns false.

Example

const result = isNumber(123);
// result = true

const result2 = isNumber('123');
// result2 = false

Defined in​

src/utils/num.ts:357


isBoolean​

â–¸ isBoolean(value): value is boolean

Checks if a given value is of boolean type.

Parameters​

NameTypeDescription
valueunknownThe value to check.

Returns​

value is boolean

  • True if the value is of boolean type, false otherwise.

  • True if the value is of boolean type, false otherwise.

Example

const result = isBoolean(true);
// result = true

const result2 = isBoolean(false);
// result2 = false

Defined in​

src/utils/num.ts:376


toHexString​

â–¸ toHexString(value): string

Alias of ToHex

Parameters​

NameType
valueBigNumberish

Returns​

string

Defined in​

src/utils/num.ts:75