Skip to content

Commit

Permalink
docs: Improve JSDocs by adding examples of usages
Browse files Browse the repository at this point in the history
* Update tuple.ts with examples

* Update formatter.ts with examples

* Updated tuple.ts

---------

Co-authored-by: Priyank Makwana <117025290+PriyankMkwna@users.noreply.github.com>
  • Loading branch information
RuneRogue and RuneRogue authored Sep 6, 2024
1 parent 41ed6e6 commit 8f30b33
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 7 deletions.
46 changes: 42 additions & 4 deletions src/utils/calldata/formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@ import { isBigInt } from '../typed';
import { decodeShortString } from '../shortString';

const guard = {
/**
* Checks if the data is a BigInt (BN) and throws an error if not.
*
* @param {Record<string, any>} data - The data object containing the key to check.
* @param {Record<string, any>} type - The type definition object.
* @param {string} key - The key in the data object to check.
* @throws {Error} If the data type does not match the expected BigInt (BN) type.
*/
isBN: (data: Record<string, any>, type: Record<string, any>, key: string) => {
if (!isBigInt(data[key]))
throw new Error(
Expand All @@ -10,6 +18,15 @@ const guard = {
} to be BN instead it is ${typeof data[key]}`
);
},

/**
* Throws an error for unhandled formatter types.
*
* @param {Record<string, any>} data - The data object containing the key.
* @param {Record<string, any>} type - The type definition object.
* @param {string} key - The key in the data object to check.
* @throws {Error} If the formatter encounters an unknown type.
*/
unknown: (data: Record<string, any>, type: Record<string, any>, key: string) => {
throw new Error(`Unhandled formatter type on ${key}:${type[key]} for data ${key}:${data[key]}`);
},
Expand All @@ -18,16 +35,37 @@ const guard = {
/**
* Formats the given data based on the provided type definition.
*
* @param {any} data - The data to be formatted.
* @param {any} type - The type definition for the data.
* @param {Record<string, any>} data - The data to be formatted.
* @param {Record<string, any>} type - The type definition for the data.
* @param {any} [sameType] - The same type definition to be used (optional).
* @returns - The formatted data.
* @returns {Record<string, any>} The formatted data.
*
* @example
* // Example 1: Formatting a simple object
* const data = { value: '123', name: 'test' };
* const type = { value: 'number', name: 'string' };
* const formatted = formatter(data, type);
* // formatted: { value: 123, name: 'test' }
*
* @example
* // Example 2: Formatting an object with nested structures
* const data = { user: { id: '123', age: '30' }, active: '1' };
* const type = { user: { id: 'number', age: 'number' }, active: 'number' };
* const formatted = formatter(data, type);
* // formatted: { user: { id: 123, age: 30 }, active: 1 }
*
* @example
* // Example 3: Handling arrays in the data object
* const data = { items: ['1', '2', '3'], name: 'test' };
* const type = { items: ['number'], name: 'string' };
* const formatted = formatter(data, type);
* // formatted: { items: [1, 2, 3], name: 'test' }
*/
export default function formatter(
data: Record<string, any>,
type: Record<string, any>,
sameType?: any
) {
): Record<string, any> {
// match data element with type element
return Object.entries(data).reduce(
(acc, [key, value]: [any, any]) => {
Expand Down
26 changes: 23 additions & 3 deletions src/utils/calldata/tuple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,29 @@ function extractCairo1Tuple(type: string): string[] {
}

/**
* Convert tuple string definition into object like definition
* @param type tuple string definition
* @returns object like tuple
* Convert a tuple string definition into an object-like definition.
* Supports both Cairo 0 and Cairo 1 tuple formats.
*
* @param type - The tuple string definition (e.g., "(u8, u8)" or "(x:u8, y:u8)").
* @returns An array of strings or objects representing the tuple components.
*
* @example
* // Cairo 0 Tuple
* const cairo0Tuple = "(u8, u8)";
* const result = extractTupleMemberTypes(cairo0Tuple);
* // result: ["u8", "u8"]
*
* @example
* // Named Cairo 0 Tuple
* const namedCairo0Tuple = "(x:u8, y:u8)";
* const namedResult = extractTupleMemberTypes(namedCairo0Tuple);
* // namedResult: [{ name: "x", type: "u8" }, { name: "y", type: "u8" }]
*
* @example
* // Cairo 1 Tuple
* const cairo1Tuple = "(core::result::Result::<u8, u8>, u8)";
* const cairo1Result = extractTupleMemberTypes(cairo1Tuple);
* // cairo1Result: ["core::result::Result::<u8, u8>", "u8"]
*/
export default function extractTupleMemberTypes(type: string): (string | object)[] {
if (isCairo1Type(type)) {
Expand Down

0 comments on commit 8f30b33

Please sign in to comment.