encodingapis
Encodes and decodes strings to and from UTF-8 bytes using TextEncoder/TextDecoder.
Import: @rtorcato/browser-common/encodingapis
๐ MDN: TextEncoder ยท ๐ caniuse: textencoder
encodeUTF8/decodeUTF8 throw where TextEncoder/TextDecoder are
unsupported โ guard with isTextEncoderAvailable()/isTextDecoderAvailable() first.
Unlike most of this library, this module is environment-agnostic: TextEncoder and
TextDecoder are Node.js globals too, so it works outside the browser.
Browser supportโ
| API | Chrome | Edge | Firefox | Safari | iOS Safari | Baseline |
|---|---|---|---|---|---|---|
TextEncoder | โ 38+ | โ 79+ | โ 18+ | โ 10.1+ | โ 10.3+ | Widely available |
TextDecoder | โ 38+ | โ 79+ | โ 19+ | โ 10.1+ | โ 10.3+ | Widely available |
โ
supported ยท โ partial ยท โ not supported ยท [n] see note. Generated from MDN browser-compat-data and web-features. caniuse: TextEncoder ยท TextDecoder
Exampleโ
import {
isTextEncoderAvailable,
encodeUTF8,
decodeUTF8,
} from '@rtorcato/browser-common/encodingapis'
if (isTextEncoderAvailable()) {
const bytes = encodeUTF8('hello')
const text = decodeUTF8(bytes)
}
Exportsโ
isTextEncoderAvailable()โ feature check forTextEncoderisTextDecoderAvailable()โ feature check forTextDecoderencodeUTF8(input)โ encodes a string into aUint8ArraydecodeUTF8(bytes)โ decodes aUint8Arrayinto a string
See the API reference for full signatures.