npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

jsunicode

v1.2.4

Published

A JavaScript library for encoding/decoding unicode

Downloads

679

Readme

JSUnicode

Overview

JSUnicode is a set of JavaScript utilities for handling Unicode. JSUnicode's capabilities include:

  • Encode JavaScript strings as Unicode in binary representations (such as byte arrays, hex strings, and more)
  • Decode binary representations into JavaScript strings
  • Create custom binary representations to interop with other systems
  • Convert between representations
  • Built-in support for 5 Unicode encodings:
    • UTF-8
    • UTF-16 Big Endian
    • UTF-16 Little Endian
    • UTF-32 Big Endian
    • UTF-32 Little Endian

JSUnicode is designed to be small (requiring no runtime dependencies) and to work in node.js or in a browser. See doc/api-reference.md for complete documentation. If you encounter any problems, please file an issue on JSUnicode's github issues page. If there is a feature missing, you may want to take a look at doc/roadmap.md to see if it is planned in the future. If you do have any feature requests, please file an issue even if it is planned on the roadmap; that will help prioritize new features.

Example Usage

There are many reasons you may wish to encode or decode data in Unicode, but here are a few examples of use cases that may be common.

Byte Count

Often, data persistence layers have limits on encoded data storage. For instance, a database may specify that it stores VARCHAR fields in a UTF-8 collation, and a particular field may have a 200-byte limit. If your application accepts a user-input string that will be stored in such a field, you may wish to inform the user how many characters they have left in real-time, but JavaScript's string.length will be insufficient for this case, which provides a "character count" of sorts.

This use case is sufficiently common for JSUnicode to come equipped with a convenience function to provide byte counts for particular encodings. For instance, to get the byte count of the variable myString, you might do something like:

var jsunicode = require("jsunicode");

var byteCount = jsunicode.countEncodedBytes(myString);

This will default to UTF-8. If you're interested in the UTF-16 byte count instead, you could do something like:

var jsunicode = require("jsunicode");

var byteCount = jsunicode.countEncodedBytes(myString, jsunicode.constants.encoding.utf16);

Read UTF-16BE File

Node.js buffers only support reading UTF-16 as Little Endian; JSUnicode can be used to handle UTF-16BE. For instance, you may use code like the following to read a UTF-16LE file using Node's built-in functionality:

var fs = require("fs");

fs.readFile("./myfile.txt", {
    encoding: "utf-16le"
}, function (err, contents) { console.log(contents); });

If you want to use JSUnicode to read a UTF-16BE file, you might do something like this:

var fs = require("fs");
var jsunicode = require("jsunicode");

fs.readFile("./myfile.txt", function (err, contents) {
    console.log(jsunicode.decode(contents, {
        byteReader: jsunicode.constants.binaryFormat.buffer,
        encoding: jsunicode.constants.encoding.utf16be
    }));
});

Note that Byte order marks are now handled by JSUnicode. The default behavior should work for most cases, but refer to doc/api-reference.md for more.

Write UTF-16BE File

Similarly to the above example, it's also possible to take a string and write it to a UTF-16BE buffer (or any other binary format). A node example might look like:

var fs = require("fs");
var jsunicode = require("jsunicode");

fs.writeFile("./myfile.txt", jsunicode.encode(myString, {
    encoding: jsunicode.constants.encoding.utf16be,
    byteWriter: jsunicode.constants.binaryFormat.buffer
}), function (err) { console.log(err); })

For encoding, it's generally assumed that Byte Order Marks are not desired, so if you do want BOMs in your output, you may add for instance BOMBehavior: jsunicode.constants.BOMBehavior.auto to add BOMs for UTF-16. Again, see doc/api-reference.md for all the available options.