1- import { assertSize } from "./helpers/assert-size.js" ;
2- import type { Hex } from "./helpers/is-hex.js" ;
3- import { trim } from "./helpers/trim.js" ;
4- import { hexToBigInt , hexToNumber , uint8ArrayToHex } from "./hex.js" ;
1+ import * as ox__Bytes from "ox/Bytes" ;
2+ import { type Hex , uint8ArrayToHex } from "./hex.js" ;
53
64export type FromBytesParameters <
75 TTo extends "string" | "hex" | "bigint" | "number" | "boolean" ,
@@ -86,17 +84,10 @@ export function bytesToBigInt(
8684 bytes : Uint8Array ,
8785 opts : BytesToBigIntOpts = { } ,
8886) : bigint {
89- if ( typeof opts . size !== "undefined" ) {
90- assertSize ( bytes , { size : opts . size } ) ;
91- }
92- const hex = uint8ArrayToHex ( bytes , opts ) ;
93- return hexToBigInt ( hex , opts ) ;
87+ return ox__Bytes . toBigInt ( bytes , opts ) ;
9488}
9589
96- export type BytesToBoolOpts = {
97- /** Size of the bytes. */
98- size ?: number ;
99- } ;
90+ export type BytesToBoolOpts = ox__Bytes . toBoolean . Options ;
10091
10192/**
10293 * Converts a byte array to a boolean value.
@@ -117,16 +108,7 @@ export function bytesToBool(
117108 bytes_ : Uint8Array ,
118109 opts : BytesToBoolOpts = { } ,
119110) : boolean {
120- let bytes = bytes_ ;
121- if ( typeof opts . size !== "undefined" ) {
122- assertSize ( bytes , { size : opts . size } ) ;
123- bytes = trim ( bytes ) ;
124- }
125-
126- if ( bytes . length > 1 || ( bytes [ 0 ] && bytes [ 0 ] > 1 ) ) {
127- throw new Error ( `Invalid boolean representation: ${ bytes } ` ) ;
128- }
129- return Boolean ( bytes [ 0 ] ) ;
111+ return ox__Bytes . toBoolean ( bytes_ , opts ) ;
130112}
131113
132114export type BytesToNumberOpts = BytesToBigIntOpts ;
@@ -149,17 +131,10 @@ export function bytesToNumber(
149131 bytes : Uint8Array ,
150132 opts : BytesToNumberOpts = { } ,
151133) : number {
152- if ( typeof opts . size !== "undefined" ) {
153- assertSize ( bytes , { size : opts . size } ) ;
154- }
155- const hex = uint8ArrayToHex ( bytes , opts ) ;
156- return hexToNumber ( hex , opts ) ;
134+ return ox__Bytes . toNumber ( bytes , opts ) ;
157135}
158136
159- export type BytesToStringOpts = {
160- /** Size of the bytes. */
161- size ?: number ;
162- } ;
137+ export type BytesToStringOpts = ox__Bytes . toString . Options ;
163138
164139/**
165140 * Converts an array of bytes to a string using UTF-8 encoding.
@@ -179,10 +154,5 @@ export function bytesToString(
179154 bytes_ : Uint8Array ,
180155 opts : BytesToStringOpts = { } ,
181156) : string {
182- let bytes = bytes_ ;
183- if ( typeof opts . size !== "undefined" ) {
184- assertSize ( bytes , { size : opts . size } ) ;
185- bytes = trim ( bytes , { dir : "right" } ) ;
186- }
187- return new TextDecoder ( ) . decode ( bytes ) ;
157+ return ox__Bytes . toString ( bytes_ , opts ) ;
188158}
0 commit comments