Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

utf8-codec

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

utf8-codec

utf8 to/from bytes codec (esm/cjs)

  • 1.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
16K
increased by112.6%
Maintainers
1
Weekly downloads
 
Created
Source

utf8-codec

A javascript-only (esm/cjs) utf8 codec that is abstract-encoding compatible, well-tested and pretty efficient. Bonus: It doesn't use Nodejs' Buffer object and comes with typescript types.

Usage

import { encode, encodingLength, decode } from 'utf8-codec' // require works too!

const str = 'Hello World / こんにちは世界'
const bytes = encode(
  str,
  new Uint8Array(endcodingLength(str)), // own buffer supplied, optional
  0 // offset, at which to write the str, optional
)
str === decode(bytes, 0, bytes.length)

Why?

The TextEncoder and TextDecoder classes exist to encode utf8 strings but they are not optimized for bigger byte processing and don't offer APIs to figure out how preemptively how many bytes are supposed to be written/read. Surprisingly this algorithm is even faster.

The other implementations found at the time do either not implement the edge cases properly and/or both directions of the codec.

License

MIT

Keywords

FAQs

Package last updated on 30 May 2022

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc