Skip to content

Commit ffeacf5

Browse files
add roleBytes utility for contract role usage
1 parent 9760615 commit ffeacf5

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { type Hex, isHex } from "../hex.js";
2+
3+
/**
4+
* Calculates the byte size of a Hex string or Uint8Array.
5+
* If the value is a Hex string, it accounts for the leading "0x" prefix.
6+
* @param value The Hex string or Uint8Array.
7+
* @returns The byte size of the value.
8+
* @example
9+
* ```ts
10+
* import { byteSize } from "thirdweb/utils";
11+
* const size = byteSize("0x1a4");
12+
* console.log(size); // 2
13+
* ```
14+
*/
15+
export function byteSize(value: Hex | Uint8Array) {
16+
if (isHex(value, { strict: false })) {
17+
return Math.ceil((value.length - 2) / 2);
18+
}
19+
return value.length;
20+
}

0 commit comments

Comments
 (0)