All files / src/utils text-decode-encode.util.js

90.24% Statements 74/82
76.47% Branches 13/17
28.57% Functions 2/7
90.24% Lines 74/82

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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 831x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x     2x 1x 1x 1x 1x 1x 1x 2x     2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x     2x 1x 1x 1x 1x 1x 1x 1x 2x     2x 2x 1x  
const encoder = new TextEncoder()
const decoder = new TextDecoder()
 
/**
 * @param {string} str - input text
 * @returns {Uint8Array} utf8 encoded byte array
 */
export const textToBytes = (str) => encoder.encode(str)
 
/**
 * @param {Uint8Array} bytes - utf8 encoded byte array
 * @returns {string} decoded output
 */
export const bytesToText = (bytes) => decoder.decode(bytes)
 
/**
 * @param {string} base64 - input base64 text
 * @returns {Uint8Array} byte array
 */
export const base64ToBytes = (() => {
  if(typeof Uint8Array.fromBase64 === "function"){
    return Uint8Array.fromBase64
  }
  return (/** @type {string} */ base64) => Uint8Array.from(atob(base64), (m) => /** @type {number} */(m.codePointAt(0)))
})()
/**
 * @param {Uint8Array} bytes - byte array
 * @returns {string} base64 text
 */
export const bytesToBase64 = (() => {
  if(typeof Uint8Array.prototype.toBase64 === "function"){
    return (/** @type {Uint8Array} */ bytes) => bytes.toBase64()
  }
  return (/** @type {Uint8Array} */ bytes) => btoa(Array.from(bytes, (x) => String.fromCodePoint(x)).join(''))
})()
 
/**
 * @param {string} str - input text
 * @returns {string} encoded input text in base64
 */
export const textToBase64 = (str) => bytesToBase64(encoder.encode(str))
 
/**
 * @param {string} base64 - input base64 text
 * @returns {string} decoded input text
 */
export const base64ToText = (base64) => decoder.decode(base64ToBytes(base64))
 
/**
 * @param {string} hex - hex string
 * @returns {string} converted string in base64
 */
export const hexToBase64 = (hex) => bytesToBase64(hexToBytes(hex))
 
/**
 * @param {string} base64 - input base64 text
 * @returns {string} converted string in hexadecimal
 */
export const base64ToHex = (base64) => bytesToHex(base64ToBytes(base64))
 
/**
 * @param {string} hex - hex string
 * @returns {Uint8Array} byte array
 */
export const hexToBytes = (() => {
  if(typeof Uint8Array.fromHex === "function"){
    return Uint8Array.fromHex
  }
  return (/** @type {string} */ hex) => Uint8Array.from({ length: hex.length >> 1 }, (_, i) => Number.parseInt(hex.slice(i, i + 2), 16))
})()
 
/**
 * @param {Uint8Array} bytes - byte array
 * @returns {string} hex string
 */
export const bytesToHex = (() => {
  if(typeof Uint8Array.prototype.toHex === "function"){
    return (/** @type {Uint8Array} */ bytes) => bytes.toHex()
  }
  const byteToHex = Array.from({ length: 0xff }, (_, i) => i.toString(16).padStart(2, '0'))
  return (/** @type {Uint8Array} */ bytes) => bytes.reduce((result, byte) => result + byteToHex[byte], '')
})()