This is a fast polyfill for TextEncoder
and TextDecoder
, which let you encode and decode JavaScript strings into UTF-8 bytes.
It is fast partially as it does not support any encodings aside UTF-8 (and note that natively, only TextDecoder
supports alternative encodings anyway).
Usage
Include the minified inside a script
tag or as an ES6 Module for its side-effects.
It will create TextEncoder
and TextDecoder
if the symbols are missing on window
.
<script src="node_modules/fast-text-encoding/text.min.js"></script>
<script type="module">
import './node_modules/fast-text-encoding/text.min.js';
// confidently do something with TextEncoder \o/
</script>
Note: Always include text.min.js
, as it's compiled to ES5 for older environments.
Node
Install via NPM or Yarn (name "fast-text-encoding"), and then import purely for side effects:
// don't need to save this anywhere, just require before use
require('fast-text-encoding');
const buffer = new TextEncoder().encode('Turn me into UTF-8!');
// buffer is now a Uint8Array of [84, 117, 114, 110, ...]
Supports
Built for IE11, Edge and Node environments. Not required for Chrome, Firefox etc, which have native implementations.
Release
Compile code with Closure Compiler.
// ==ClosureCompiler==
// @compilation_level ADVANCED_OPTIMIZATIONS
// @output_file_name text.min.js
// ==/ClosureCompiler==
// code here