-
Notifications
You must be signed in to change notification settings - Fork 36
feat(core): Add bytesLen and bytesLenUnsafe utilities (#353)
#392
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 @@ | ||
| --- | ||
| "@ckb-ccc/core": minor | ||
| --- | ||
|
|
||
| Add `bytesLen` and `bytesLenUnsafe` utilities |
This file contains hidden or 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,71 @@ | ||
| import { describe, expect, test } from "vitest"; | ||
| import { bytesLen, bytesLenUnsafe, hexFrom } from "./index.js"; | ||
|
|
||
| describe("hexFrom", () => { | ||
| const cases: [string, Parameters<typeof hexFrom>[0], string][] = [ | ||
| ["normalized hex", "0x1234", "0x1234"], | ||
| ["empty hex", "0x", "0x"], | ||
| ["odd-length prefixed hex", "0x123", "0x0123"], | ||
| ["hex without prefix", "1234", "0x1234"], | ||
| ["odd-length hex without prefix", "123", "0x0123"], | ||
| ["uppercase hex", "0xABCD", "0xabcd"], | ||
| ["Uint8Array", new Uint8Array([0x12, 0x34]), "0x1234"], | ||
| ["ArrayBuffer", new Uint8Array([0x12, 0x34, 0x56]).buffer, "0x123456"], | ||
| ["number array", [0x12, 0x34, 0x56], "0x123456"], | ||
| ]; | ||
|
|
||
| cases.forEach(([name, input, expected]) => | ||
| test(`returns ${expected} for ${name}`, () => { | ||
| expect(hexFrom(input)).toBe(expected); | ||
| }), | ||
| ); | ||
|
|
||
| test("throws for invalid hex string", () => { | ||
| expect(() => hexFrom("0xzz")).toThrow("Invalid bytes 0xzz"); | ||
| }); | ||
|
|
||
| test("throws for invalid byte values", () => { | ||
| expect(() => hexFrom([256])).toThrow("Invalid bytes [256]"); | ||
| }); | ||
| }); | ||
|
|
||
| describe("bytesLen", () => { | ||
| const cases: [string, Parameters<typeof bytesLen>[0], number][] = [ | ||
| ["normalized hex", "0x1234", 2], | ||
| ["empty hex", "0x", 0], | ||
| ["odd-length hex", "0x123", 2], | ||
| ["hex without prefix", "123", 2], | ||
| ["uppercase hex", "0xABCD", 2], | ||
| ["Uint8Array", new Uint8Array([1, 2, 3]), 3], | ||
| ["ArrayBuffer", new Uint8Array([1, 2, 3, 4]).buffer, 4], | ||
| ["number array", [1, 2, 3, 4, 5], 5], | ||
| ]; | ||
|
|
||
| cases.forEach(([name, input, expected]) => | ||
| test(`returns ${expected} for ${name}`, () => { | ||
| expect(bytesLen(input)).toBe(expected); | ||
| }), | ||
| ); | ||
|
|
||
| test("throws for invalid hex string", () => { | ||
| expect(() => bytesLen("0xzz")).toThrow("Invalid bytes 0xzz"); | ||
| }); | ||
|
|
||
| test("throws for invalid byte values", () => { | ||
| expect(() => bytesLen([256])).toThrow("Invalid bytes [256]"); | ||
| }); | ||
| }); | ||
|
|
||
| describe("bytesLenUnsafe", () => { | ||
| const cases: [string, `0x${string}`, number][] = [ | ||
| ["empty hex", "0x", 0], | ||
| ["even-length hex", "0x1234", 2], | ||
| ["odd-length hex", "0x123", 2], | ||
| ]; | ||
|
|
||
| cases.forEach(([name, input, expected]) => | ||
| test(`returns ${expected} for ${name}`, () => { | ||
| expect(bytesLenUnsafe(input)).toBe(expected); | ||
| }), | ||
| ); | ||
| }); |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.