@@ -14,32 +14,141 @@ import { CairoUint256 } from '../cairoDataTypes/uint256';
1414import { CairoUint512 } from '../cairoDataTypes/uint512' ;
1515
1616// Intended for internal usage, maybe should be exported somewhere else and not exported to utils
17+ /**
18+ * Checks if the given name ends with "_len".
19+ *
20+ * @param {string } name - The name to be checked.
21+ * @returns - True if the name ends with "_len", false otherwise.
22+ */
1723export const isLen = ( name : string ) => / _ l e n $ / . test ( name ) ;
24+ /**
25+ * Checks if a given type is felt.
26+ *
27+ * @param {string } type - The type to check.
28+ * @returns - True if the type is felt, false otherwise.
29+ */
1830export const isTypeFelt = ( type : string ) => type === 'felt' || type === 'core::felt252' ;
31+ /**
32+ * Checks if the given type is an array type.
33+ *
34+ * @param {string } type - The type to check.
35+ * @returns - `true` if the type is an array type, `false` otherwise.
36+ */
1937export const isTypeArray = ( type : string ) =>
2038 / \* / . test ( type ) ||
2139 type . startsWith ( 'core::array::Array::' ) ||
2240 type . startsWith ( 'core::array::Span::' ) ;
41+ /**
42+ * Checks if the given type is a tuple type.
43+ *
44+ * @param {string } type - The type to be checked.
45+ * @returns - `true` if the type is a tuple type, otherwise `false`.
46+ */
2347export const isTypeTuple = ( type : string ) => / ^ \( .* \) $ / i. test ( type ) ;
48+ /**
49+ * Checks whether a given type is a named tuple.
50+ *
51+ * @param {string } type - The type to be checked.
52+ * @returns - True if the type is a named tuple, false otherwise.
53+ */
2454export const isTypeNamedTuple = ( type : string ) => / \( .* \) / i. test ( type ) && type . includes ( ':' ) ;
55+ /**
56+ * Checks if a given type is a struct.
57+ *
58+ * @param {string } type - The type to check for existence.
59+ * @param {AbiStructs } structs - The collection of structs to search in.
60+ * @returns - True if the type exists in the structs, false otherwise.
61+ */
2562export const isTypeStruct = ( type : string , structs : AbiStructs ) => type in structs ;
63+ /**
64+ * Checks if a given type is an enum.
65+ *
66+ * @param {string } type - The type to check.
67+ * @param {AbiEnums } enums - The enumeration to search in.
68+ * @returns - True if the type exists in the enumeration, otherwise false.
69+ */
2670export const isTypeEnum = ( type : string , enums : AbiEnums ) => type in enums ;
71+ /**
72+ * Determines if the given type is an Option type.
73+ *
74+ * @param {string } type - The type to check.
75+ * @returns - True if the type is an Option type, false otherwise.
76+ */
2777export const isTypeOption = ( type : string ) => type . startsWith ( 'core::option::Option::' ) ;
78+ /**
79+ * Checks whether a given type starts with 'core::result::Result::'.
80+ *
81+ * @param {string } type - The type to check.
82+ * @returns - True if the type starts with 'core::result::Result::', false otherwise.
83+ */
2884export const isTypeResult = ( type : string ) => type . startsWith ( 'core::result::Result::' ) ;
85+ /**
86+ * Checks if the given value is a valid Uint type.
87+ *
88+ * @param {string } type - The value to check.
89+ * @returns - Returns true if the value is a valid Uint type, otherwise false.
90+ */
2991export const isTypeUint = ( type : string ) => Object . values ( Uint ) . includes ( type as Uint ) ;
3092// Legacy Export
93+ /**
94+ * Checks if the given type is `uint256`.
95+ *
96+ * @param {string } type - The type to be checked.
97+ * @returns - Returns true if the type is `uint256`, otherwise false.
98+ */
3199export const isTypeUint256 = ( type : string ) => CairoUint256 . isAbiType ( type ) ;
100+ /**
101+ * Checks if the given type is a literal type.
102+ *
103+ * @param {string } type - The type to check.
104+ * @returns - True if the type is a literal type, false otherwise.
105+ */
32106export const isTypeLiteral = ( type : string ) => Object . values ( Literal ) . includes ( type as Literal ) ;
107+ /**
108+ * Checks if the given type is a boolean type.
109+ *
110+ * @param {string } type - The type to be checked.
111+ * @returns - Returns true if the type is a boolean type, otherwise false.
112+ */
33113export const isTypeBool = ( type : string ) => type === 'core::bool' ;
114+ /**
115+ * Checks if the provided type is equal to 'core::starknet::contract_address::ContractAddress'.
116+ * @param {string } type - The type to be checked.
117+ * @returns - true if the type matches 'core::starknet::contract_address::ContractAddress', false otherwise.
118+ */
34119export const isTypeContractAddress = ( type : string ) =>
35120 type === 'core::starknet::contract_address::ContractAddress' ;
121+ /**
122+ * Determines if the given type is an Ethereum address type.
123+ *
124+ * @param {string } type - The type to check.
125+ * @returns - Returns true if the given type is 'core::starknet::eth_address::EthAddress', otherwise false.
126+ */
36127export const isTypeEthAddress = ( type : string ) =>
37128 type === 'core::starknet::eth_address::EthAddress' ;
129+ /**
130+ * Checks if the given type is 'core::bytes_31::bytes31'.
131+ *
132+ * @param {string } type - The type to check.
133+ * @returns - True if the type is 'core::bytes_31::bytes31', false otherwise.
134+ */
38135export const isTypeBytes31 = ( type : string ) => type === 'core::bytes_31::bytes31' ;
136+ /**
137+ * Checks if the given type is equal to the 'core::byte_array::ByteArray'.
138+ *
139+ * @param {string } type - The type to check.
140+ * @returns - True if the given type is equal to 'core::byte_array::ByteArray', false otherwise.
141+ */
39142export const isTypeByteArray = ( type : string ) => type === 'core::byte_array::ByteArray' ;
40143export const isTypeSecp256k1Point = ( type : string ) =>
41144 type === 'core::starknet::secp256k1::Secp256k1Point' ;
42145export const isCairo1Type = ( type : string ) => type . includes ( '::' ) ;
146+ /**
147+ * Retrieves the array type from the given type string.
148+ *
149+ * @param {string } type - The type string.
150+ * @returns - The array type.
151+ */
43152export const getArrayType = ( type : string ) => {
44153 if ( isCairo1Type ( type ) ) {
45154 return type . substring ( type . indexOf ( '<' ) + 1 , type . lastIndexOf ( '>' ) ) ;
0 commit comments