Comparing version 0.0.0-20240815T034436 to 0.0.0-20240815T050956
@@ -1,2 +0,2 @@ | ||
export declare const version = "0.0.0-20240815T034436"; | ||
export declare const version = "0.0.0-20240815T050956"; | ||
//# sourceMappingURL=version.d.ts.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.version = void 0; | ||
exports.version = '0.0.0-20240815T034436'; | ||
exports.version = '0.0.0-20240815T050956'; | ||
//# sourceMappingURL=version.js.map |
@@ -9,2 +9,10 @@ import { assertSize } from '../data/assertSize.js'; | ||
type To = 'string' | 'hex' | 'bigint' | 'number' | 'boolean'; | ||
export declare namespace fromBytes { | ||
type Options = { | ||
/** Size of the bytes. */ | ||
size?: number | undefined; | ||
}; | ||
type ReturnType<to extends To> = (to extends 'string' ? string : never) | (to extends 'hex' ? Hex : never) | (to extends 'bigint' ? bigint : never) | (to extends 'number' ? number : never) | (to extends 'boolean' ? boolean : never); | ||
type ErrorType = bytesToBigInt.ErrorType | bytesToBoolean.ErrorType | bytesToNumber.ErrorType | bytesToString.ErrorType | bytesToHex.ErrorType | InvalidTypeErrorType | ErrorType_; | ||
} | ||
/** | ||
@@ -26,11 +34,12 @@ * Decodes {@link Bytes} into a UTF-8 string, {@link Hex}, number, bigint or boolean. | ||
*/ | ||
export declare namespace fromBytes { | ||
export declare function fromBytes<to extends 'string' | 'hex' | 'bigint' | 'number' | 'boolean'>(bytes: Bytes, to: to | To, options?: fromBytes.Options): fromBytes.ReturnType<to>; | ||
export declare namespace bytesToBigInt { | ||
type Options = { | ||
/** Whether or not the number of a signed representation. */ | ||
signed?: boolean | undefined; | ||
/** Size of the bytes. */ | ||
size?: number | undefined; | ||
}; | ||
type ReturnType<to extends To> = (to extends 'string' ? string : never) | (to extends 'hex' ? Hex : never) | (to extends 'bigint' ? bigint : never) | (to extends 'number' ? number : never) | (to extends 'boolean' ? boolean : never); | ||
type ErrorType = bytesToBigInt.ErrorType | bytesToBoolean.ErrorType | bytesToNumber.ErrorType | bytesToString.ErrorType | bytesToHex.ErrorType | InvalidTypeErrorType | ErrorType_; | ||
type ErrorType = bytesToHex.ErrorType | hexToBigInt.ErrorType | ErrorType_; | ||
} | ||
export declare function fromBytes<to extends 'string' | 'hex' | 'bigint' | 'number' | 'boolean'>(bytes: Bytes, to: to | To, options?: fromBytes.Options): fromBytes.ReturnType<to>; | ||
/** | ||
@@ -44,12 +53,10 @@ * Decodes a byte array into a bigint. | ||
*/ | ||
export declare namespace bytesToBigInt { | ||
export declare function bytesToBigInt(bytes: Bytes, options?: bytesToBigInt.Options): bigint; | ||
export declare namespace bytesToBoolean { | ||
type Options = { | ||
/** Whether or not the number of a signed representation. */ | ||
signed?: boolean | undefined; | ||
/** Size of the bytes. */ | ||
size?: number | undefined; | ||
}; | ||
type ErrorType = bytesToHex.ErrorType | hexToBigInt.ErrorType | ErrorType_; | ||
type ErrorType = assertSize.ErrorType | trimLeft.ErrorType | ErrorType_; | ||
} | ||
export declare function bytesToBigInt(bytes: Bytes, options?: bytesToBigInt.Options): bigint; | ||
/** | ||
@@ -63,10 +70,7 @@ * Decodes a byte array into a boolean. | ||
*/ | ||
export declare namespace bytesToBoolean { | ||
type Options = { | ||
/** Size of the bytes. */ | ||
size?: number | undefined; | ||
}; | ||
type ErrorType = assertSize.ErrorType | trimLeft.ErrorType | ErrorType_; | ||
export declare function bytesToBoolean(bytes_: Bytes, options?: bytesToBoolean.Options): boolean; | ||
export declare namespace bytesToNumber { | ||
type Options = bytesToBigInt.Options; | ||
type ErrorType = bytesToHex.ErrorType | hexToNumber.ErrorType | ErrorType_; | ||
} | ||
export declare function bytesToBoolean(bytes_: Bytes, options?: bytesToBoolean.Options): boolean; | ||
/** | ||
@@ -80,7 +84,10 @@ * Decodes a byte array into a number. | ||
*/ | ||
export declare namespace bytesToNumber { | ||
type Options = bytesToBigInt.Options; | ||
type ErrorType = bytesToHex.ErrorType | hexToNumber.ErrorType | ErrorType_; | ||
export declare function bytesToNumber(bytes: Bytes, options?: bytesToNumber.Options): number; | ||
export declare namespace bytesToString { | ||
type Options = { | ||
/** Size of the bytes. */ | ||
size?: number | undefined; | ||
}; | ||
type ErrorType = assertSize.ErrorType | trimRight.ErrorType | ErrorType_; | ||
} | ||
export declare function bytesToNumber(bytes: Bytes, options?: bytesToNumber.Options): number; | ||
/** | ||
@@ -94,11 +101,4 @@ * Decodes a byte array into a UTF-8 string. | ||
*/ | ||
export declare namespace bytesToString { | ||
type Options = { | ||
/** Size of the bytes. */ | ||
size?: number | undefined; | ||
}; | ||
type ErrorType = assertSize.ErrorType | trimRight.ErrorType | ErrorType_; | ||
} | ||
export declare function bytesToString(bytes_: Bytes, options?: bytesToString.Options): string; | ||
export {}; | ||
//# sourceMappingURL=fromBytes.d.ts.map |
@@ -6,2 +6,18 @@ import { assertSize } from '../data/assertSize.js'; | ||
import { bytesToHex } from '../hex/toHex.js'; | ||
/** | ||
* Decodes {@link Bytes} into a UTF-8 string, {@link Hex}, number, bigint or boolean. | ||
* | ||
* @example | ||
* import { Bytes } from 'ox' | ||
* Bytes.to(Bytes.from([1, 164]), 'number') | ||
* // 420 | ||
* | ||
* @example | ||
* import { Bytes } from 'ox' | ||
* Bytes.to( | ||
* Bytes.from([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33]), | ||
* 'string' | ||
* ) | ||
* // 'Hello world' | ||
*/ | ||
export function fromBytes(bytes, to, options = {}) { | ||
@@ -20,2 +36,10 @@ if (to === 'number') | ||
} | ||
/** | ||
* Decodes a byte array into a bigint. | ||
* | ||
* @example | ||
* import { Bytes } from 'ox' | ||
* Bytes.toBigInt(Bytes.from([1, 164])) | ||
* // 420n | ||
*/ | ||
export function bytesToBigInt(bytes, options = {}) { | ||
@@ -28,2 +52,10 @@ const { size } = options; | ||
} | ||
/** | ||
* Decodes a byte array into a boolean. | ||
* | ||
* @example | ||
* import { Bytes } from 'ox' | ||
* Bytes.toBoolean(Bytes.from([1])) | ||
* // true | ||
*/ | ||
export function bytesToBoolean(bytes_, options = {}) { | ||
@@ -40,2 +72,10 @@ const { size } = options; | ||
} | ||
/** | ||
* Decodes a byte array into a number. | ||
* | ||
* @example | ||
* import { Bytes } from 'ox' | ||
* Bytes.toNumber(Bytes.from([1, 164])) | ||
* // 420 | ||
*/ | ||
export function bytesToNumber(bytes, options = {}) { | ||
@@ -49,2 +89,10 @@ const { size } = options; | ||
const decoder = /*#__PURE__*/ new TextDecoder(); | ||
/** | ||
* Decodes a byte array into a UTF-8 string. | ||
* | ||
* @example | ||
* import { Bytes } from 'ox' | ||
* const data = Bytes.toString(Bytes.from([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33])) | ||
* // 'Hello world' | ||
*/ | ||
export function bytesToString(bytes_, options = {}) { | ||
@@ -51,0 +99,0 @@ const { size } = options; |
@@ -9,2 +9,9 @@ import { assertSize } from '../data/assertSize.js'; | ||
import type { Bytes, Hex } from '../types/data.js'; | ||
export declare namespace toBytes { | ||
type Options = { | ||
/** Size of the output bytes. */ | ||
size?: number | undefined; | ||
}; | ||
type ErrorType = numberToBytes.ErrorType | booleanToBytes.ErrorType | hexToBytes.ErrorType | stringToBytes.ErrorType | isBytes.ErrorType | isHex.ErrorType | InvalidTypeErrorType | ErrorType_; | ||
} | ||
/** | ||
@@ -28,3 +35,4 @@ * Encodes an arbitrary value to {@link Bytes}. | ||
*/ | ||
export declare namespace toBytes { | ||
export declare function toBytes(value: string | bigint | number | boolean | Hex | Bytes | readonly number[], options?: toBytes.Options): Bytes; | ||
export declare namespace booleanToBytes { | ||
type Options = { | ||
@@ -34,5 +42,4 @@ /** Size of the output bytes. */ | ||
}; | ||
type ErrorType = numberToBytes.ErrorType | booleanToBytes.ErrorType | hexToBytes.ErrorType | stringToBytes.ErrorType | isBytes.ErrorType | isHex.ErrorType | InvalidTypeErrorType | ErrorType_; | ||
type ErrorType = assertSize.ErrorType | padLeft.ErrorType | ErrorType_; | ||
} | ||
export declare function toBytes(value: string | bigint | number | boolean | Hex | Bytes | readonly number[], options?: toBytes.Options): Bytes; | ||
/** | ||
@@ -51,3 +58,4 @@ * Encodes a boolean value into {@link Bytes}. | ||
*/ | ||
export declare namespace booleanToBytes { | ||
export declare function booleanToBytes(value: boolean, options?: booleanToBytes.Options): Uint8Array; | ||
export declare namespace hexToBytes { | ||
type Options = { | ||
@@ -57,5 +65,4 @@ /** Size of the output bytes. */ | ||
}; | ||
type ErrorType = assertSize.ErrorType | padLeft.ErrorType | ErrorType_; | ||
type ErrorType = assertSize.ErrorType | padRight.ErrorType | ErrorType_; | ||
} | ||
export declare function booleanToBytes(value: boolean, options?: booleanToBytes.Options): Uint8Array; | ||
/** | ||
@@ -79,10 +86,7 @@ * Encodes a hex value into {@link Bytes}. | ||
*/ | ||
export declare namespace hexToBytes { | ||
type Options = { | ||
/** Size of the output bytes. */ | ||
size?: number | undefined; | ||
}; | ||
type ErrorType = assertSize.ErrorType | padRight.ErrorType | ErrorType_; | ||
export declare function hexToBytes(hex_: Hex, options?: hexToBytes.Options): Bytes; | ||
export declare namespace numberToBytes { | ||
type Options = numberToHex.Options; | ||
type ErrorType = numberToHex.ErrorType | hexToBytes.ErrorType | ErrorType_; | ||
} | ||
export declare function hexToBytes(hex_: Hex, options?: hexToBytes.Options): Bytes; | ||
/** | ||
@@ -101,6 +105,2 @@ * Encodes a number value into {@link Bytes}. | ||
*/ | ||
export declare namespace numberToBytes { | ||
type Options = numberToHex.Options; | ||
type ErrorType = numberToHex.ErrorType | hexToBytes.ErrorType | ErrorType_; | ||
} | ||
export declare function numberToBytes(value: bigint | number, options?: numberToBytes.Options | undefined): Uint8Array; | ||
@@ -107,0 +107,0 @@ export declare namespace stringToBytes { |
@@ -8,2 +8,20 @@ import { assertSize } from '../data/assertSize.js'; | ||
import { numberToHex } from '../hex/toHex.js'; | ||
/** | ||
* Encodes an arbitrary value to {@link Bytes}. | ||
* | ||
* @example | ||
* import { Bytes } from 'ox' | ||
* const data = Bytes.from('Hello world') | ||
* // Uint8Array([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33]) | ||
* | ||
* @example | ||
* import { Bytes } from 'ox' | ||
* const data = Bytes.from(420) | ||
* // Uint8Array([1, 164]) | ||
* | ||
* @example | ||
* import { Bytes } from 'ox' | ||
* const data = Bytes.from(420, { size: 4 }) | ||
* // Uint8Array([0, 0, 1, 164]) | ||
*/ | ||
export function toBytes(value, options = {}) { | ||
@@ -24,2 +42,15 @@ if (isBytes(value)) | ||
} | ||
/** | ||
* Encodes a boolean value into {@link Bytes}. | ||
* | ||
* @example | ||
* import { Bytes } from 'ox' | ||
* const data = Bytes.fromBoolean(true) | ||
* // Uint8Array([1]) | ||
* | ||
* @example | ||
* import { Bytes } from 'ox' | ||
* const data = Bytes.fromBoolean(true, { size: 32 }) | ||
* // Uint8Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]) | ||
*/ | ||
export function booleanToBytes(value, options = {}) { | ||
@@ -53,2 +84,20 @@ const { size } = options; | ||
} | ||
/** | ||
* Encodes a hex value into {@link Bytes}. | ||
* | ||
* @example | ||
* import { Hex } from 'ox' | ||
* const data = Hex.toBytes('0x48656c6c6f20776f726c6421') | ||
* // Uint8Array([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33]) | ||
* | ||
* @example | ||
* import { Bytes } from 'ox' | ||
* const data = Bytes.fromHex('0x48656c6c6f20776f726c6421') | ||
* // Uint8Array([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33]) | ||
* | ||
* @example | ||
* import { Bytes } from 'ox' | ||
* const data = Bytes.fromHex('0x48656c6c6f20776f726c6421', { size: 32 }) | ||
* // Uint8Array([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) | ||
*/ | ||
export function hexToBytes(hex_, options = {}) { | ||
@@ -76,2 +125,15 @@ const { size } = options; | ||
} | ||
/** | ||
* Encodes a number value into {@link Bytes}. | ||
* | ||
* @example | ||
* import { Bytes } from 'ox' | ||
* const data = Bytes.fromNumber(420) | ||
* // Uint8Array([1, 164]) | ||
* | ||
* @example | ||
* import { Bytes } from 'ox' | ||
* const data = Bytes.fromNumber(420, { size: 4 }) | ||
* // Uint8Array([0, 0, 1, 164]) | ||
*/ | ||
export function numberToBytes(value, options) { | ||
@@ -78,0 +140,0 @@ const hex = numberToHex(value, options); |
@@ -43,3 +43,2 @@ import { NegativeOffsetError, type NegativeOffsetErrorType, type PositionOutOfBoundsErrorType, type RecursiveReadLimitExceededErrorType } from './errors/cursor.js'; | ||
type StaticCursorErrorType = NegativeOffsetErrorType | RecursiveReadLimitExceededErrorType; | ||
/** @internal */ | ||
export declare namespace createCursor { | ||
@@ -51,4 +50,5 @@ type Config = { | ||
} | ||
/** @internal */ | ||
export declare function createCursor(bytes: Bytes, { recursiveReadLimit }?: createCursor.Config): Cursor; | ||
export {}; | ||
//# sourceMappingURL=cursor.d.ts.map |
@@ -162,2 +162,3 @@ import { NegativeOffsetError, PositionOutOfBoundsError, RecursiveReadLimitExceededError, } from './errors/cursor.js'; | ||
}; | ||
/** @internal */ | ||
export function createCursor(bytes, { recursiveReadLimit = 8_192 } = {}) { | ||
@@ -164,0 +165,0 @@ const cursor = Object.create(staticCursor); |
@@ -5,7 +5,7 @@ import { type SizeOverflowErrorType } from '../errors/data.js'; | ||
import { size } from './size.js'; | ||
/** @internal */ | ||
export declare namespace assertSize { | ||
type ErrorType = size.ErrorType | SizeOverflowErrorType | ErrorType_; | ||
} | ||
/** @internal */ | ||
export declare function assertSize(hexOrBytes: Hex | Bytes, size_: number): void; | ||
//# sourceMappingURL=assertSize.d.ts.map |
import { SizeOverflowError, } from '../errors/data.js'; | ||
import { size } from './size.js'; | ||
/** @internal */ | ||
export function assertSize(hexOrBytes, size_) { | ||
@@ -4,0 +5,0 @@ if (size(hexOrBytes) > size_) |
import type { ErrorType as ErrorType_ } from '../errors/error.js'; | ||
import type { Bytes, Hex } from '../types/data.js'; | ||
export declare namespace concat { | ||
type ReturnType<value extends Hex | Bytes> = value extends Hex ? Hex : Bytes; | ||
type ErrorType = concatBytes.ErrorType | concatHex.ErrorType | ErrorType_; | ||
} | ||
/** | ||
@@ -21,12 +25,6 @@ * Concatenates two or more {@link Bytes} or {@link Hex}. | ||
*/ | ||
export declare namespace concat { | ||
type ReturnType<value extends Hex | Bytes> = value extends Hex ? Hex : Bytes; | ||
type ErrorType = concatBytes.ErrorType | concatHex.ErrorType | ErrorType_; | ||
} | ||
export declare function concat<value extends Hex | Bytes>(...values: readonly value[]): concat.ReturnType<value>; | ||
/** @internal */ | ||
export declare namespace concatBytes { | ||
type ErrorType = ErrorType_; | ||
} | ||
/** @internal */ | ||
export declare namespace concatHex { | ||
@@ -33,0 +31,0 @@ type ErrorType = ErrorType_; |
@@ -0,1 +1,19 @@ | ||
/** | ||
* Concatenates two or more {@link Bytes} or {@link Hex}. | ||
* | ||
* @example | ||
* ```ts | ||
* import { Data } from 'ox' | ||
* const bytes = Data.concat( | ||
* Bytes.from([1, 2, 3]), | ||
* Bytes.from([4, 5, 6]) | ||
* ) | ||
* ``` | ||
* | ||
* @example | ||
* ```ts | ||
* import { Data } from 'ox' | ||
* const hex = Data.concat('0x1234', '0x5678') | ||
* ``` | ||
*/ | ||
export function concat(...values) { | ||
@@ -6,2 +24,3 @@ if (typeof values[0] === 'string') | ||
} | ||
/** @internal */ | ||
function concatBytes(...values) { | ||
@@ -20,2 +39,3 @@ let length = 0; | ||
} | ||
/** @internal */ | ||
function concatHex(...values) { | ||
@@ -22,0 +42,0 @@ return `0x${values.reduce((acc, x) => acc + x.replace('0x', ''), '')}`; |
import type { ErrorType as ErrorType_ } from '../errors/error.js'; | ||
import type { Bytes } from '../types/data.js'; | ||
export declare namespace isBytes { | ||
type ErrorType = ErrorType_; | ||
} | ||
/** | ||
@@ -11,6 +14,3 @@ * Checks if the given value is {@link Bytes}. | ||
*/ | ||
export declare namespace isBytes { | ||
type ErrorType = ErrorType_; | ||
} | ||
export declare function isBytes(value: unknown): value is Bytes; | ||
//# sourceMappingURL=isBytes.d.ts.map |
@@ -0,1 +1,9 @@ | ||
/** | ||
* Checks if the given value is {@link Bytes}. | ||
* | ||
* @example | ||
* import { Bytes } from 'ox' | ||
* Bytes.isBytes('0x') // false | ||
* Bytes.isBytes(Bytes.from([1, 2, 3])) // true | ||
*/ | ||
export function isBytes(value) { | ||
@@ -2,0 +10,0 @@ if (!value) |
@@ -5,2 +5,5 @@ import { toBytes } from '../bytes/toBytes.js'; | ||
import { isHex } from './isHex.js'; | ||
export declare namespace isBytesEqual { | ||
type ErrorType = isHex.ErrorType | toBytes.ErrorType | ErrorType_; | ||
} | ||
/** | ||
@@ -19,6 +22,3 @@ * Checks if two {@link Bytes} or {@link Hex} values are equal. | ||
*/ | ||
export declare namespace isBytesEqual { | ||
type ErrorType = isHex.ErrorType | toBytes.ErrorType | ErrorType_; | ||
} | ||
export declare function isBytesEqual(a_: Bytes | Hex, b_: Bytes | Hex): boolean; | ||
//# sourceMappingURL=isBytesEqual.d.ts.map |
import { equalBytes } from '@noble/curves/abstract/utils'; | ||
import { toBytes } from '../bytes/toBytes.js'; | ||
import { isHex } from './isHex.js'; | ||
/** | ||
* Checks if two {@link Bytes} or {@link Hex} values are equal. | ||
* | ||
* @example | ||
* import { Bytes } from 'ox' | ||
* Bytes.isEqual(Bytes.from([1]), Bytes.from([1])) // true | ||
* Bytes.isEqual(Bytes.from([1]), Bytes.from([2])) // false | ||
* | ||
* @example | ||
* import { Hex } from 'ox' | ||
* Hex.isEqual('0xdeadbeef', '0xdeadbeef') // true | ||
* Hex.isEqual('0xda', '0xba') // false | ||
*/ | ||
export function isBytesEqual(a_, b_) { | ||
@@ -5,0 +18,0 @@ const a = isHex(a_, { strict: false }) ? toBytes(a_) : a_; |
import type { ErrorType as ErrorType_ } from '../errors/error.js'; | ||
import type { Hex } from '../types/data.js'; | ||
export declare namespace isHex { | ||
type Options = { | ||
strict?: boolean | undefined; | ||
}; | ||
type ErrorType = ErrorType_; | ||
} | ||
/** | ||
@@ -11,9 +17,3 @@ * Checks if the given value is {@link Hex}. | ||
*/ | ||
export declare namespace isHex { | ||
type Options = { | ||
strict?: boolean | undefined; | ||
}; | ||
type ErrorType = ErrorType_; | ||
} | ||
export declare function isHex(value: unknown, options?: isHex.Options): value is Hex; | ||
//# sourceMappingURL=isHex.d.ts.map |
@@ -0,1 +1,9 @@ | ||
/** | ||
* Checks if the given value is {@link Hex}. | ||
* | ||
* @example | ||
* import { Hex } from 'ox' | ||
* Hex.isHex('0x') // true | ||
* Hex.isHex(Bytes.from([1, 2, 3])) // false | ||
*/ | ||
export function isHex(value, options = {}) { | ||
@@ -2,0 +10,0 @@ const { strict = true } = options; |
import { type SizeExceedsPaddingSizeErrorType } from '../errors/data.js'; | ||
import type { ErrorType as ErrorType_ } from '../errors/error.js'; | ||
import type { Bytes, Hex } from '../types/data.js'; | ||
export declare namespace padLeft { | ||
type ReturnType<value extends Bytes | Hex> = pad.ReturnType<value>; | ||
type ErrorType = pad.ErrorType | ErrorType_; | ||
} | ||
/** | ||
@@ -17,7 +21,7 @@ * Pads a {@link Bytes} or {@link Hex} value to the left with zero bytes until it reaches the given `size` (default: 32 bytes). | ||
*/ | ||
export declare namespace padLeft { | ||
export declare function padLeft<value extends Bytes | Hex>(value: value, size?: number | undefined): padLeft.ReturnType<value>; | ||
export declare namespace padRight { | ||
type ReturnType<value extends Bytes | Hex> = pad.ReturnType<value>; | ||
type ErrorType = pad.ErrorType | ErrorType_; | ||
} | ||
export declare function padLeft<value extends Bytes | Hex>(value: value, size?: number | undefined): padLeft.ReturnType<value>; | ||
/** | ||
@@ -36,6 +40,2 @@ * Pads a {@link Bytes} or {@link Hex} value to the right with zero bytes until it reaches the given `size` (default: 32 bytes). | ||
*/ | ||
export declare namespace padRight { | ||
type ReturnType<value extends Bytes | Hex> = pad.ReturnType<value>; | ||
type ErrorType = pad.ErrorType | ErrorType_; | ||
} | ||
export declare function padRight<value extends Bytes | Hex>(value: value, size?: number | undefined): padRight.ReturnType<value>; | ||
@@ -42,0 +42,0 @@ export declare namespace pad { |
import { SizeExceedsPaddingSizeError, } from '../errors/data.js'; | ||
/** | ||
* Pads a {@link Bytes} or {@link Hex} value to the left with zero bytes until it reaches the given `size` (default: 32 bytes). | ||
* | ||
* @example | ||
* import { Bytes } from 'ox' | ||
* Bytes.padLeft(Bytes.from([1]), 4) | ||
* // Uint8Array([0, 0, 0, 1]) | ||
* | ||
* @example | ||
* import { Hex } from 'ox' | ||
* Hex.padLeft('0x1234', 4) | ||
* // '0x00001234' | ||
*/ | ||
export function padLeft(value, size) { | ||
return pad(value, { dir: 'left', size }); | ||
} | ||
/** | ||
* Pads a {@link Bytes} or {@link Hex} value to the right with zero bytes until it reaches the given `size` (default: 32 bytes). | ||
* | ||
* @example | ||
* import { Bytes } from 'ox' | ||
* Bytes.padRight(Bytes.from([1]), 4) | ||
* // Uint8Array([1, 0, 0, 0]) | ||
* | ||
* @example | ||
* import { Hex } from 'ox' | ||
* Hex.padRight('0x1234', 4) | ||
* // '0x12340000' | ||
*/ | ||
export function padRight(value, size) { | ||
@@ -6,0 +32,0 @@ return pad(value, { dir: 'right', size }); |
import type { ErrorType as ErrorType_ } from '../errors/error.js'; | ||
import type { Bytes } from '../types/data.js'; | ||
export declare namespace randomBytes { | ||
type ErrorType = ErrorType_; | ||
} | ||
/** | ||
@@ -18,6 +21,3 @@ * Generates a random byte array of the specified length. | ||
*/ | ||
export declare namespace randomBytes { | ||
type ErrorType = ErrorType_; | ||
} | ||
export declare function randomBytes(length: number): Bytes; | ||
//# sourceMappingURL=random.d.ts.map |
@@ -0,1 +1,16 @@ | ||
/** | ||
* Generates a random byte array of the specified length. | ||
* | ||
* @example | ||
* ```ts | ||
* import { Bytes } from 'ox' | ||
* const bytes = Bytes.random(32) | ||
* ``` | ||
* | ||
* @example | ||
* ```ts | ||
* import { Data } from 'ox' | ||
* const bytes = Data.randomBytes(32) | ||
* ``` | ||
*/ | ||
export function randomBytes(length) { | ||
@@ -2,0 +17,0 @@ return crypto.getRandomValues(new Uint8Array(length)); |
import type { ErrorType as ErrorType_ } from '../errors/error.js'; | ||
import type { Bytes, Hex } from '../types/data.js'; | ||
import { isHex } from './isHex.js'; | ||
export declare namespace size { | ||
type ErrorType = isHex.ErrorType | ErrorType_; | ||
} | ||
/** | ||
@@ -20,6 +23,3 @@ * Retrieves the size of a {@link Hex} or {@link Bytes} value (in bytes). | ||
*/ | ||
export declare namespace size { | ||
type ErrorType = isHex.ErrorType | ErrorType_; | ||
} | ||
export declare function size(value: Hex | Bytes): number; | ||
//# sourceMappingURL=size.d.ts.map |
import { isHex } from './isHex.js'; | ||
/** | ||
* Retrieves the size of a {@link Hex} or {@link Bytes} value (in bytes). | ||
* | ||
* @example | ||
* import { Data } from 'ox' | ||
* Data.size('0xdeadbeef') // 4 | ||
* Data.size(Bytes.from([1, 2, 3, 4])) // 4 | ||
* | ||
* @example | ||
* import { Bytes } from 'ox' | ||
* Bytes.size(Bytes.from([1, 2, 3, 4])) // 4 | ||
* | ||
* @example | ||
* import { Hex } from 'ox' | ||
* Hex.size('0xdeadbeef') // 4 | ||
*/ | ||
export function size(value) { | ||
@@ -3,0 +19,0 @@ if (isHex(value, { strict: false })) |
@@ -6,2 +6,9 @@ import { type SliceOffsetOutOfBoundsErrorType } from '../errors/data.js'; | ||
import { size } from './size.js'; | ||
export declare namespace slice { | ||
type Options = { | ||
strict?: boolean | undefined; | ||
}; | ||
type ReturnType<value extends Bytes | Hex> = value extends Hex ? Hex : Bytes; | ||
type ErrorType = isHex.ErrorType | sliceBytes.ErrorType | sliceHex.ErrorType | ErrorType_; | ||
} | ||
/** | ||
@@ -23,9 +30,2 @@ * Returns a section of a {@link Hex} or {@link Bytes} value given a start/end bytes offset. | ||
*/ | ||
export declare namespace slice { | ||
type Options = { | ||
strict?: boolean | undefined; | ||
}; | ||
type ReturnType<value extends Bytes | Hex> = value extends Hex ? Hex : Bytes; | ||
type ErrorType = isHex.ErrorType | sliceBytes.ErrorType | sliceHex.ErrorType | ErrorType_; | ||
} | ||
export declare function slice<value extends Bytes | Hex>(value: value, start?: number | undefined, end?: number | undefined, options?: slice.Options): slice.ReturnType<value>; | ||
@@ -32,0 +32,0 @@ declare namespace assertStartOffset { |
import { SliceOffsetOutOfBoundsError, } from '../errors/data.js'; | ||
import { isHex } from './isHex.js'; | ||
import { size } from './size.js'; | ||
/** | ||
* Returns a section of a {@link Hex} or {@link Bytes} value given a start/end bytes offset. | ||
* | ||
* @example | ||
* import { Data } from 'ox' | ||
* Data.slice('0x0123456789', 1, 4) // '0x234567' | ||
* | ||
* @example | ||
* import { Hex } from 'ox' | ||
* Hex.slice('0x0123456789', 1, 4) // '0x234567' | ||
* | ||
* @example | ||
* import { Bytes } from 'ox' | ||
* Bytes.slice(Bytes.from([1, 2, 3, 4, 5, 6, 7, 8, 9]), 1, 4) | ||
* // Uint8Array([2, 3, 4]) | ||
*/ | ||
export function slice(value, start, end, options = {}) { | ||
@@ -5,0 +21,0 @@ const { strict } = options; |
import type { ErrorType as ErrorType_ } from '../errors/error.js'; | ||
import type { Bytes, Hex } from '../types/data.js'; | ||
export declare namespace trimLeft { | ||
type ReturnType<value extends Bytes | Hex> = value extends Hex ? Hex : Bytes; | ||
type ErrorType = trim.ErrorType | ErrorType_; | ||
} | ||
/** | ||
@@ -18,7 +22,7 @@ * Trims leading zeros from a {@link Bytes} or {@link Hex} value. | ||
*/ | ||
export declare namespace trimLeft { | ||
export declare function trimLeft<value extends Bytes | Hex>(value: value): trimLeft.ReturnType<value>; | ||
export declare namespace trimRight { | ||
type ReturnType<value extends Bytes | Hex> = value extends Hex ? Hex : Bytes; | ||
type ErrorType = trim.ErrorType | ErrorType_; | ||
} | ||
export declare function trimLeft<value extends Bytes | Hex>(value: value): trimLeft.ReturnType<value>; | ||
/** | ||
@@ -39,6 +43,2 @@ * Trims trailing zeros from a {@link Bytes} or {@link Hex} value. | ||
*/ | ||
export declare namespace trimRight { | ||
type ReturnType<value extends Bytes | Hex> = value extends Hex ? Hex : Bytes; | ||
type ErrorType = trim.ErrorType | ErrorType_; | ||
} | ||
export declare function trimRight<value extends Bytes | Hex>(value: value): trimRight.ReturnType<value>; | ||
@@ -45,0 +45,0 @@ export declare namespace trim { |
@@ -0,4 +1,34 @@ | ||
/** | ||
* Trims leading zeros from a {@link Bytes} or {@link Hex} value. | ||
* | ||
* @example | ||
* import { Data } from 'ox' | ||
* Data.trimLeft('0x00000000deadbeef') // '0xdeadbeef' | ||
* | ||
* @example | ||
* import { Hex } from 'ox' | ||
* Hex.trimLeft('0x00000000deadbeef') // '0xdeadbeef' | ||
* | ||
* @example | ||
* import { Bytes } from 'ox' | ||
* Bytes.trimLeft(Bytes.from([0, 0, 0, 0, 1, 2, 3])) // Uint8Array([1, 2, 3]) | ||
*/ | ||
export function trimLeft(value) { | ||
return trim(value, { dir: 'left' }); | ||
} | ||
/** | ||
* Trims trailing zeros from a {@link Bytes} or {@link Hex} value. | ||
* | ||
* @example | ||
* import { Data } from 'ox' | ||
* Data.trimRight('0xdeadbeef00000000') // '0xdeadbeef' | ||
* | ||
* @example | ||
* import { Hex } from 'ox' | ||
* Hex.trimRight('0xdeadbeef00000000') // '0xdeadbeef' | ||
* | ||
* @example | ||
* import { Bytes } from 'ox' | ||
* Bytes.trimRight(Bytes.from([1, 2, 3, 0, 0, 0, 0])) // Uint8Array([1, 2, 3]) | ||
*/ | ||
export function trimRight(value) { | ||
@@ -5,0 +35,0 @@ return trim(value, { dir: 'right' }); |
@@ -8,2 +8,10 @@ import { hexToBytes } from '../bytes/toBytes.js'; | ||
type To = 'string' | 'bytes' | 'bigint' | 'number' | 'boolean'; | ||
export declare namespace fromHex { | ||
type Options = { | ||
/** Size (in bytes) of the hex value. */ | ||
size?: number | undefined; | ||
}; | ||
type ReturnType<to> = (to extends 'string' ? string : never) | (to extends 'bytes' ? Bytes : never) | (to extends 'bigint' ? bigint : never) | (to extends 'number' ? number : never) | (to extends 'boolean' ? boolean : never); | ||
type ErrorType = hexToNumber.ErrorType | hexToBigInt.ErrorType | hexToBoolean.ErrorType | hexToString.ErrorType | hexToBytes.ErrorType | InvalidTypeErrorType | ErrorType_; | ||
} | ||
/** | ||
@@ -29,11 +37,12 @@ * Decodes a {@link Hex} value into a string, number, bigint, boolean, or {@link Bytes}. | ||
*/ | ||
export declare namespace fromHex { | ||
export declare function fromHex<to extends To>(hex: Hex, to: to | To, options?: fromHex.Options): fromHex.ReturnType<to>; | ||
export declare namespace hexToBigInt { | ||
type Options = { | ||
/** Whether or not the number of a signed representation. */ | ||
signed?: boolean | undefined; | ||
/** Size (in bytes) of the hex value. */ | ||
size?: number | undefined; | ||
}; | ||
type ReturnType<to> = (to extends 'string' ? string : never) | (to extends 'bytes' ? Bytes : never) | (to extends 'bigint' ? bigint : never) | (to extends 'number' ? number : never) | (to extends 'boolean' ? boolean : never); | ||
type ErrorType = hexToNumber.ErrorType | hexToBigInt.ErrorType | hexToBoolean.ErrorType | hexToString.ErrorType | hexToBytes.ErrorType | InvalidTypeErrorType | ErrorType_; | ||
type ErrorType = assertSize.ErrorType | ErrorType_; | ||
} | ||
export declare function fromHex<to extends To>(hex: Hex, to: to | To, options?: fromHex.Options): fromHex.ReturnType<to>; | ||
/** | ||
@@ -52,12 +61,10 @@ * Decodes a {@link Hex} value into a BigInt. | ||
*/ | ||
export declare namespace hexToBigInt { | ||
export declare function hexToBigInt(hex: Hex, options?: hexToBigInt.Options): bigint; | ||
export declare namespace hexToBoolean { | ||
type Options = { | ||
/** Whether or not the number of a signed representation. */ | ||
signed?: boolean | undefined; | ||
/** Size (in bytes) of the hex value. */ | ||
size?: number | undefined; | ||
}; | ||
type ErrorType = assertSize.ErrorType | ErrorType_; | ||
type ErrorType = assertSize.ErrorType | trimLeft.ErrorType | InvalidHexBooleanErrorType | ErrorType_; | ||
} | ||
export declare function hexToBigInt(hex: Hex, options?: hexToBigInt.Options): bigint; | ||
/** | ||
@@ -76,10 +83,7 @@ * Decodes a {@link Hex} value into a boolean. | ||
*/ | ||
export declare namespace hexToBoolean { | ||
type Options = { | ||
/** Size (in bytes) of the hex value. */ | ||
size?: number | undefined; | ||
}; | ||
type ErrorType = assertSize.ErrorType | trimLeft.ErrorType | InvalidHexBooleanErrorType | ErrorType_; | ||
export declare function hexToBoolean(hex_: Hex, options?: hexToBoolean.Options): boolean; | ||
export declare namespace hexToNumber { | ||
type Options = hexToBigInt.Options; | ||
type ErrorType = hexToBigInt.ErrorType | ErrorType_; | ||
} | ||
export declare function hexToBoolean(hex_: Hex, options?: hexToBoolean.Options): boolean; | ||
/** | ||
@@ -98,7 +102,10 @@ * Decodes a {@link Hex} value into a number. | ||
*/ | ||
export declare namespace hexToNumber { | ||
type Options = hexToBigInt.Options; | ||
type ErrorType = hexToBigInt.ErrorType | ErrorType_; | ||
export declare function hexToNumber(hex: Hex, options?: hexToNumber.Options): number; | ||
export declare namespace hexToString { | ||
type Options = { | ||
/** Size (in bytes) of the hex value. */ | ||
size?: number | undefined; | ||
}; | ||
type ErrorType = assertSize.ErrorType | hexToBytes.ErrorType | trimRight.ErrorType | ErrorType_; | ||
} | ||
export declare function hexToNumber(hex: Hex, options?: hexToNumber.Options): number; | ||
/** | ||
@@ -119,11 +126,4 @@ * Decodes a {@link Hex} value into a UTF-8 string. | ||
*/ | ||
export declare namespace hexToString { | ||
type Options = { | ||
/** Size (in bytes) of the hex value. */ | ||
size?: number | undefined; | ||
}; | ||
type ErrorType = assertSize.ErrorType | hexToBytes.ErrorType | trimRight.ErrorType | ErrorType_; | ||
} | ||
export declare function hexToString(hex: Hex, options?: hexToString.Options): string; | ||
export {}; | ||
//# sourceMappingURL=fromHex.d.ts.map |
@@ -5,2 +5,22 @@ import { hexToBytes } from '../bytes/toBytes.js'; | ||
import { InvalidHexBooleanError, InvalidTypeError, } from '../errors/data.js'; | ||
/** | ||
* Decodes a {@link Hex} value into a string, number, bigint, boolean, or {@link Bytes}. | ||
* | ||
* @example | ||
* import { Hex } from 'ox' | ||
* Hex.to('0x1a4', 'number') | ||
* // 420 | ||
* | ||
* @example | ||
* import { Hex } from 'ox' | ||
* Hex.to('0x48656c6c6f20576f726c6421', 'string') | ||
* // 'Hello world' | ||
* | ||
* @example | ||
* import { Hex } from 'ox' | ||
* Hex.to('0x48656c6c6f20576f726c64210000000000000000000000000000000000000000', 'string', { | ||
* size: 32, | ||
* }) | ||
* // 'Hello world' | ||
*/ | ||
export function fromHex(hex, to, options = {}) { | ||
@@ -19,2 +39,15 @@ if (to === 'number') | ||
} | ||
/** | ||
* Decodes a {@link Hex} value into a BigInt. | ||
* | ||
* @example | ||
* import { Hex } from 'ox' | ||
* Hex.toBigInt('0x1a4') | ||
* // 420n | ||
* | ||
* @example | ||
* import { Hex } from 'ox' | ||
* Hex.toBigInt('0x00000000000000000000000000000000000000000000000000000000000001a4', { size: 32 }) | ||
* // 420n | ||
*/ | ||
export function hexToBigInt(hex, options = {}) { | ||
@@ -33,2 +66,15 @@ const { signed } = options; | ||
} | ||
/** | ||
* Decodes a {@link Hex} value into a boolean. | ||
* | ||
* @example | ||
* import { Hex } from 'ox' | ||
* Hex.toBoolean('0x01') | ||
* // true | ||
* | ||
* @example | ||
* import { Hex } from 'ox' | ||
* Hex.toBoolean('0x0000000000000000000000000000000000000000000000000000000000000001', { size: 32 }) | ||
* // true | ||
*/ | ||
export function hexToBoolean(hex_, options = {}) { | ||
@@ -46,2 +92,15 @@ let hex = hex_; | ||
} | ||
/** | ||
* Decodes a {@link Hex} value into a number. | ||
* | ||
* @example | ||
* import { Hex } from 'ox' | ||
* Hex.toNumber('0x1a4') | ||
* // 420 | ||
* | ||
* @example | ||
* import { Hex } from 'ox' | ||
* Hex.toNumber('0x00000000000000000000000000000000000000000000000000000000000001a4', { size: 32 }) | ||
* // 420 | ||
*/ | ||
export function hexToNumber(hex, options = {}) { | ||
@@ -53,2 +112,17 @@ const { signed, size } = options; | ||
} | ||
/** | ||
* Decodes a {@link Hex} value into a UTF-8 string. | ||
* | ||
* @example | ||
* import { Hex } from 'ox' | ||
* Hex.toString('0x48656c6c6f20576f726c6421') | ||
* // 'Hello world!' | ||
* | ||
* @example | ||
* import { Hex } from 'ox' | ||
* Hex.toString('0x48656c6c6f20576f726c64210000000000000000000000000000000000000000', { | ||
* size: 32, | ||
* }) | ||
* // 'Hello world' | ||
*/ | ||
export function hexToString(hex, options = {}) { | ||
@@ -55,0 +129,0 @@ const { size } = options; |
@@ -7,2 +7,9 @@ import { assertSize } from '../data/assertSize.js'; | ||
import type { Bytes, Hex } from '../types/data.js'; | ||
export declare namespace toHex { | ||
type Parameters = { | ||
/** The size (in bytes) of the output hex value. */ | ||
size?: number | undefined; | ||
}; | ||
type ErrorType = booleanToHex.ErrorType | bytesToHex.ErrorType | numberToHex.ErrorType | stringToHex.ErrorType | isHex.ErrorType | InvalidTypeErrorType | ErrorType_; | ||
} | ||
/** | ||
@@ -26,10 +33,10 @@ * Encodes an arbitrary value into a {@link Hex} value. | ||
*/ | ||
export declare namespace toHex { | ||
type Parameters = { | ||
export declare function toHex(value: string | number | bigint | boolean | readonly number[] | Bytes, options?: toHex.Parameters): Hex; | ||
export declare namespace booleanToHex { | ||
type Options = { | ||
/** The size (in bytes) of the output hex value. */ | ||
size?: number | undefined; | ||
}; | ||
type ErrorType = booleanToHex.ErrorType | bytesToHex.ErrorType | numberToHex.ErrorType | stringToHex.ErrorType | isHex.ErrorType | InvalidTypeErrorType | ErrorType_; | ||
type ErrorType = assertSize.ErrorType | padLeft.ErrorType | ErrorType_; | ||
} | ||
export declare function toHex(value: string | number | bigint | boolean | readonly number[] | Bytes, options?: toHex.Parameters): Hex; | ||
/** | ||
@@ -53,3 +60,4 @@ * Encodes a boolean into a {@link Hex} value. | ||
*/ | ||
export declare namespace booleanToHex { | ||
export declare function booleanToHex(value: boolean, options?: booleanToHex.Options): Hex; | ||
export declare namespace bytesToHex { | ||
type Options = { | ||
@@ -59,5 +67,4 @@ /** The size (in bytes) of the output hex value. */ | ||
}; | ||
type ErrorType = assertSize.ErrorType | padLeft.ErrorType | ErrorType_; | ||
type ErrorType = assertSize.ErrorType | padRight.ErrorType | ErrorType_; | ||
} | ||
export declare function booleanToHex(value: boolean, options?: booleanToHex.Options): Hex; | ||
/** | ||
@@ -81,10 +88,16 @@ * Encodes a {@link Bytes} value into a {@link Hex} value. | ||
*/ | ||
export declare namespace bytesToHex { | ||
export declare function bytesToHex(value: Bytes, options?: bytesToHex.Options): Hex; | ||
export declare namespace numberToHex { | ||
type Options = { | ||
/** Whether or not the number of a signed representation. */ | ||
signed?: boolean | undefined; | ||
/** The size (in bytes) of the output hex value. */ | ||
size: number; | ||
} | { | ||
signed?: undefined; | ||
/** The size (in bytes) of the output hex value. */ | ||
size?: number | undefined; | ||
}; | ||
type ErrorType = assertSize.ErrorType | padRight.ErrorType | ErrorType_; | ||
type ErrorType = IntegerOutOfRangeErrorType | padLeft.ErrorType | ErrorType_; | ||
} | ||
export declare function bytesToHex(value: Bytes, options?: bytesToHex.Options): Hex; | ||
/** | ||
@@ -103,16 +116,10 @@ * Encodes a number or bigint into a {@link Hex} value. | ||
*/ | ||
export declare namespace numberToHex { | ||
export declare function numberToHex(value_: number | bigint, options?: numberToHex.Options): Hex; | ||
export declare namespace stringToHex { | ||
type Options = { | ||
/** Whether or not the number of a signed representation. */ | ||
signed?: boolean | undefined; | ||
/** The size (in bytes) of the output hex value. */ | ||
size: number; | ||
} | { | ||
signed?: undefined; | ||
/** The size (in bytes) of the output hex value. */ | ||
size?: number | undefined; | ||
}; | ||
type ErrorType = IntegerOutOfRangeErrorType | padLeft.ErrorType | ErrorType_; | ||
type ErrorType = bytesToHex.ErrorType | ErrorType_; | ||
} | ||
export declare function numberToHex(value_: number | bigint, options?: numberToHex.Options): Hex; | ||
/** | ||
@@ -131,10 +138,3 @@ * Encodes a UTF-8 string into a hex string | ||
*/ | ||
export declare namespace stringToHex { | ||
type Options = { | ||
/** The size (in bytes) of the output hex value. */ | ||
size?: number | undefined; | ||
}; | ||
type ErrorType = bytesToHex.ErrorType | ErrorType_; | ||
} | ||
export declare function stringToHex(value_: string, options?: stringToHex.Options): Hex; | ||
//# sourceMappingURL=toHex.d.ts.map |
@@ -7,2 +7,20 @@ import { assertSize } from '../data/assertSize.js'; | ||
const hexes = /*#__PURE__*/ Array.from({ length: 256 }, (_v, i) => i.toString(16).padStart(2, '0')); | ||
/** | ||
* Encodes an arbitrary value into a {@link Hex} value. | ||
* | ||
* @example | ||
* import { Hex } from 'ox' | ||
* Hex.from('Hello world') | ||
* // '0x48656c6c6f20776f726c6421' | ||
* | ||
* @example | ||
* import { Hex } from 'ox' | ||
* Hex.from(420) | ||
* // '0x1a4' | ||
* | ||
* @example | ||
* import { Hex } from 'ox' | ||
* Hex.from('Hello world', { size: 32 }) | ||
* // '0x48656c6c6f20776f726c64210000000000000000000000000000000000000000' | ||
*/ | ||
export function toHex(value, options = {}) { | ||
@@ -23,2 +41,20 @@ if (isHex(value)) | ||
} | ||
/** | ||
* Encodes a boolean into a {@link Hex} value. | ||
* | ||
* @example | ||
* import { Hex } from 'ox' | ||
* Hex.fromBoolean(true) | ||
* // '0x1' | ||
* | ||
* @example | ||
* import { Hex } from 'ox' | ||
* Hex.fromBoolean(false) | ||
* // '0x0' | ||
* | ||
* @example | ||
* import { Hex } from 'ox' | ||
* Hex.fromBoolean(true, { size: 32 }) | ||
* // '0x0000000000000000000000000000000000000000000000000000000000000001' | ||
*/ | ||
export function booleanToHex(value, options = {}) { | ||
@@ -32,2 +68,20 @@ const hex = `0x${Number(value)}`; | ||
} | ||
/** | ||
* Encodes a {@link Bytes} value into a {@link Hex} value. | ||
* | ||
* @example | ||
* import { Bytes } from 'ox' | ||
* Bytes.toHex(Bytes.from([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33]) | ||
* // '0x48656c6c6f20576f726c6421' | ||
* | ||
* @example | ||
* import { Hex } from 'ox' | ||
* Hex.fromBytes(Bytes.from([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33]) | ||
* // '0x48656c6c6f20576f726c6421' | ||
* | ||
* @example | ||
* import { Hex } from 'ox' | ||
* Hex.fromBytes(Bytes.from([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33]), { size: 32 }) | ||
* // '0x48656c6c6f20576f726c642100000000000000000000000000000000000000000' | ||
*/ | ||
export function bytesToHex(value, options = {}) { | ||
@@ -44,2 +98,15 @@ let string = ''; | ||
} | ||
/** | ||
* Encodes a number or bigint into a {@link Hex} value. | ||
* | ||
* @example | ||
* import { Hex } from 'ox' | ||
* Hex.fromNumber(420) | ||
* // '0x1a4' | ||
* | ||
* @example | ||
* import { Hex } from 'ox' | ||
* Hex.fromNumber(420, { size: 32 }) | ||
* // '0x00000000000000000000000000000000000000000000000000000000000001a4' | ||
*/ | ||
export function numberToHex(value_, options = {}) { | ||
@@ -77,2 +144,15 @@ const { signed, size } = options; | ||
const encoder = /*#__PURE__*/ new TextEncoder(); | ||
/** | ||
* Encodes a UTF-8 string into a hex string | ||
* @example | ||
* import { Hex } from 'ox' | ||
* Hex.fromString('Hello World!') | ||
* // '0x48656c6c6f20576f726c6421' | ||
* | ||
* @example | ||
* import { Hex } from 'ox' | ||
* Hex.fromString('Hello World!', { size: 32 }) | ||
* // '0x48656c6c6f20576f726c64210000000000000000000000000000000000000000' | ||
*/ | ||
export function stringToHex(value_, options = {}) { | ||
@@ -79,0 +159,0 @@ const value = encoder.encode(value_); |
@@ -10,2 +10,6 @@ import { hexToBytes } from '../bytes/toBytes.js'; | ||
type To = 'hex' | 'bytes'; | ||
export declare namespace fromRlp { | ||
type ReturnType<to extends To> = (to extends 'bytes' ? RecursiveArray<Bytes> : never) | (to extends 'hex' ? RecursiveArray<Hex> : never); | ||
type ErrorType = hexToBytes.ErrorType | fromRlpCursor.ErrorType | createCursor.ErrorType | InvalidHexValueErrorType | ErrorType_; | ||
} | ||
/** | ||
@@ -19,7 +23,7 @@ * Decodes a Recursive-Length Prefix (RLP) value into a decoded {@link Bytes} or {@link Hex} value. | ||
*/ | ||
export declare namespace fromRlp { | ||
type ReturnType<to extends To> = (to extends 'bytes' ? RecursiveArray<Bytes> : never) | (to extends 'hex' ? RecursiveArray<Hex> : never); | ||
type ErrorType = hexToBytes.ErrorType | fromRlpCursor.ErrorType | createCursor.ErrorType | InvalidHexValueErrorType | ErrorType_; | ||
export declare function fromRlp<value extends Bytes | Hex, to extends To = value extends Bytes ? 'bytes' : 'hex'>(value: value, to_?: to | To | undefined): fromRlp.ReturnType<to>; | ||
export declare namespace rlpToBytes { | ||
type ErrorType = fromRlp.ErrorType; | ||
type ReturnType = fromRlp.ReturnType<'bytes'>; | ||
} | ||
export declare function fromRlp<value extends Bytes | Hex, to extends To = value extends Bytes ? 'bytes' : 'hex'>(value: value, to_?: to | To | undefined): fromRlp.ReturnType<to>; | ||
/** | ||
@@ -33,7 +37,7 @@ * Decodes a Recursive-Length Prefix (RLP) value into a decoded {@link Bytes} value. | ||
*/ | ||
export declare namespace rlpToBytes { | ||
export declare function rlpToBytes(value: Bytes | Hex): rlpToBytes.ReturnType; | ||
export declare namespace rlpToHex { | ||
type ErrorType = fromRlp.ErrorType; | ||
type ReturnType = fromRlp.ReturnType<'bytes'>; | ||
type ReturnType = fromRlp.ReturnType<'hex'>; | ||
} | ||
export declare function rlpToBytes(value: Bytes | Hex): rlpToBytes.ReturnType; | ||
/** | ||
@@ -47,6 +51,2 @@ * Decodes a Recursive-Length Prefix (RLP) value into a decoded {@link Hex} value. | ||
*/ | ||
export declare namespace rlpToHex { | ||
type ErrorType = fromRlp.ErrorType; | ||
type ReturnType = fromRlp.ReturnType<'hex'>; | ||
} | ||
export declare function rlpToHex(value: Bytes | Hex): rlpToHex.ReturnType; | ||
@@ -53,0 +53,0 @@ declare namespace fromRlpCursor { |
@@ -6,2 +6,10 @@ import { hexToBytes } from '../bytes/toBytes.js'; | ||
import { bytesToHex } from '../hex/toHex.js'; | ||
/** | ||
* Decodes a Recursive-Length Prefix (RLP) value into a decoded {@link Bytes} or {@link Hex} value. | ||
* | ||
* @example | ||
* import { Rlp } from 'ox' | ||
* Rlp.decode('0x8b68656c6c6f20776f726c64') | ||
* // 0x68656c6c6f20776f726c64 | ||
*/ | ||
export function fromRlp(value, to_) { | ||
@@ -23,5 +31,21 @@ const to = to_ ?? (typeof value === 'string' ? 'hex' : 'bytes'); | ||
} | ||
/** | ||
* Decodes a Recursive-Length Prefix (RLP) value into a decoded {@link Bytes} value. | ||
* | ||
* @example | ||
* import { Rlp } from 'ox' | ||
* Rlp.toBytes('0x8b68656c6c6f20776f726c64') | ||
* // Uint8Array([139, 104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100]) | ||
*/ | ||
export function rlpToBytes(value) { | ||
return fromRlp(value, 'bytes'); | ||
} | ||
/** | ||
* Decodes a Recursive-Length Prefix (RLP) value into a decoded {@link Hex} value. | ||
* | ||
* @example | ||
* import { Rlp } from 'ox' | ||
* Rlp.toHex('0x8b68656c6c6f20776f726c64') | ||
* // 0x68656c6c6f20776f726c64 | ||
*/ | ||
export function rlpToHex(value) { | ||
@@ -28,0 +52,0 @@ return fromRlp(value, 'hex'); |
@@ -8,2 +8,6 @@ import { hexToBytes } from '../bytes/toBytes.js'; | ||
type To = 'hex' | 'bytes'; | ||
export declare namespace toRlp { | ||
type ReturnType<to extends To> = (to extends 'bytes' ? Bytes : never) | (to extends 'hex' ? Hex : never); | ||
type ErrorType = createCursor.ErrorType | bytesToHex.ErrorType | hexToBytes.ErrorType | ErrorType_; | ||
} | ||
/** | ||
@@ -17,7 +21,7 @@ * Encodes a {@link Bytes} or {@link Hex} value into a Recursive-Length Prefix (RLP) value. | ||
*/ | ||
export declare namespace toRlp { | ||
type ReturnType<to extends To> = (to extends 'bytes' ? Bytes : never) | (to extends 'hex' ? Hex : never); | ||
type ErrorType = createCursor.ErrorType | bytesToHex.ErrorType | hexToBytes.ErrorType | ErrorType_; | ||
export declare function toRlp<bytes extends RecursiveArray<Bytes> | RecursiveArray<Hex>, to extends To = bytes extends RecursiveArray<Bytes> ? 'bytes' : 'hex'>(bytes: bytes, to_?: to | To | undefined): toRlp.ReturnType<to>; | ||
export declare namespace bytesToRlp { | ||
type ReturnType<to extends To> = toRlp.ReturnType<to>; | ||
type ErrorType = toRlp.ErrorType | ErrorType_; | ||
} | ||
export declare function toRlp<bytes extends RecursiveArray<Bytes> | RecursiveArray<Hex>, to extends To = bytes extends RecursiveArray<Bytes> ? 'bytes' : 'hex'>(bytes: bytes, to_?: to | To | undefined): toRlp.ReturnType<to>; | ||
/** | ||
@@ -31,7 +35,7 @@ * Encodes a {@link Bytes} value into a Recursive-Length Prefix (RLP) value. | ||
*/ | ||
export declare namespace bytesToRlp { | ||
export declare function bytesToRlp<to extends To = 'bytes'>(bytes: RecursiveArray<Bytes>, to?: to | To | undefined): bytesToRlp.ReturnType<to>; | ||
export declare namespace hexToRlp { | ||
type ReturnType<to extends To> = toRlp.ReturnType<to>; | ||
type ErrorType = toRlp.ErrorType | ErrorType_; | ||
} | ||
export declare function bytesToRlp<to extends To = 'bytes'>(bytes: RecursiveArray<Bytes>, to?: to | To | undefined): bytesToRlp.ReturnType<to>; | ||
/** | ||
@@ -45,8 +49,4 @@ * Encodes a {@link Hex} value into a Recursive-Length Prefix (RLP) value. | ||
*/ | ||
export declare namespace hexToRlp { | ||
type ReturnType<to extends To> = toRlp.ReturnType<to>; | ||
type ErrorType = toRlp.ErrorType | ErrorType_; | ||
} | ||
export declare function hexToRlp<to extends To = 'hex'>(hex: RecursiveArray<Hex>, to?: to | To | undefined): hexToRlp.ReturnType<to>; | ||
export {}; | ||
//# sourceMappingURL=toRlp.d.ts.map |
@@ -5,2 +5,10 @@ import { hexToBytes } from '../bytes/toBytes.js'; | ||
import { bytesToHex } from '../hex/toHex.js'; | ||
/** | ||
* Encodes a {@link Bytes} or {@link Hex} value into a Recursive-Length Prefix (RLP) value. | ||
* | ||
* @example | ||
* import { Rlp } from 'ox' | ||
* Rlp.encode('0x68656c6c6f20776f726c64') | ||
* // 0x8b68656c6c6f20776f726c64 | ||
*/ | ||
export function toRlp(bytes, to_) { | ||
@@ -15,5 +23,21 @@ const encodable = getEncodable(bytes); | ||
} | ||
/** | ||
* Encodes a {@link Bytes} value into a Recursive-Length Prefix (RLP) value. | ||
* | ||
* @example | ||
* import { Rlp } from 'ox' | ||
* Rlp.fromBytes(Uint8Array([139, 104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100])) | ||
* // Uint8Array([104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100]) | ||
*/ | ||
export function bytesToRlp(bytes, to = 'bytes') { | ||
return toRlp(bytes, to); | ||
} | ||
/** | ||
* Encodes a {@link Hex} value into a Recursive-Length Prefix (RLP) value. | ||
* | ||
* @example | ||
* import { Rlp } from 'ox' | ||
* Rlp.fromHex('0x68656c6c6f20776f726c64') | ||
* // 0x8b68656c6c6f20776f726c64 | ||
*/ | ||
export function hexToRlp(hex, to = 'hex') { | ||
@@ -20,0 +44,0 @@ return toRlp(hex, to); |
/** @internal */ | ||
export declare const version = "0.0.0-20240815T034436"; | ||
export declare const version = "0.0.0-20240815T050956"; | ||
//# sourceMappingURL=version.d.ts.map |
/** @internal */ | ||
export const version = '0.0.0-20240815T034436'; | ||
export const version = '0.0.0-20240815T050956'; | ||
//# sourceMappingURL=version.js.map |
@@ -15,18 +15,2 @@ import { assertSize } from '../data/assertSize.js' | ||
/** | ||
* Decodes {@link Bytes} into a UTF-8 string, {@link Hex}, number, bigint or boolean. | ||
* | ||
* @example | ||
* import { Bytes } from 'ox' | ||
* Bytes.to(Bytes.from([1, 164]), 'number') | ||
* // 420 | ||
* | ||
* @example | ||
* import { Bytes } from 'ox' | ||
* Bytes.to( | ||
* Bytes.from([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33]), | ||
* 'string' | ||
* ) | ||
* // 'Hello world' | ||
*/ | ||
export declare namespace fromBytes { | ||
@@ -54,2 +38,19 @@ type Options = { | ||
} | ||
/** | ||
* Decodes {@link Bytes} into a UTF-8 string, {@link Hex}, number, bigint or boolean. | ||
* | ||
* @example | ||
* import { Bytes } from 'ox' | ||
* Bytes.to(Bytes.from([1, 164]), 'number') | ||
* // 420 | ||
* | ||
* @example | ||
* import { Bytes } from 'ox' | ||
* Bytes.to( | ||
* Bytes.from([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33]), | ||
* 'string' | ||
* ) | ||
* // 'Hello world' | ||
*/ | ||
export function fromBytes< | ||
@@ -75,10 +76,2 @@ to extends 'string' | 'hex' | 'bigint' | 'number' | 'boolean', | ||
/** | ||
* Decodes a byte array into a bigint. | ||
* | ||
* @example | ||
* import { Bytes } from 'ox' | ||
* Bytes.toBigInt(Bytes.from([1, 164])) | ||
* // 420n | ||
*/ | ||
export declare namespace bytesToBigInt { | ||
@@ -94,2 +87,11 @@ type Options = { | ||
} | ||
/** | ||
* Decodes a byte array into a bigint. | ||
* | ||
* @example | ||
* import { Bytes } from 'ox' | ||
* Bytes.toBigInt(Bytes.from([1, 164])) | ||
* // 420n | ||
*/ | ||
export function bytesToBigInt( | ||
@@ -105,2 +107,11 @@ bytes: Bytes, | ||
export declare namespace bytesToBoolean { | ||
type Options = { | ||
/** Size of the bytes. */ | ||
size?: number | undefined | ||
} | ||
type ErrorType = assertSize.ErrorType | trimLeft.ErrorType | ErrorType_ | ||
} | ||
/** | ||
@@ -114,10 +125,2 @@ * Decodes a byte array into a boolean. | ||
*/ | ||
export declare namespace bytesToBoolean { | ||
type Options = { | ||
/** Size of the bytes. */ | ||
size?: number | undefined | ||
} | ||
type ErrorType = assertSize.ErrorType | trimLeft.ErrorType | ErrorType_ | ||
} | ||
export function bytesToBoolean( | ||
@@ -138,2 +141,11 @@ bytes_: Bytes, | ||
export declare namespace bytesToNumber { | ||
export type Options = bytesToBigInt.Options | ||
export type ErrorType = | ||
| bytesToHex.ErrorType | ||
| hexToNumber.ErrorType | ||
| ErrorType_ | ||
} | ||
/** | ||
@@ -147,10 +159,2 @@ * Decodes a byte array into a number. | ||
*/ | ||
export declare namespace bytesToNumber { | ||
export type Options = bytesToBigInt.Options | ||
export type ErrorType = | ||
| bytesToHex.ErrorType | ||
| hexToNumber.ErrorType | ||
| ErrorType_ | ||
} | ||
export function bytesToNumber( | ||
@@ -168,10 +172,2 @@ bytes: Bytes, | ||
/** | ||
* Decodes a byte array into a UTF-8 string. | ||
* | ||
* @example | ||
* import { Bytes } from 'ox' | ||
* const data = Bytes.toString(Bytes.from([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33])) | ||
* // 'Hello world' | ||
*/ | ||
export declare namespace bytesToString { | ||
@@ -188,2 +184,11 @@ export type Options = { | ||
} | ||
/** | ||
* Decodes a byte array into a UTF-8 string. | ||
* | ||
* @example | ||
* import { Bytes } from 'ox' | ||
* const data = Bytes.toString(Bytes.from([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33])) | ||
* // 'Hello world' | ||
*/ | ||
export function bytesToString( | ||
@@ -190,0 +195,0 @@ bytes_: Bytes, |
@@ -11,2 +11,19 @@ import { assertSize } from '../data/assertSize.js' | ||
export declare namespace toBytes { | ||
export type Options = { | ||
/** Size of the output bytes. */ | ||
size?: number | undefined | ||
} | ||
export type ErrorType = | ||
| numberToBytes.ErrorType | ||
| booleanToBytes.ErrorType | ||
| hexToBytes.ErrorType | ||
| stringToBytes.ErrorType | ||
| isBytes.ErrorType | ||
| isHex.ErrorType | ||
| InvalidTypeErrorType | ||
| ErrorType_ | ||
} | ||
/** | ||
@@ -30,18 +47,2 @@ * Encodes an arbitrary value to {@link Bytes}. | ||
*/ | ||
export declare namespace toBytes { | ||
export type Options = { | ||
/** Size of the output bytes. */ | ||
size?: number | undefined | ||
} | ||
export type ErrorType = | ||
| numberToBytes.ErrorType | ||
| booleanToBytes.ErrorType | ||
| hexToBytes.ErrorType | ||
| stringToBytes.ErrorType | ||
| isBytes.ErrorType | ||
| isHex.ErrorType | ||
| InvalidTypeErrorType | ||
| ErrorType_ | ||
} | ||
export function toBytes( | ||
@@ -61,2 +62,11 @@ value: string | bigint | number | boolean | Hex | Bytes | readonly number[], | ||
export declare namespace booleanToBytes { | ||
type Options = { | ||
/** Size of the output bytes. */ | ||
size?: number | undefined | ||
} | ||
type ErrorType = assertSize.ErrorType | padLeft.ErrorType | ErrorType_ | ||
} | ||
/** | ||
@@ -75,10 +85,2 @@ * Encodes a boolean value into {@link Bytes}. | ||
*/ | ||
export declare namespace booleanToBytes { | ||
type Options = { | ||
/** Size of the output bytes. */ | ||
size?: number | undefined | ||
} | ||
type ErrorType = assertSize.ErrorType | padLeft.ErrorType | ErrorType_ | ||
} | ||
export function booleanToBytes( | ||
@@ -118,2 +120,11 @@ value: boolean, | ||
export declare namespace hexToBytes { | ||
type Options = { | ||
/** Size of the output bytes. */ | ||
size?: number | undefined | ||
} | ||
type ErrorType = assertSize.ErrorType | padRight.ErrorType | ErrorType_ | ||
} | ||
/** | ||
@@ -137,10 +148,2 @@ * Encodes a hex value into {@link Bytes}. | ||
*/ | ||
export declare namespace hexToBytes { | ||
type Options = { | ||
/** Size of the output bytes. */ | ||
size?: number | undefined | ||
} | ||
type ErrorType = assertSize.ErrorType | padRight.ErrorType | ErrorType_ | ||
} | ||
export function hexToBytes(hex_: Hex, options: hexToBytes.Options = {}): Bytes { | ||
@@ -173,2 +176,10 @@ const { size } = options | ||
export declare namespace numberToBytes { | ||
export type Options = numberToHex.Options | ||
export type ErrorType = | ||
| numberToHex.ErrorType | ||
| hexToBytes.ErrorType | ||
| ErrorType_ | ||
} | ||
/** | ||
@@ -187,9 +198,2 @@ * Encodes a number value into {@link Bytes}. | ||
*/ | ||
export declare namespace numberToBytes { | ||
export type Options = numberToHex.Options | ||
export type ErrorType = | ||
| numberToHex.ErrorType | ||
| hexToBytes.ErrorType | ||
| ErrorType_ | ||
} | ||
export function numberToBytes( | ||
@@ -196,0 +200,0 @@ value: bigint | number, |
@@ -222,3 +222,2 @@ import { | ||
/** @internal */ | ||
export declare namespace createCursor { | ||
@@ -229,2 +228,4 @@ type Config = { recursiveReadLimit?: number | undefined } | ||
} | ||
/** @internal */ | ||
export function createCursor( | ||
@@ -231,0 +232,0 @@ bytes: Bytes, |
@@ -9,6 +9,7 @@ import { | ||
/** @internal */ | ||
export declare namespace assertSize { | ||
type ErrorType = size.ErrorType | SizeOverflowErrorType | ErrorType_ | ||
} | ||
/** @internal */ | ||
export function assertSize(hexOrBytes: Hex | Bytes, size_: number): void { | ||
@@ -15,0 +16,0 @@ if (size(hexOrBytes) > size_) |
import type { ErrorType as ErrorType_ } from '../errors/error.js' | ||
import type { Bytes, Hex } from '../types/data.js' | ||
export declare namespace concat { | ||
type ReturnType<value extends Hex | Bytes> = value extends Hex ? Hex : Bytes | ||
type ErrorType = concatBytes.ErrorType | concatHex.ErrorType | ErrorType_ | ||
} | ||
/** | ||
@@ -22,6 +27,2 @@ * Concatenates two or more {@link Bytes} or {@link Hex}. | ||
*/ | ||
export declare namespace concat { | ||
type ReturnType<value extends Hex | Bytes> = value extends Hex ? Hex : Bytes | ||
type ErrorType = concatBytes.ErrorType | concatHex.ErrorType | ErrorType_ | ||
} | ||
export function concat<value extends Hex | Bytes>( | ||
@@ -41,6 +42,7 @@ ...values: readonly value[] | ||
/** @internal */ | ||
export declare namespace concatBytes { | ||
type ErrorType = ErrorType_ | ||
} | ||
/** @internal */ | ||
function concatBytes(...values: readonly Bytes[]): Bytes { | ||
@@ -60,8 +62,9 @@ let length = 0 | ||
/** @internal */ | ||
export declare namespace concatHex { | ||
type ErrorType = ErrorType_ | ||
} | ||
/** @internal */ | ||
function concatHex(...values: readonly Hex[]): Hex { | ||
return `0x${(values as Hex[]).reduce((acc, x) => acc + x.replace('0x', ''), '')}` | ||
} |
import type { ErrorType as ErrorType_ } from '../errors/error.js' | ||
import type { Bytes } from '../types/data.js' | ||
export declare namespace isBytes { | ||
export type ErrorType = ErrorType_ | ||
} | ||
/** | ||
@@ -12,5 +16,2 @@ * Checks if the given value is {@link Bytes}. | ||
*/ | ||
export declare namespace isBytes { | ||
export type ErrorType = ErrorType_ | ||
} | ||
export function isBytes(value: unknown): value is Bytes { | ||
@@ -17,0 +18,0 @@ if (!value) return false |
@@ -8,2 +8,6 @@ import { equalBytes } from '@noble/curves/abstract/utils' | ||
export declare namespace isBytesEqual { | ||
type ErrorType = isHex.ErrorType | toBytes.ErrorType | ErrorType_ | ||
} | ||
/** | ||
@@ -22,5 +26,2 @@ * Checks if two {@link Bytes} or {@link Hex} values are equal. | ||
*/ | ||
export declare namespace isBytesEqual { | ||
type ErrorType = isHex.ErrorType | toBytes.ErrorType | ErrorType_ | ||
} | ||
export function isBytesEqual(a_: Bytes | Hex, b_: Bytes | Hex) { | ||
@@ -27,0 +28,0 @@ const a = isHex(a_, { strict: false }) ? toBytes(a_) : a_ |
import type { ErrorType as ErrorType_ } from '../errors/error.js' | ||
import type { Hex } from '../types/data.js' | ||
export declare namespace isHex { | ||
type Options = { | ||
strict?: boolean | undefined | ||
} | ||
type ErrorType = ErrorType_ | ||
} | ||
/** | ||
@@ -12,9 +20,2 @@ * Checks if the given value is {@link Hex}. | ||
*/ | ||
export declare namespace isHex { | ||
type Options = { | ||
strict?: boolean | undefined | ||
} | ||
type ErrorType = ErrorType_ | ||
} | ||
export function isHex( | ||
@@ -21,0 +22,0 @@ value: unknown, |
@@ -8,2 +8,7 @@ import { | ||
export declare namespace padLeft { | ||
type ReturnType<value extends Bytes | Hex> = pad.ReturnType<value> | ||
type ErrorType = pad.ErrorType | ErrorType_ | ||
} | ||
/** | ||
@@ -22,6 +27,2 @@ * Pads a {@link Bytes} or {@link Hex} value to the left with zero bytes until it reaches the given `size` (default: 32 bytes). | ||
*/ | ||
export declare namespace padLeft { | ||
type ReturnType<value extends Bytes | Hex> = pad.ReturnType<value> | ||
type ErrorType = pad.ErrorType | ErrorType_ | ||
} | ||
export function padLeft<value extends Bytes | Hex>( | ||
@@ -34,2 +35,7 @@ value: value, | ||
export declare namespace padRight { | ||
type ReturnType<value extends Bytes | Hex> = pad.ReturnType<value> | ||
type ErrorType = pad.ErrorType | ErrorType_ | ||
} | ||
/** | ||
@@ -48,6 +54,2 @@ * Pads a {@link Bytes} or {@link Hex} value to the right with zero bytes until it reaches the given `size` (default: 32 bytes). | ||
*/ | ||
export declare namespace padRight { | ||
type ReturnType<value extends Bytes | Hex> = pad.ReturnType<value> | ||
type ErrorType = pad.ErrorType | ErrorType_ | ||
} | ||
export function padRight<value extends Bytes | Hex>( | ||
@@ -54,0 +56,0 @@ value: value, |
import type { ErrorType as ErrorType_ } from '../errors/error.js' | ||
import type { Bytes } from '../types/data.js' | ||
export declare namespace randomBytes { | ||
type ErrorType = ErrorType_ | ||
} | ||
/** | ||
@@ -19,7 +23,4 @@ * Generates a random byte array of the specified length. | ||
*/ | ||
export declare namespace randomBytes { | ||
type ErrorType = ErrorType_ | ||
} | ||
export function randomBytes(length: number): Bytes { | ||
return crypto.getRandomValues(new Uint8Array(length)) | ||
} |
@@ -5,2 +5,6 @@ import type { ErrorType as ErrorType_ } from '../errors/error.js' | ||
export declare namespace size { | ||
export type ErrorType = isHex.ErrorType | ErrorType_ | ||
} | ||
/** | ||
@@ -22,5 +26,2 @@ * Retrieves the size of a {@link Hex} or {@link Bytes} value (in bytes). | ||
*/ | ||
export declare namespace size { | ||
export type ErrorType = isHex.ErrorType | ErrorType_ | ||
} | ||
export function size(value: Hex | Bytes) { | ||
@@ -27,0 +28,0 @@ if (isHex(value, { strict: false })) return Math.ceil((value.length - 2) / 2) |
@@ -10,2 +10,16 @@ import { | ||
export declare namespace slice { | ||
type Options = { | ||
strict?: boolean | undefined | ||
} | ||
type ReturnType<value extends Bytes | Hex> = value extends Hex ? Hex : Bytes | ||
type ErrorType = | ||
| isHex.ErrorType | ||
| sliceBytes.ErrorType | ||
| sliceHex.ErrorType | ||
| ErrorType_ | ||
} | ||
/** | ||
@@ -27,15 +41,2 @@ * Returns a section of a {@link Hex} or {@link Bytes} value given a start/end bytes offset. | ||
*/ | ||
export declare namespace slice { | ||
type Options = { | ||
strict?: boolean | undefined | ||
} | ||
type ReturnType<value extends Bytes | Hex> = value extends Hex ? Hex : Bytes | ||
type ErrorType = | ||
| isHex.ErrorType | ||
| sliceBytes.ErrorType | ||
| sliceHex.ErrorType | ||
| ErrorType_ | ||
} | ||
export function slice<value extends Bytes | Hex>( | ||
@@ -42,0 +43,0 @@ value: value, |
import type { ErrorType as ErrorType_ } from '../errors/error.js' | ||
import type { Bytes, Hex } from '../types/data.js' | ||
export declare namespace trimLeft { | ||
type ReturnType<value extends Bytes | Hex> = value extends Hex ? Hex : Bytes | ||
type ErrorType = trim.ErrorType | ErrorType_ | ||
} | ||
/** | ||
@@ -19,7 +25,2 @@ * Trims leading zeros from a {@link Bytes} or {@link Hex} value. | ||
*/ | ||
export declare namespace trimLeft { | ||
type ReturnType<value extends Bytes | Hex> = value extends Hex ? Hex : Bytes | ||
type ErrorType = trim.ErrorType | ErrorType_ | ||
} | ||
export function trimLeft<value extends Bytes | Hex>( | ||
@@ -31,2 +32,10 @@ value: value, | ||
export declare namespace trimRight { | ||
export type ReturnType<value extends Bytes | Hex> = value extends Hex | ||
? Hex | ||
: Bytes | ||
export type ErrorType = trim.ErrorType | ErrorType_ | ||
} | ||
/** | ||
@@ -47,9 +56,2 @@ * Trims trailing zeros from a {@link Bytes} or {@link Hex} value. | ||
*/ | ||
export declare namespace trimRight { | ||
export type ReturnType<value extends Bytes | Hex> = value extends Hex | ||
? Hex | ||
: Bytes | ||
export type ErrorType = trim.ErrorType | ErrorType_ | ||
} | ||
export function trimRight<value extends Bytes | Hex>( | ||
@@ -56,0 +58,0 @@ value: value, |
@@ -15,22 +15,2 @@ import { hexToBytes } from '../bytes/toBytes.js' | ||
/** | ||
* Decodes a {@link Hex} value into a string, number, bigint, boolean, or {@link Bytes}. | ||
* | ||
* @example | ||
* import { Hex } from 'ox' | ||
* Hex.to('0x1a4', 'number') | ||
* // 420 | ||
* | ||
* @example | ||
* import { Hex } from 'ox' | ||
* Hex.to('0x48656c6c6f20576f726c6421', 'string') | ||
* // 'Hello world' | ||
* | ||
* @example | ||
* import { Hex } from 'ox' | ||
* Hex.to('0x48656c6c6f20576f726c64210000000000000000000000000000000000000000', 'string', { | ||
* size: 32, | ||
* }) | ||
* // 'Hello world' | ||
*/ | ||
export declare namespace fromHex { | ||
@@ -58,2 +38,23 @@ type Options = { | ||
} | ||
/** | ||
* Decodes a {@link Hex} value into a string, number, bigint, boolean, or {@link Bytes}. | ||
* | ||
* @example | ||
* import { Hex } from 'ox' | ||
* Hex.to('0x1a4', 'number') | ||
* // 420 | ||
* | ||
* @example | ||
* import { Hex } from 'ox' | ||
* Hex.to('0x48656c6c6f20576f726c6421', 'string') | ||
* // 'Hello world' | ||
* | ||
* @example | ||
* import { Hex } from 'ox' | ||
* Hex.to('0x48656c6c6f20576f726c64210000000000000000000000000000000000000000', 'string', { | ||
* size: 32, | ||
* }) | ||
* // 'Hello world' | ||
*/ | ||
export function fromHex<to extends To>( | ||
@@ -76,2 +77,13 @@ hex: Hex, | ||
export declare namespace hexToBigInt { | ||
type Options = { | ||
/** Whether or not the number of a signed representation. */ | ||
signed?: boolean | undefined | ||
/** Size (in bytes) of the hex value. */ | ||
size?: number | undefined | ||
} | ||
type ErrorType = assertSize.ErrorType | ErrorType_ | ||
} | ||
/** | ||
@@ -90,12 +102,2 @@ * Decodes a {@link Hex} value into a BigInt. | ||
*/ | ||
export declare namespace hexToBigInt { | ||
type Options = { | ||
/** Whether or not the number of a signed representation. */ | ||
signed?: boolean | undefined | ||
/** Size (in bytes) of the hex value. */ | ||
size?: number | undefined | ||
} | ||
type ErrorType = assertSize.ErrorType | ErrorType_ | ||
} | ||
export function hexToBigInt( | ||
@@ -119,2 +121,15 @@ hex: Hex, | ||
export declare namespace hexToBoolean { | ||
type Options = { | ||
/** Size (in bytes) of the hex value. */ | ||
size?: number | undefined | ||
} | ||
type ErrorType = | ||
| assertSize.ErrorType | ||
| trimLeft.ErrorType | ||
| InvalidHexBooleanErrorType | ||
| ErrorType_ | ||
} | ||
/** | ||
@@ -133,14 +148,2 @@ * Decodes a {@link Hex} value into a boolean. | ||
*/ | ||
export declare namespace hexToBoolean { | ||
type Options = { | ||
/** Size (in bytes) of the hex value. */ | ||
size?: number | undefined | ||
} | ||
type ErrorType = | ||
| assertSize.ErrorType | ||
| trimLeft.ErrorType | ||
| InvalidHexBooleanErrorType | ||
| ErrorType_ | ||
} | ||
export function hexToBoolean( | ||
@@ -160,2 +163,8 @@ hex_: Hex, | ||
export declare namespace hexToNumber { | ||
type Options = hexToBigInt.Options | ||
type ErrorType = hexToBigInt.ErrorType | ErrorType_ | ||
} | ||
/** | ||
@@ -174,7 +183,2 @@ * Decodes a {@link Hex} value into a number. | ||
*/ | ||
export declare namespace hexToNumber { | ||
type Options = hexToBigInt.Options | ||
type ErrorType = hexToBigInt.ErrorType | ErrorType_ | ||
} | ||
export function hexToNumber( | ||
@@ -189,2 +193,15 @@ hex: Hex, | ||
export declare namespace hexToString { | ||
type Options = { | ||
/** Size (in bytes) of the hex value. */ | ||
size?: number | undefined | ||
} | ||
type ErrorType = | ||
| assertSize.ErrorType | ||
| hexToBytes.ErrorType | ||
| trimRight.ErrorType | ||
| ErrorType_ | ||
} | ||
/** | ||
@@ -205,14 +222,2 @@ * Decodes a {@link Hex} value into a UTF-8 string. | ||
*/ | ||
export declare namespace hexToString { | ||
type Options = { | ||
/** Size (in bytes) of the hex value. */ | ||
size?: number | undefined | ||
} | ||
type ErrorType = | ||
| assertSize.ErrorType | ||
| hexToBytes.ErrorType | ||
| trimRight.ErrorType | ||
| ErrorType_ | ||
} | ||
export function hexToString( | ||
@@ -219,0 +224,0 @@ hex: Hex, |
@@ -18,2 +18,18 @@ import { assertSize } from '../data/assertSize.js' | ||
export declare namespace toHex { | ||
type Parameters = { | ||
/** The size (in bytes) of the output hex value. */ | ||
size?: number | undefined | ||
} | ||
type ErrorType = | ||
| booleanToHex.ErrorType | ||
| bytesToHex.ErrorType | ||
| numberToHex.ErrorType | ||
| stringToHex.ErrorType | ||
| isHex.ErrorType | ||
| InvalidTypeErrorType | ||
| ErrorType_ | ||
} | ||
/** | ||
@@ -37,17 +53,2 @@ * Encodes an arbitrary value into a {@link Hex} value. | ||
*/ | ||
export declare namespace toHex { | ||
type Parameters = { | ||
/** The size (in bytes) of the output hex value. */ | ||
size?: number | undefined | ||
} | ||
type ErrorType = | ||
| booleanToHex.ErrorType | ||
| bytesToHex.ErrorType | ||
| numberToHex.ErrorType | ||
| stringToHex.ErrorType | ||
| isHex.ErrorType | ||
| InvalidTypeErrorType | ||
| ErrorType_ | ||
} | ||
export function toHex( | ||
@@ -67,2 +68,11 @@ value: string | number | bigint | boolean | readonly number[] | Bytes, | ||
export declare namespace booleanToHex { | ||
type Options = { | ||
/** The size (in bytes) of the output hex value. */ | ||
size?: number | undefined | ||
} | ||
type ErrorType = assertSize.ErrorType | padLeft.ErrorType | ErrorType_ | ||
} | ||
/** | ||
@@ -86,10 +96,2 @@ * Encodes a boolean into a {@link Hex} value. | ||
*/ | ||
export declare namespace booleanToHex { | ||
type Options = { | ||
/** The size (in bytes) of the output hex value. */ | ||
size?: number | undefined | ||
} | ||
type ErrorType = assertSize.ErrorType | padLeft.ErrorType | ErrorType_ | ||
} | ||
export function booleanToHex( | ||
@@ -107,2 +109,11 @@ value: boolean, | ||
export declare namespace bytesToHex { | ||
type Options = { | ||
/** The size (in bytes) of the output hex value. */ | ||
size?: number | undefined | ||
} | ||
type ErrorType = assertSize.ErrorType | padRight.ErrorType | ErrorType_ | ||
} | ||
/** | ||
@@ -126,10 +137,2 @@ * Encodes a {@link Bytes} value into a {@link Hex} value. | ||
*/ | ||
export declare namespace bytesToHex { | ||
type Options = { | ||
/** The size (in bytes) of the output hex value. */ | ||
size?: number | undefined | ||
} | ||
type ErrorType = assertSize.ErrorType | padRight.ErrorType | ErrorType_ | ||
} | ||
export function bytesToHex( | ||
@@ -150,15 +153,2 @@ value: Bytes, | ||
/** | ||
* Encodes a number or bigint into a {@link Hex} value. | ||
* | ||
* @example | ||
* import { Hex } from 'ox' | ||
* Hex.fromNumber(420) | ||
* // '0x1a4' | ||
* | ||
* @example | ||
* import { Hex } from 'ox' | ||
* Hex.fromNumber(420, { size: 32 }) | ||
* // '0x00000000000000000000000000000000000000000000000000000000000001a4' | ||
*/ | ||
export declare namespace numberToHex { | ||
@@ -180,2 +170,16 @@ type Options = | ||
} | ||
/** | ||
* Encodes a number or bigint into a {@link Hex} value. | ||
* | ||
* @example | ||
* import { Hex } from 'ox' | ||
* Hex.fromNumber(420) | ||
* // '0x1a4' | ||
* | ||
* @example | ||
* import { Hex } from 'ox' | ||
* Hex.fromNumber(420, { size: 32 }) | ||
* // '0x00000000000000000000000000000000000000000000000000000000000001a4' | ||
*/ | ||
export function numberToHex( | ||
@@ -220,2 +224,11 @@ value_: number | bigint, | ||
export declare namespace stringToHex { | ||
type Options = { | ||
/** The size (in bytes) of the output hex value. */ | ||
size?: number | undefined | ||
} | ||
type ErrorType = bytesToHex.ErrorType | ErrorType_ | ||
} | ||
/** | ||
@@ -234,10 +247,2 @@ * Encodes a UTF-8 string into a hex string | ||
*/ | ||
export declare namespace stringToHex { | ||
type Options = { | ||
/** The size (in bytes) of the output hex value. */ | ||
size?: number | undefined | ||
} | ||
type ErrorType = bytesToHex.ErrorType | ErrorType_ | ||
} | ||
export function stringToHex( | ||
@@ -244,0 +249,0 @@ value_: string, |
@@ -15,10 +15,2 @@ import { hexToBytes } from '../bytes/toBytes.js' | ||
/** | ||
* Decodes a Recursive-Length Prefix (RLP) value into a decoded {@link Bytes} or {@link Hex} value. | ||
* | ||
* @example | ||
* import { Rlp } from 'ox' | ||
* Rlp.decode('0x8b68656c6c6f20776f726c64') | ||
* // 0x68656c6c6f20776f726c64 | ||
*/ | ||
export declare namespace fromRlp { | ||
@@ -36,2 +28,11 @@ type ReturnType<to extends To> = | ||
} | ||
/** | ||
* Decodes a Recursive-Length Prefix (RLP) value into a decoded {@link Bytes} or {@link Hex} value. | ||
* | ||
* @example | ||
* import { Rlp } from 'ox' | ||
* Rlp.decode('0x8b68656c6c6f20776f726c64') | ||
* // 0x68656c6c6f20776f726c64 | ||
*/ | ||
export function fromRlp< | ||
@@ -60,2 +61,7 @@ value extends Bytes | Hex, | ||
export declare namespace rlpToBytes { | ||
type ErrorType = fromRlp.ErrorType | ||
type ReturnType = fromRlp.ReturnType<'bytes'> | ||
} | ||
/** | ||
@@ -69,6 +75,2 @@ * Decodes a Recursive-Length Prefix (RLP) value into a decoded {@link Bytes} value. | ||
*/ | ||
export declare namespace rlpToBytes { | ||
type ErrorType = fromRlp.ErrorType | ||
type ReturnType = fromRlp.ReturnType<'bytes'> | ||
} | ||
export function rlpToBytes(value: Bytes | Hex): rlpToBytes.ReturnType { | ||
@@ -78,2 +80,7 @@ return fromRlp(value, 'bytes') | ||
export declare namespace rlpToHex { | ||
type ErrorType = fromRlp.ErrorType | ||
type ReturnType = fromRlp.ReturnType<'hex'> | ||
} | ||
/** | ||
@@ -87,6 +94,2 @@ * Decodes a Recursive-Length Prefix (RLP) value into a decoded {@link Hex} value. | ||
*/ | ||
export declare namespace rlpToHex { | ||
type ErrorType = fromRlp.ErrorType | ||
type ReturnType = fromRlp.ReturnType<'hex'> | ||
} | ||
export function rlpToHex(value: Bytes | Hex): rlpToHex.ReturnType { | ||
@@ -93,0 +96,0 @@ return fromRlp(value, 'hex') |
@@ -17,10 +17,2 @@ import { hexToBytes } from '../bytes/toBytes.js' | ||
/** | ||
* Encodes a {@link Bytes} or {@link Hex} value into a Recursive-Length Prefix (RLP) value. | ||
* | ||
* @example | ||
* import { Rlp } from 'ox' | ||
* Rlp.encode('0x68656c6c6f20776f726c64') | ||
* // 0x8b68656c6c6f20776f726c64 | ||
*/ | ||
export declare namespace toRlp { | ||
@@ -37,2 +29,11 @@ type ReturnType<to extends To> = | ||
} | ||
/** | ||
* Encodes a {@link Bytes} or {@link Hex} value into a Recursive-Length Prefix (RLP) value. | ||
* | ||
* @example | ||
* import { Rlp } from 'ox' | ||
* Rlp.encode('0x68656c6c6f20776f726c64') | ||
* // 0x8b68656c6c6f20776f726c64 | ||
*/ | ||
export function toRlp< | ||
@@ -51,2 +52,7 @@ bytes extends RecursiveArray<Bytes> | RecursiveArray<Hex>, | ||
export declare namespace bytesToRlp { | ||
type ReturnType<to extends To> = toRlp.ReturnType<to> | ||
type ErrorType = toRlp.ErrorType | ErrorType_ | ||
} | ||
/** | ||
@@ -60,6 +66,2 @@ * Encodes a {@link Bytes} value into a Recursive-Length Prefix (RLP) value. | ||
*/ | ||
export declare namespace bytesToRlp { | ||
type ReturnType<to extends To> = toRlp.ReturnType<to> | ||
type ErrorType = toRlp.ErrorType | ErrorType_ | ||
} | ||
export function bytesToRlp<to extends To = 'bytes'>( | ||
@@ -72,2 +74,7 @@ bytes: RecursiveArray<Bytes>, | ||
export declare namespace hexToRlp { | ||
type ReturnType<to extends To> = toRlp.ReturnType<to> | ||
type ErrorType = toRlp.ErrorType | ErrorType_ | ||
} | ||
/** | ||
@@ -81,6 +88,2 @@ * Encodes a {@link Hex} value into a Recursive-Length Prefix (RLP) value. | ||
*/ | ||
export declare namespace hexToRlp { | ||
type ReturnType<to extends To> = toRlp.ReturnType<to> | ||
type ErrorType = toRlp.ErrorType | ErrorType_ | ||
} | ||
export function hexToRlp<to extends To = 'hex'>( | ||
@@ -87,0 +90,0 @@ hex: RecursiveArray<Hex>, |
{ | ||
"name": "ox", | ||
"description": "Ethereum Standard Library", | ||
"version": "0.0.0-20240815T034436", | ||
"version": "0.0.0-20240815T050956", | ||
"main": "./_cjs/index.js", | ||
@@ -6,0 +6,0 @@ "module": "./_esm/index.js", |
/** @internal */ | ||
export const version = '0.0.0-20240815T034436' | ||
export const version = '0.0.0-20240815T050956' |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
428526
7266