Socket
Socket
Sign inDemoInstall

fast-text-encoding

Package Overview
Dependencies
0
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    fast-text-encoding

Fast polyfill for TextEncoder and TextDecoder, only supports utf-8


Version published
Maintainers
1
Install size
33.0 kB
Created

Package description

What is fast-text-encoding?

The fast-text-encoding npm package is a polyfill for the TextEncoder and TextDecoder APIs, which are part of the Encoding Living Standard. These APIs provide a straightforward way to encode and decode text using various character encodings, primarily UTF-8. This package is particularly useful in environments where the native TextEncoder and TextDecoder are not available, such as older browsers or specific Node.js versions.

What are fast-text-encoding's main functionalities?

Text Encoding

This feature allows you to encode a JavaScript string into a Uint8Array of bytes using UTF-8 encoding. It's useful for preparing text data for transmission or storage in binary formats.

const textEncoder = new TextEncoder();
const encoded = textEncoder.encode('Hello, World!');
console.log(encoded);

Text Decoding

This feature enables you to decode a Uint8Array of bytes into a JavaScript string using UTF-8 or other supported encodings. It's commonly used for reading text data received in binary format.

const textDecoder = new TextDecoder();
const decoded = textDecoder.decode(new Uint8Array([72, 101, 108, 108, 111]));
console.log(decoded);

Other packages similar to fast-text-encoding

Readme

Source

Test

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). See some benchmarks.

Usage

Install as "fast-text-encoding" via your favourite package manager.

You only need this polyfill if you're supporting older browsers like IE, legacy Edge, ancient Chrome and Firefox, or Node before v11.

Browser

Include the minified code 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 or global.

<script src="node_modules/fast-text-encoding/text.min.js"></script>
<script type="module">
  import './node_modules/fast-text-encoding/text.min.js';
  import 'fast-text-encoding';  // or perhaps this
  // confidently do something with TextEncoder or TextDecoder \o/
</script>

⚠️ You'll probably want to depend on text.min.js, as it's compiled to ES5 for older environments.

Node

You only need this polyfill in Node before v11. However, you can use Buffer to provide the same functionality (but not conforming to any spec) in versions even older than that.

require('fast-text-encoding');  // just require me before use

const buffer = new TextEncoder().encode('Turn me into UTF-8!');
// buffer is now a Uint8Array of [84, 117, 114, 110, ...]

In Node v5.1 and above, this polyfill uses Buffer to implement TextDecoder.

Release

Compile code with Closure Compiler.

// ==ClosureCompiler==
// @compilation_level ADVANCED_OPTIMIZATIONS
// @output_file_name text.min.js
// ==/ClosureCompiler==

// code here

FAQs

Last updated on 20 Jun 2022

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc