UNPKG

@ayonli/jsext

Version:

A JavaScript extension package for building strong and modern applications.

68 lines (67 loc) 2.08 kB
/** * Utilities for encoding and decoding binary representations like hex and * base64 strings. * @module */ /** * Encodes the given data to a hex string. * * @example * ```ts * import { encodeHex } from "@ayonli/jsext/encoding"; * * const hex = encodeHex("Hello, World!"); * console.log(hex); // "48656c6c6f2c20576f726c6421" * * const hex2 = encodeHex(new Uint8Array([72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33])); * console.log(hex2); // "48656c6c6f2c20576f726c6421" * ``` */ export declare function encodeHex(data: string | ArrayBufferLike | ArrayBufferView): string; /** * Decodes the given hex string to a byte array. * * @example * ```ts * import { decodeHex } from "@ayonli/jsext/encoding"; * * const data = decodeHex("48656c6c6f2c20576f726c6421"); * console.log(data); // Uint8Array(13) [ 72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33 ] */ export declare function decodeHex(hex: string): Uint8Array; /** * Encodes the given data to a base64 string. * * Unlike the built-in `btoa` function, this function is not limited to the * Latin1 range. * * @example * ```ts * import { encodeBase64 } from "@ayonli/jsext/encoding"; * * const base64 = encodeBase64("Hello, World!"); * console.log(base64); // "SGVsbG8sIFdvcmxkIQ==" * * const base64_2 = encodeBase64("你好,世界!"); * console.log(base64_2); // "5L2g5aW977yM5LiW55WM77yB" * ``` */ export declare function encodeBase64(data: string | ArrayBufferLike | Uint8Array): string; /** * Decodes the given base64 string to a byte array. * * Unlike the built-in `atob` function, this function is not limited to the * Latin1 range. * * @example * ```ts * import { decodeBase64 } from "@ayonli/jsext/encoding"; * * const data = decodeBase64("SGVsbG8sIFdvcmxkIQ=="); * console.log(new TextDecoder.decode(data)); // "Hello, World!" * * const data2 = decodeBase64("5L2g5aW977yM5LiW55WM77yB"); * console.log(new TextDecoder.decode(data2)); // "你好,世界!" * ``` */ export declare function decodeBase64(base64: string): Uint8Array;