Skip to content
velt/encoding exports

velt/encoding

API reference

Converts between bytes and text: Base64 (RFC 4648, standard and URL-safe alphabets), lowercase hex, and UTF-8. The functions work on u8[], Velt's byte array; Uint8Array.fromBytes(b) and xs.toBytes() convert to and from typed arrays. For a walkthrough, read the guide.

import { base64Encode, hexEncode, utf8Encode } from "velt/encoding";

Exports

base64DecodeDecodes standard Base64; padding is optional.base64DecodeStringDecodes Base64 to bytes and those as UTF-8.base64EncodeStandard Base64 (+/ alphabet) with = padding.base64EncodeStringBase64 of the UTF-8 bytes of s.base64UrlDecodeDecodes URL-safe Base64; padding is optional.base64UrlEncodeURL-safe Base64 (-_ alphabet, RFC 4648 §5) without padding.atobThe bytes the Base64 data encodes, as a string with one code unit per byte, as Node's atob (WHATWG forgiving-base64): ASCII whitespace anywhere is ignored, and so is the padding (but at most two =, only at the end, and only where they complete a group of four).btoaThe Base64 (standard alphabet, = padded) of data, whose code units are bytes, as Node's btoa.hexDecodeDecodes hex digits (either case).hexEncodeLowercase hex, two digits per byte.isAsciiWhether every character of s is ASCII (so its bytes are its UTF-16 code units).utf8ByteAtByte i of s's UTF-8 (as utf8Length counts them), read in place; a RangeError past it.utf8LengthThe number of bytes s keeps its text in: UTF-8, a lone surrogate as its three-byte form (Buffer.byteLength(s) for well-formed text).utf8SubstringThe text of bytes start to end of s's UTF-8 (as utf8ByteAt numbers them), shared with s where slice would share it.firstInvalidOffset of the first byte of data that starts an invalid UTF-8 sequence, or -1.utf8DecodeDecodes UTF-8.utf8DecodeLossyDecodes UTF-8, replacing each maximal invalid subsequence with U+FFFD (WHATWG / Unicode "substitution of maximal subparts", as TextDecoder and Rust's from_utf8_lossy do).utf8EncodeThe UTF-8 bytes of s (a copy).utf8ValidWhether data is valid UTF-8.validToStringBytes known to be valid UTF-8 (encoder output, repaired lossy input) as a string.EncodingErrorThrown for malformed Base64, hex or UTF-8 input.
function

base64Decode

#std/encoding/base64.vlt:21

Re-exported from velt/encoding/base64

function base64Decode(s: string): u8[]

Decodes standard Base64; padding is optional. Throws EncodingError on a character outside the alphabet or a length or padding that cannot come from whole bytes.

function

base64DecodeString

#std/encoding/base64.vlt:41

Re-exported from velt/encoding/base64

function base64DecodeString(s: string): string

Decodes Base64 to bytes and those as UTF-8. Throws EncodingError on bad Base64 or UTF-8.

function

base64Encode

#std/encoding/base64.vlt:13

Re-exported from velt/encoding/base64

function base64Encode(data: u8[]): string

Standard Base64 (+/ alphabet) with = padding.

function

base64EncodeString

#std/encoding/base64.vlt:36

Re-exported from velt/encoding/base64

function base64EncodeString(s: string): string

Base64 of the UTF-8 bytes of s.

function

base64UrlDecode

#std/encoding/base64.vlt:31

Re-exported from velt/encoding/base64

function base64UrlDecode(s: string): u8[]

Decodes URL-safe Base64; padding is optional. Throws EncodingError.

function

base64UrlEncode

#std/encoding/base64.vlt:26

Re-exported from velt/encoding/base64

function base64UrlEncode(data: u8[]): string

URL-safe Base64 (-_ alphabet, RFC 4648 §5) without padding.

Re-exported from velt/encoding/binary_string

function atob(data: string): string

The bytes the Base64 data encodes, as a string with one code unit per byte, as Node's atob (WHATWG forgiving-base64): ASCII whitespace anywhere is ignored, and so is the padding (but at most two =, only at the end, and only where they complete a group of four). Throws a DOMException named InvalidCharacterError on any other character and on a length that cannot come from whole bytes.

Re-exported from velt/encoding/binary_string

function btoa(data: string): string

The Base64 (standard alphabet, = padded) of data, whose code units are bytes, as Node's btoa. Throws a DOMException named InvalidCharacterError if a code unit is above 0xFF.

function

hexDecode

#std/encoding/hex.vlt:74

Re-exported from velt/encoding/hex

function hexDecode(s: string): u8[]

Decodes hex digits (either case). Throws EncodingError on odd length or a non-hex character.

function

hexEncode

#std/encoding/hex.vlt:38

Re-exported from velt/encoding/hex

function hexEncode(data: u8[]): string

Lowercase hex, two digits per byte.

Re-exported from velt/encoding/string_bytes

function isAscii(s: string): bool

Whether every character of s is ASCII (so its bytes are its UTF-16 code units). O(1).

Re-exported from velt/encoding/string_bytes

function utf8ByteAt(s: string, i: usize): u8

Byte i of s's UTF-8 (as utf8Length counts them), read in place; a RangeError past it.

Re-exported from velt/encoding/string_bytes

function utf8Length(s: string): usize

The number of bytes s keeps its text in: UTF-8, a lone surrogate as its three-byte form (Buffer.byteLength(s) for well-formed text). O(1).

Re-exported from velt/encoding/string_bytes

function utf8Substring(s: string, start: usize, end: usize): string

The text of bytes start to end of s's UTF-8 (as utf8ByteAt numbers them), shared with s where slice would share it. A RangeError unless start <= end <= utf8Length(s) and both are at the start of a character (or the end).

function

firstInvalid

#std/encoding/utf8.vlt:73

Re-exported from velt/encoding/utf8

function firstInvalid(data: u8[]): i64

Offset of the first byte of data that starts an invalid UTF-8 sequence, or -1.

function

utf8Decode

#std/encoding/utf8.vlt:31

Re-exported from velt/encoding/utf8

function utf8Decode(data: u8[]): string

Decodes UTF-8. Throws EncodingError if data is not valid UTF-8 (overlong forms, surrogates and code points above U+10FFFF are invalid).

function

utf8DecodeLossy

#std/encoding/utf8.vlt:43

Re-exported from velt/encoding/utf8

function utf8DecodeLossy(data: u8[]): string

Decodes UTF-8, replacing each maximal invalid subsequence with U+FFFD (WHATWG / Unicode "substitution of maximal subparts", as TextDecoder and Rust's from_utf8_lossy do).

function

utf8Encode

#std/encoding/utf8.vlt:23

Re-exported from velt/encoding/utf8

function utf8Encode(s: string): u8[]

The UTF-8 bytes of s (a copy).

function

utf8Valid

#std/encoding/utf8.vlt:68

Re-exported from velt/encoding/utf8

function utf8Valid(data: u8[]): bool

Whether data is valid UTF-8.

function

validToString

#std/encoding/utf8.vlt:128

Re-exported from velt/encoding/utf8

function validToString(bytes: u8[]): string

Bytes known to be valid UTF-8 (encoder output, repaired lossy input) as a string.

class

EncodingError

#std/encoding/errors.vlt:4

Re-exported from velt/encoding/errors

class EncodingError extends Error

Thrown for malformed Base64, hex or UTF-8 input.

constructor(message: string)No doc comment yet