Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add examples to JsDoc for num.ts file #1100

Merged
merged 6 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
# [6.8.0](https://github.com/starknet-io/starknet.js/compare/v6.7.0...v6.8.0) (2024-04-23)

### Bug Fixes

- starkne types 0.7 ([#1087](https://github.com/starknet-io/starknet.js/issues/1087)) ([b038c76](https://github.com/starknet-io/starknet.js/commit/b038c76fe204746f1d1023c2ad3b46c022f6edbd))
- tslib ([#1068](https://github.com/starknet-io/starknet.js/issues/1068)) ([dd7dc10](https://github.com/starknet-io/starknet.js/commit/dd7dc10c57fc3cc35298c0d584a178666e9cfed1))
- **utils:** fix block identifier ([#1076](https://github.com/starknet-io/starknet.js/issues/1076)) ([0a3499d](https://github.com/starknet-io/starknet.js/commit/0a3499d49751061ceae1a4d6023b34f402376efc))

### Features

- add getGasPrice rpc provider method ([#1056](https://github.com/starknet-io/starknet.js/issues/1056)) ([d396275](https://github.com/starknet-io/starknet.js/commit/d396275348aff9c932d2bb7466b2a55f96214e4e))
- Export function parseCalldataField() ([4d59658](https://github.com/starknet-io/starknet.js/commit/4d596582023f24522c25a1a515ee0246d2eca90a))
- rpc 0.7.1 ([#1071](https://github.com/starknet-io/starknet.js/issues/1071)) ([11dc600](https://github.com/starknet-io/starknet.js/commit/11dc6003c74b6b6d0408b3f5894b5b6739d4bfba))

# [6.7.0](https://github.com/starknet-io/starknet.js/compare/v6.6.6...v6.7.0) (2024-04-03)

### Features
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "starknet",
"version": "6.7.0",
"version": "6.8.0",
"description": "JavaScript library for Starknet",
"main": "dist/index.js",
"module": "dist/index.mjs",
Expand Down
171 changes: 163 additions & 8 deletions src/utils/num.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,75 @@ export type { BigNumberish };
/**
* Test if string is hex-string
* @param hex hex-string
* @returns{boolean} True if the input string is a hexadecimal string, false otherwise
* @example
* ```typescript
* const hexString1 = "0x2fd23d9182193775423497fc0c472e156c57c69e4089a1967fb288a2d84e914";
* const result1 = isHex(hexString1);
* // result1 = true
*
* const hexString2 = "2fd23d9182193775423497fc0c472e156c57c69e4089a1967fb288a2d84e914";
* const result2 = isHex(hexString2);
* // result2 = false
* ```
*/
export function isHex(hex: string): boolean {
return /^0x[0-9a-f]*$/i.test(hex);
}

/**
* Convert BigNumberish to bigint
* @param value BigNumberish value to convert to bigint
* @returns {bigint} Converted bigint value
* @example
* ```typescript
* const bigNumberishValue1: BigNumberish = 1234567890;
* const result1 = toBigInt(bigNumberishValue1);
* // result1 = 1234567890n
* ```
*/
export function toBigInt(value: BigNumberish): bigint {
return BigInt(value);
}

/**
* Test if value is bigint
* @param value Value to test
* @returns {boolean} True if the value is a bigint, false otherwise
* @example
* ```typescript
* const bigIntValue1: bigint = 1234567890n;
* const result1 = isBigint(bigIntValue1);
* // result1 = true
* ```
*/
export function isBigInt(value: any): value is bigint {
return typeof value === 'bigint';
}

/**
* Convert BigNumberish to hex-string
* @param number BigNumberish value to convert to hex-string
* @returns format: hex-string
* @example
* ```typescript
* const bigNumberishValue1: BigNumberish = 1234567890;
* const result1 = toHex(bigNumberishValue1);
* // result = "0x499602d2"
* ```
*/
export function toHex(number: BigNumberish): string {
return addHexPrefix(toBigInt(number).toString(16));
}

/**
* Alias of ToHex
* Alias of toHex
* @returns format: hex-string
* @example
* ```typescript
* const result = toHexString(123);
* // result = "0x7b"
* ```
*/
export const toHexString = toHex;

Expand All @@ -49,7 +89,14 @@ export const toHexString = toHex;
*
* 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`
* @param number BigNumberish value to convert to storage-key-string
* @returns format: storage-key-string
* @example
* ```typescript
* const bigNumberishValue1: BigNumberish = 1234567890;
* const result = toStorageKey(bigNumberishValue1);
* // result = "0x000000000000000000000000000000000000000000000000000000000499602d2"
* ```
*/
export function toStorageKey(number: BigNumberish): string {
const res = addHexPrefix(toBigInt(number).toString(16).padStart(64, '0'));
Expand All @@ -60,23 +107,48 @@ export function toStorageKey(number: BigNumberish): string {
* Convert hexadecimal string to decimal string
* @param hex hex-string
* @returns format: decimal string
* @example
* ```typescript
* const hexString = "0x2fd23d9182193775423497fc0c472e156c57c69e4089a1967fb288a2d84e914";
* const result = hexToDecimalString(hexString);
* // result = "32507161997631881240494522159444018041090212371621416251492750949915267618964";
* ```
*/
export function hexToDecimalString(hex: string): string {
return BigInt(addHexPrefix(hex)).toString(10);
}

/**
* Remove hex string leading zero and lowercase it
* @example '0x01A...' -> '0x1a..'
* Remove hex string leading zero and lowercase it;
* @param hex hex-string
* @returns format: hex-string
* @example
* ```typescript
* const hexString = '0x01A2F3'
* const result = cleanHex(hexString);
* // result = '0x1a2f3'
* ```
*/
export const cleanHex = (hex: string) => hex.toLowerCase().replace(/^(0x)0+/, '$1');

/**
* Asserts input is equal to or greater then lowerBound and lower then upperBound.
* Asserts input is equal to or greater than lowerBound and lower than upperBound.
*
* The `inputName` parameter is used in the assertion message.
* @param input Value to check
* @param lowerBound Lower bound value
* @param upperBound Upper bound value
* @param inputName Name of the input for error message
* @Throws Error if input is out of range
* @example
* ```typescript
* 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.
* ```
*/
export function assertInRange(
input: BigNumberish,
Expand All @@ -97,28 +169,58 @@ export function assertInRange(

/**
* Convert BigNumberish array to decimal string array
* @param rawCalldata Array of BigNumberish values
* @returns format: decimal string array
* @example
* ```typescript
* const bigNumberishArray: BigNumberish[] = [123, "456", 789n];
* const result = bigNumberishArrayToDecimalStringArray(bigNumberishArray);
* // result = ["123", "456", "789"]
* ```
*/
export function bigNumberishArrayToDecimalStringArray(rawCalldata: BigNumberish[]): string[] {
return rawCalldata.map((x) => toBigInt(x).toString(10));
}

/**
* Convert BigNumberish array to hexadecimal string array
* @param rawCalldata Array of BigNumberish values
* @returns format: hex-string array
* @example
* ```typescript
* const bigNumberishArray: BigNumberish[] = [123, "456", 789n];
* const result = bigNumberishArrayToHexadecimalStringArray(bigNumberishArray);
* // result = ["0x7b", "0x1c8", "0x315"]
* ```
*/
export function bigNumberishArrayToHexadecimalStringArray(rawCalldata: BigNumberish[]): string[] {
return rawCalldata.map((x) => toHex(x));
}

/**
* Test if string is whole number (0, 1, 2, 3...)
* @param value The string to be tested.
* @return {boolean} Returns true if the value is a number, otherwise returns false.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@return is not existing in JSDoc.
Only @returns (with a s) is authorized : https://jsdoc.app/tags-returns

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To check everywhere

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed, changed all @return to @returns and conflict resolved

* @example
Copy link
Collaborator

@PhilippeR26 PhilippeR26 Apr 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check if @param is present in all functions including at least one parameter

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added @param to all function where at least one parameter exist

* ```typescript
* const result = isStringWholeNumber("123");
* // result = true
* ```
*/
export const isStringWholeNumber = (value: string) => /^\d+$/.test(value);

/**
* Convert string to decimal string
* @param value The string to be converted.
* @returns format: decimal string
* @example
* ```typescript
* const result = getDecimalString("0x1a");
* // result = "26"
*
* const result2 = getDecimalString("Hello");
* // Throws Error: "Hello need to be hex-string or whole-number-string"
* ```
*/
export function getDecimalString(value: string) {
if (isHex(value)) {
Expand All @@ -132,7 +234,16 @@ export function getDecimalString(value: string) {

/**
* Convert string to hexadecimal string
* @param value The string to be converted.
* @returns format: hex-string
* @example
* ```typescript
* const result = getHexString("123");
* // result = "0x7b"
*
* const result2 = getHexString("Hello");
* // Throws Error: Hello need to be hex-string or whole-number-string
* ```
*/
export function getHexString(value: string) {
if (isHex(value)) {
Expand All @@ -146,20 +257,43 @@ export function getHexString(value: string) {

/**
* Convert string array to hex-string array
* @param value The string array to be converted.
* @returns format: hex-string array
* @example
* ```typescript
* const stringArray: string[] = ["123", "456", "789"];
* const result = getHexStringArray(stringArray);
* // result = ["0x7b", "0x1c8", "0x315"]
* ```
*/
export function getHexStringArray(value: Array<string>) {
return value.map((el) => getHexString(el));
}

/**
* Convert boolean to "0" or "1"
* @param value The boolean value to be converted.
* @return {boolean} Returns true if the value is a number, otherwise returns false.
* @example
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check everywhere that @returns is present.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.
added @returns to every function

* ```typescript
* const result = toCairoBool(true);
* // result ="1"
*
* const result2 = toCairoBool(false);
* // result2 = "0"
* ```
*/
export const toCairoBool = (value: boolean): string => (+value).toString();

/**
* Convert hex-string to an array of Bytes (Uint8Array)
* @param value hex-string
* @param value The hex-string to be converted.
* @return The array of bytes (Uint8Array) corresponding to the hex-string.
* @example
* ```typescript
* const result = hexToBytes("0x123456");
* // result = Uint8Array [ 18, 52, 86 ]
* ```
*/
export function hexToBytes(value: string): Uint8Array {
if (!isHex(value)) throw new Error(`${value} need to be a hex-string`);
Expand All @@ -172,10 +306,15 @@ export function hexToBytes(value: string): Uint8Array {
}

/**
*
* @param number value to be increased
* Increase a give number by specified percentage
* @param number The value to be increased (BigInt or number).
* @param percent integer as percent ex. 50 for 50%
* @returns increased value
* @returns The increased value as a BigInt.
* @example
* ```typescript
* const result = addPercent(100, 50);
* // result = 150n
* ```
*/
export function addPercent(number: BigNumberish, percent: number) {
const bigIntNum = BigInt(number);
Expand All @@ -187,6 +326,14 @@ export function addPercent(number: BigNumberish, percent: number) {
*
* @param {unknown} value - The value to check.
* @return {boolean} Returns true if the value is a number, otherwise returns false.
* @example
* ```typescript
* const result = isNumber(123);
* // result = true
*
* const result2 = isNumber("123");
* // result2 = false
* ```
*/
export function isNumber(value: unknown): value is number {
return typeof value === 'number';
Expand All @@ -197,6 +344,14 @@ export function isNumber(value: unknown): value is number {
*
* @param {unknown} value - The value to check.
* @return {boolean} - True if the value is of boolean type, false otherwise.
* @example
* ```typescript
* const result = isBoolean(true);
* // result = true
*
* const result2 = isBoolean(false);
* // result2 = false
* ```
*/
export function isBoolean(value: unknown): value is boolean {
return typeof value === 'boolean';
Expand Down