Skip to content
Snippets Groups Projects
stringUtilities.js 1.18 KiB
Newer Older
  • Learn to ignore specific revisions
  • Alexey Lunin's avatar
    Alexey Lunin committed
    export const stringToUtf8ByteArray = str => {
      if (typeof str !== "string") {
    
        str = str.toString();
      }
    
    Alexey Lunin's avatar
    Alexey Lunin committed
      const res = Buffer.from(str, "utf-8");
    
    Alexey Lunin's avatar
    Alexey Lunin committed
    };
    
    Alexey Lunin's avatar
    Alexey Lunin committed
    export const utf8ByteArrayToString = ba => {
    
      if (!Buffer.isBuffer(ba)) {
        ba = Buffer.from(ba);
      }
    
    Alexey Lunin's avatar
    Alexey Lunin committed
      const res = ba.toString("utf-8");
    
    Alexey Lunin's avatar
    Alexey Lunin committed
    };
    
    Alexey Lunin's avatar
    Alexey Lunin committed
    export const stringToUtf8Base64 = str => {
    
      if (!Buffer.isBuffer(str)) {
    
    Alexey Lunin's avatar
    Alexey Lunin committed
        if (typeof str !== "string") {
    
          str = str.toString();
        }
    
    Alexey Lunin's avatar
    Alexey Lunin committed
        str = Buffer.from(str, "utf-8");
    
    Alexey Lunin's avatar
    Alexey Lunin committed
      const res = str.toString("base64");
    
    Alexey Lunin's avatar
    Alexey Lunin committed
    };
    
    Alexey Lunin's avatar
    Alexey Lunin committed
    export const utf8Base64ToString = strBase64 => {
    
      if (!Buffer.isBuffer(strBase64)) {
    
    Alexey Lunin's avatar
    Alexey Lunin committed
        if (typeof strBase64 !== "string") {
    
          strBase64 = strBase64.toString();
        }
    
    Alexey Lunin's avatar
    Alexey Lunin committed
        strBase64 = Buffer.from(strBase64, "base64");
    
    Alexey Lunin's avatar
    Alexey Lunin committed
      const res = strBase64.toString("utf-8");
    
    Alexey Lunin's avatar
    Alexey Lunin committed
    };
    
    Alexey Lunin's avatar
    Alexey Lunin committed
    export const base64ToByteArray = strBase64 => {
      if (typeof strBase64 !== "string") {
    
        strBase64 = strBase64.toString();
      }
    
    Alexey Lunin's avatar
    Alexey Lunin committed
      const res = Buffer.from(strBase64, "base64");
    
    Alexey Lunin's avatar
    Alexey Lunin committed
    };
    
    Alexey Lunin's avatar
    Alexey Lunin committed
    export const byteArrayToBase64 = ba => {
    
      if (!Buffer.isBuffer(ba)) {
        ba = Buffer.from(ba);
      }
    
    Alexey Lunin's avatar
    Alexey Lunin committed
      const res = ba.toString("base64");
    
    Alexey Lunin's avatar
    Alexey Lunin committed
    };