Goal: Encapsulate string utility classes to implement commonly used functions, such as checking whether strings are empty, converting strings to byte streams, etc.

Knowledge points:
The Buffer object is used to represent a fixed length byte sequence and is a dedicated cache area for storing binary data.
buffer.from: Create a new Buffer object based on the specified array.
BufferEncoding: Indicates the supported encoding format types.

util.TextEncoder

util.TextEncoder(encoding?: string);

Used to encode strings into byte arrays, supporting multiple encoding formats.
It should be noted that when using TextEncoder for encoding, the number of bytes occupied by characters varies under different encoding formats. When using TextEncoder, it is necessary to clearly specify the encoding format to be used to ensure the correct encoding result.

util.TextDecoder.create

static create(encoding?: string): TextEncoder

Method for creating TextEncoder object.

util.Base64Helper()
The Base64Helper class provides Base64 encoding and decoding as well as Base64 URL encoding and decoding functionality. The Base64 encoding table includes A-Z a-z、 The 62 characters from 0-9, as well as the two special characters'+'and'/'. When encoding, divide the raw data into groups of 3 bytes to obtain several 6-digit numbers, and then use the corresponding characters in the Base64 encoding table to represent these numbers. If there are 1 or 2 bytes remaining at the end, the '=' character needs to be used to fill in. The Base64 URL encoding table includes A-Z a-z、 0-9 and 64 characters' - 'and' _ ', Base64 URL encoding result does not contain'='.

Actual combat:

import { buffer, util } from "@kit.ArkTS";

/**
 * 字符串工具
 */
export class StringKit {
  /**
   * 字符串是否为空
   * @param str 被检测的字符串
   * @return 当字符串为undefined、null或者空字符串时,返回true,否则返回false
   */
  static isEmpty(str: string | undefined | null): boolean {
    return str == undefined || str == null || str == '';
  }

  /**
   * 字符串是否不为空
   * @param str 被检测的字符串
   * @returns 当字符串为非空字符串时,返回true,否则返回false
   */
  static isNotEmpty(str: string | undefined | null) {
    return !StringKit.isEmpty(str);
  }

  /**
   * 字符串转Uint8Array
   * @param str 字符串
   * @param encoding 编码,默认'utf-8'
   * @returns Uint8Array
   */
  public static stringToUint8Array(str: string, encoding: buffer.BufferEncoding = 'utf-8'): Uint8Array {
    const textEncoder = new util.TextEncoder(encoding);
    return textEncoder.encodeInto(str);
  }

  /**
   * Uint8Array转字符串
   * @param uint8Array Uint8Array
   * @param encoding 编码,默认'utf-8'
   * @returns 字符串
   */
  static uint8ArrayToString(uint8Array: Uint8Array, encoding: buffer.BufferEncoding = 'utf-8'): string {
    const textDecoder = util.TextDecoder.create(encoding, { ignoreBOM: true });
    return textDecoder.decodeToString(uint8Array);
  }

  /**
   * 字符串转Base64字符串
   * @param str 字符串
   * @returns Base64字符串
   */
  static stringToBase64(str: string): string {
    const uint8Array = StringKit.stringToUint8Array(str);
    const base64Helper = new util.Base64Helper();
    return base64Helper.encodeToStringSync(uint8Array);
  }

  /**
   * Base64字符串转字符串
   * @param base64Str Base64字符串
   * @returns 字符串
   */
  static base64ToString(base64: string): string {
    let base64Helper = new util.Base64Helper();
    const uint8Array = base64Helper.decodeSync(base64);
    return StringKit.uint8ArrayToString(uint8Array)
  }

  /**
   * 字符串转Buffer
   * @param str 字符串
   * @param encoding 编码,默认'utf-8'
   * @returns Buffer
   */
  static stringToBuffer(str: string, encoding: buffer.BufferEncoding = 'utf-8'): buffer.Buffer {
    return buffer.from(str, encoding);
  }

  /**
   * 字符串转ArrayBuffer
   * @param str 字符串
   * @param encoding 编码,默认'utf-8'
   * @returns ArrayBuffer
   */
  static stringToArrayBuffer(str: string, encoding: buffer.BufferEncoding = 'utf-8'): ArrayBuffer {
    return buffer.from(str, encoding).buffer;
  }

  /**
   * ArrayBuffer转字符串
   * @param arrayBuffer ArrayBuffer
   * @param encoding 编码,默认'utf-8'
   * @returns string
   */
  static arrayBufferToString(arrayBuffer: ArrayBuffer, encoding: buffer.BufferEncoding = 'utf-8'): string {
    return buffer.from(arrayBuffer).toString(encoding);
  }

  /**
   * ArrayBuffer转Uint8Array
   * @param arrayBuffer ArrayBuffer
   * @returns Uint8Array
   */
  static arrayBufferToUint8Array(arrayBuffer: ArrayBuffer): Uint8Array {
    return new Uint8Array(arrayBuffer)
  }

  /**
   * Uint8Array转ArrayBuffer
   * @param uint8Array
   * @returns ArrayBuffer
   */
  static unit8ArrayToArrayBuffer(uint8Array: Uint8Array): ArrayBuffer {
    return uint8Array.buffer as ArrayBuffer;
  }
}