-
Notifications
You must be signed in to change notification settings - Fork 194
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(common): new utils, truncate table ID parts (#1173)
Co-authored-by: alvarius <alvarius@lattice.xyz>
- Loading branch information
Showing
10 changed files
with
143 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
--- | ||
"@latticexyz/common": minor | ||
--- | ||
|
||
`TableId.toHex()` now truncates name/namespace to 16 bytes each, to properly fit into a `bytes32` hex string. | ||
|
||
Also adds a few utils we'll need in the indexer: | ||
|
||
- `bigIntMin` is similar to `Math.min` but for `bigint`s | ||
- `bigIntMax` is similar to `Math.max` but for `bigint`s | ||
- `bigIntSort` for sorting an array of `bigint`s | ||
- `chunk` to split an array into chunks | ||
- `wait` returns a `Promise` that resolves after specified number of milliseconds |
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,3 @@ | ||
export function bigIntMax(...args: bigint[]): bigint { | ||
return args.reduce((m, e) => (e > m ? e : m)); | ||
} |
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,3 @@ | ||
export function bigIntMin(...args: bigint[]): bigint { | ||
return args.reduce((m, e) => (e < m ? e : m)); | ||
} |
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,3 @@ | ||
export function bigIntSort(a: bigint, b: bigint): -1 | 0 | 1 { | ||
return a < b ? -1 : a > b ? 1 : 0; | ||
} |
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,94 @@ | ||
import { describe, it, expect } from "vitest"; | ||
import { chunk } from "./chunk"; | ||
|
||
describe("chunk", () => { | ||
it("splits an array into chunks", () => { | ||
expect(Array.from(chunk([1, 2, 3, 4, 5, 6, 7, 8, 9], 3))).toMatchInlineSnapshot(` | ||
[ | ||
[ | ||
1, | ||
2, | ||
3, | ||
], | ||
[ | ||
4, | ||
5, | ||
6, | ||
], | ||
[ | ||
7, | ||
8, | ||
9, | ||
], | ||
] | ||
`); | ||
|
||
expect(Array.from(chunk([1, 2, 3, 4, 5, 6, 7, 8, 9], 5))).toMatchInlineSnapshot(` | ||
[ | ||
[ | ||
1, | ||
2, | ||
3, | ||
4, | ||
5, | ||
], | ||
[ | ||
6, | ||
7, | ||
8, | ||
9, | ||
], | ||
] | ||
`); | ||
|
||
expect(Array.from(chunk([1, 2, 3, 4, 5, 6, 7, 8, 9], 8))).toMatchInlineSnapshot(` | ||
[ | ||
[ | ||
1, | ||
2, | ||
3, | ||
4, | ||
5, | ||
6, | ||
7, | ||
8, | ||
], | ||
[ | ||
9, | ||
], | ||
] | ||
`); | ||
|
||
expect(Array.from(chunk([1, 2, 3, 4, 5, 6, 7, 8, 9], 9))).toMatchInlineSnapshot(` | ||
[ | ||
[ | ||
1, | ||
2, | ||
3, | ||
4, | ||
5, | ||
6, | ||
7, | ||
8, | ||
9, | ||
], | ||
] | ||
`); | ||
|
||
expect(Array.from(chunk([1, 2, 3, 4, 5, 6, 7, 8, 9], 10))).toMatchInlineSnapshot(` | ||
[ | ||
[ | ||
1, | ||
2, | ||
3, | ||
4, | ||
5, | ||
6, | ||
7, | ||
8, | ||
9, | ||
], | ||
] | ||
`); | ||
}); | ||
}); |
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,5 @@ | ||
export function* chunk<T>(arr: T[], n: number): Generator<T[], void> { | ||
for (let i = 0; i < arr.length; i += n) { | ||
yield arr.slice(i, i + n); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,4 +1,9 @@ | ||
export * from "./assertExhaustive"; | ||
export * from "./bigIntMax"; | ||
export * from "./bigIntMin"; | ||
export * from "./bigIntSort"; | ||
export * from "./chunk"; | ||
export * from "./curry"; | ||
export * from "./isDefined"; | ||
export * from "./isNotNull"; | ||
export * from "./wait"; |
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,3 @@ | ||
export function wait(ms: number): Promise<void> { | ||
return new Promise((resolve) => setTimeout(resolve, ms)); | ||
} |