utf8.js
1 const decoder = new TextDecoder() 2 const encoder = new TextEncoder() 3 4 /** 5 * @param {Uint8Array} value 6 * @returns {Iterable<string>} 7 */ 8 export const fromUTF8 = (value) => { 9 if (value instanceof Uint8Array) { 10 return [decoder.decode(value)] 11 } else { 12 return [] 13 } 14 } 15 16 /** 17 * @param {string} text 18 * @returns {Iterable<Uint8Array>} 19 */ 20 export const toUTF8 = (text) => { 21 if (typeof text === 'string') { 22 return [encoder.encode(text)] 23 } else { 24 return [] 25 } 26 }