Skip to content

Commit

Permalink
feat(protocol-parser): add getFieldIndex to internal exports (#3046)
Browse files Browse the repository at this point in the history
Co-authored-by: Kevin Ingersoll <[email protected]>
  • Loading branch information
karooolis and holic authored Aug 20, 2024
1 parent 2bbec22 commit 43ba187
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/protocol-parser/src/exports/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
18 changes: 18 additions & 0 deletions packages/protocol-parser/src/getFieldIndex.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { isDynamicAbiType, isStaticAbiType } from "@latticexyz/schema-type/internal";
import { ValueSchema } from "./common";

export function getFieldIndex<valueSchema extends ValueSchema>(
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);
}

0 comments on commit 43ba187

Please sign in to comment.