import { StaticAbiType } from "./schemaAbiTypes"; import { isStaticAbiType } from "./staticAbiTypes"; export type ArrayAbiType = `${StaticAbiType}[]`; export type FixedArrayAbiType = `${StaticAbiType}[${number}]`; const arrayPattern = /\[\]$/; const fixedArrayPattern = /\[\d+\]$/; export function isArrayAbiType(abiType: unknown): abiType is ArrayAbiType { return ( typeof abiType === "string" && arrayPattern.test(abiType) && isStaticAbiType(abiType.replace(arrayPattern, "")) ); } export function isFixedArrayAbiType(abiType: unknown): abiType is FixedArrayAbiType { return ( typeof abiType === "string" && fixedArrayPattern.test(abiType) && isStaticAbiType(abiType.replace(fixedArrayPattern, "")) ); } export type arrayToStaticAbiType = abiType extends `${infer StaticAbiType}[]` ? StaticAbiType : never; export type fixedArrayToStaticAbiType = abiType extends `${infer StaticAbiType}[${number}]` ? StaticAbiType : never; export function arrayToStaticAbiType(abiType: abiType): arrayToStaticAbiType { return abiType.replace(arrayPattern, "") as arrayToStaticAbiType; } export function fixedArrayToStaticAbiType( abiType: abiType, ): fixedArrayToStaticAbiType { return abiType.replace(fixedArrayPattern, "") as fixedArrayToStaticAbiType; } export type fixedArrayToArray = abiType extends `${infer staticAbiType}[${number}]` ? `${staticAbiType}[]` : never; export function fixedArrayToArray(abiType: abiType): fixedArrayToArray { return abiType.replace(fixedArrayPattern, "[]") as fixedArrayToArray; }