-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move some utils to common, clean up (#1068)
* move some utils into common, add linter * migrate things to new TableId * clean up * ugh vscode why * missed a couple return types
- Loading branch information
Showing
57 changed files
with
272 additions
and
192 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
24 changes: 0 additions & 24 deletions
24
examples/minimal/packages/client-phaser/src/layers/network/contractComponents.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"extends": ["../../.eslintrc"], | ||
"rules": { | ||
"@typescript-eslint/explicit-function-return-type": "error" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { describe, it, expect } from "vitest"; | ||
import { TableId } from "./TableId"; | ||
|
||
describe("TableId", () => { | ||
it("can convert to hex string", () => { | ||
const tableId = new TableId("namespace", "name"); | ||
expect(tableId.toHex()).toMatchInlineSnapshot( | ||
'"0x6e616d657370616365000000000000006e616d65000000000000000000000000"' | ||
); | ||
}); | ||
|
||
it("throws when converting namespaces >16 bytes", () => { | ||
const tableId = new TableId("AVeryLongNamespace", "name"); | ||
expect(() => tableId.toHex()).toThrowErrorMatchingInlineSnapshot(` | ||
"Size cannot exceed 16 bytes. Given size: 18 bytes. | ||
Version: [email protected]" | ||
`); | ||
}); | ||
|
||
it("throws when converting names >16 bytes", () => { | ||
const tableId = new TableId("namespace", "AnUnnecessarilyLongName"); | ||
expect(() => tableId.toHex()).toThrowErrorMatchingInlineSnapshot(` | ||
"Size cannot exceed 16 bytes. Given size: 23 bytes. | ||
Version: [email protected]" | ||
`); | ||
}); | ||
|
||
it("can convert from hex string", () => { | ||
const tableId = TableId.fromHex("0x6e616d657370616365000000000000006e616d65000000000000000000000000"); | ||
expect(tableId.namespace).toMatchInlineSnapshot('"namespace"'); | ||
expect(tableId.name).toMatchInlineSnapshot('"name"'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { Hex, stringToHex, hexToString, sliceHex, concatHex } from "viem"; | ||
|
||
export class TableId { | ||
namespace: string; | ||
name: string; | ||
|
||
constructor(namespace: string, name: string) { | ||
this.namespace = namespace; | ||
this.name = name; | ||
} | ||
|
||
toString(): string { | ||
return `TableId<${this.namespace || "[empty]"}:${this.name || "[empty]"}>`; | ||
} | ||
|
||
toHex(): Hex { | ||
return TableId.toHex(this.namespace, this.name); | ||
} | ||
|
||
static toHex(namespace: string, name: string): Hex { | ||
return concatHex([stringToHex(namespace, { size: 16 }), stringToHex(name, { size: 16 })]); | ||
} | ||
|
||
static fromHex(hex: Hex): TableId { | ||
const namespace = hexToString(sliceHex(hex, 0, 16)).replace(/\0+$/, ""); | ||
const name = hexToString(sliceHex(hex, 16, 32)).replace(/\0+$/, ""); | ||
return new TableId(namespace, name); | ||
} | ||
|
||
/** @deprecated Don't use this! This is a temporary hack for v2<>v1 compatibility until we can write v2 client libraries. This is here so it stays close to the formatting of `toString()` above. */ | ||
static parse(tableIdString: string): TableId | null { | ||
const match = tableIdString.match(/^TableId<(.+?):(.+?)>$/); | ||
if (!match) return null; | ||
const [, namespace, name] = match; | ||
return new TableId(namespace === "[empty]" ? "" : namespace, name === "[empty]" ? "" : name); | ||
} | ||
} |
Oops, something went wrong.