All files / src/utils utf8.ts

100% Statements 5/5
100% Branches 0/0
100% Functions 3/3
100% Lines 5/5

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 392x                                                       2x 1x   1x     1x        
import { decodeUTF8, encodeUTF8 } from './nacl-util-wrapper';
 
/**
 * Interface for UTF-8 string encoding and decoding operations.
 * Provides methods to convert between UTF-8 strings and binary data.
 */
export interface Utf8 {
    /**
     * Converts a UTF-8 string to a binary representation as Uint8Array.
     * @param input - UTF-8 string to be converted to binary
     * @returns Binary representation of the input string
     */
    decode(input: string): Uint8Array;
 
    /**
     * Converts binary data to a UTF-8 string.
     * @param input - Binary data to be converted to string
     * @returns UTF-8 string representation of the binary data
     */
    encode(input: Uint8Array): string;
}
 
/**
 * Factory function that creates and returns an implementation of the Utf8 interface.
 * Uses tweetnacl-util library for the underlying encoding/decoding operations.
 *
 * @returns An implementation of the Utf8 interface
 */
export function getUtf8(): Utf8 {
    return {
        decode(input: string): Uint8Array {
            return decodeUTF8(input);
        },
        encode(input: Uint8Array): string {
            return encodeUTF8(input);
        },
    };
}