From 43ba187d456cd1a02cfa14c96ff68cdbeb76a6d6 Mon Sep 17 00:00:00 2001 From: Karolis Ramanauskas Date: Tue, 20 Aug 2024 16:02:58 +0300 Subject: [PATCH] feat(protocol-parser): add `getFieldIndex` to internal exports (#3046) Co-authored-by: Kevin Ingersoll --- .../protocol-parser/src/exports/internal.ts | 1 + packages/protocol-parser/src/getFieldIndex.ts | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+) create mode 100644 packages/protocol-parser/src/getFieldIndex.ts diff --git a/packages/protocol-parser/src/exports/internal.ts b/packages/protocol-parser/src/exports/internal.ts index 6f2d68af6b..80e3d54bd0 100644 --- a/packages/protocol-parser/src/exports/internal.ts +++ b/packages/protocol-parser/src/exports/internal.ts @@ -26,6 +26,7 @@ export * from "../staticDataLength"; export * from "../valueSchemaToFieldLayoutHex"; export * from "../valueSchemaToHex"; +export * from "../getFieldIndex"; export * from "../getKeySchema"; export * from "../getValueSchema"; export * from "../getSchemaTypes"; diff --git a/packages/protocol-parser/src/getFieldIndex.ts b/packages/protocol-parser/src/getFieldIndex.ts new file mode 100644 index 0000000000..88c6fb47b0 --- /dev/null +++ b/packages/protocol-parser/src/getFieldIndex.ts @@ -0,0 +1,18 @@ +import { isDynamicAbiType, isStaticAbiType } from "@latticexyz/schema-type/internal"; +import { ValueSchema } from "./common"; + +export function getFieldIndex( + valueSchema: valueSchema, + fieldName: keyof valueSchema & string, +): number { + const fieldNames = [ + ...Object.entries(valueSchema) + .filter(([, fieldType]) => isStaticAbiType(fieldType)) + .map(([fieldName]) => fieldName), + ...Object.entries(valueSchema) + .filter(([, fieldType]) => isDynamicAbiType(fieldType)) + .map(([fieldName]) => fieldName), + ]; + + return fieldNames.indexOf(fieldName); +}