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

utfx

v1.0.1

Published

A compact library to encode, decode and convert UTF8 / UTF16 in JavaScript.

Downloads

16,004

Readme

utfx - A compact library to encode, decode and convert UTF8 / UTF16 in JavaScript.

utfx is a compact library to encode, decode and convert UTF8 / UTF16 in JavaScript using arbitrary sources and destinations through the use of successively called functions, basically eliminating memory overhead.

The standalone library is also capable of using binary strings and arrays (with the usual overhead) and provides polyfills for String.fromCodePoint and String#codePointAt.

API

encodeUTF8(src, dst)

Encodes UTF8 code points to UTF8 bytes.

| Parameter | Type | Description |-----------------|-----------------|--------------- | src | function():(number | null) | number | Code points source, either as a function returning the next code point respectively null if there are no more code points left or a single numeric code point. | dst | function(number) | Bytes destination as a function successively called with the next byte

decodeUTF8(src, dst)

Decodes UTF8 bytes to UTF8 code points.

| Parameter | Type | Description |-----------------|-----------------|--------------- | src | function():(number | null) | Bytes source as a function returning the next byte respectively null if there are no more bytes left. | dst | function(number) | Code points destination as a function successively called with each decoded code point. | @throws | RangeError | If a starting byte is invalid in UTF8 | @throws | Error | If the last sequence is truncated. Has an array property bytes holding the remaining bytes.

UTF16toUTF8(src, dst)

Converts UTF16 characters to UTF8 code points.

| Parameter | Type | Description |-----------------|-----------------|--------------- | src | function():(number | null) | Characters source as a function returning the next char code respectively null if there are no more characters left. | dst | function(number) | Code points destination as a function successively called with each converted code point.

UTF8toUTF16(src, dst)

Converts UTF8 code points to UTF16 characters.

| Parameter | Type | Description |-----------------|-----------------|--------------- | src | function():(number | null) | number | Code points source, either as a function returning the next code point respectively null if there are no more code points left or a single numeric code point. | dst | function(number) | Characters destination as a function successively called with each converted char code. | @throws | RangeError | If a code point is out of range

encodeUTF16toUTF8(src, dst)

Converts and encodes UTF16 characters to UTF8 bytes.

| Parameter | Type | Description |-----------------|-----------------|--------------- | src | function():(number | null) | Characters source as a function returning the next char code respectively null if there are no more characters left. | dst | function(number) | Bytes destination as a function successively called with the next byte.

decodeUTF8toUTF16(src, dst)

Decodes and converts UTF8 bytes to UTF16 characters.

| Parameter | Type | Description |-----------------|-----------------|--------------- | src | function():(number | null) | Bytes source as a function returning the next byte respectively null if there are no more bytes left. | dst | function(number) | Characters destination as a function successively called with each converted char code. | @throws | RangeError | If a starting byte is invalid in UTF8 | @throws | Error | If the last sequence is truncated. Has an array property bytes holding the remaining bytes.

assertByte(b)

Asserts a byte value.

| Parameter | Type | Description |-----------------|-----------------|--------------- | b | number | 8bit byte value | @returns | number | Valid byte value | @throws | TypeError | If the byte value is invalid | @throws | RangeError | If the byte value is out of range

assertCharCode(c)

Asserts an UTF16 char code.

| Parameter | Type | Description |-----------------|-----------------|--------------- | c | number | UTF16 char code | @returns | number | Valid char code | @throws | TypeError | If the char code is invalid | @throws | RangeError | If the char code is out of range

assertCodePoint(cp)

Asserts an UTF8 code point.

| Parameter | Type | Description |-----------------|-----------------|--------------- | cp | number | UTF8 code point | @returns | number | Valid code point | @throws | TypeError | If the code point is invalid | @throws | RangeError | If the code point is out of range

calculateCodePoint(cp)

Calculates the byte length of an UTF8 code point.

| Parameter | Type | Description |-----------------|-----------------|--------------- | cp | number | UTF8 code point | @returns | number | Byte length

calculateUTF8(src)

Calculates the number of UTF8 bytes required to store UTF8 code points.

| Parameter | Type | Description |-----------------|-----------------|--------------- | src | function():(number | null) | Code points source as a function returning the next code point respectively null if there are no more code points left. | @returns | number | The number of UTF8 bytes required

calculateUTF16asUTF8(src)

Calculates the number of UTF8 code points respectively UTF8 bytes required to store UTF16 char codes.

| Parameter | Type | Description |-----------------|-----------------|--------------- | src | function():(number | null) | Characters source as a function returning the next char code respectively null if there are no more characters left. | @returns | !Array.<number> | The number of UTF8 code points at index 0 and the number of UTF8 bytes required at index 1.

arraySource(a)

Creates a source function for an array.

| Parameter | Type | Description |-----------------|-----------------|--------------- | a | !Array.<number> | Array to read from | @returns | function():(number | null) | Source function returning the next value respectively null if there are no more values left. | @throws | TypeError | If the argument is invalid

arrayDestination(a)

Creates a destination function for an array.

| Parameter | Type | Description |-----------------|-----------------|--------------- | a | !Array.<number> | Array to write to | @returns | function(number) | Destination function successively called with the next value. | @throws | TypeError | If the argument is invalid

stringSource(s)

Creates a source function for a string.

| Parameter | Type | Description |-----------------|-----------------|--------------- | s | string | String to read from | @returns | function():(number | null) | Source function returning the next char code respectively null if there are no more characters left. | @throws | TypeError | If the argument is invalid

stringDestination()

Creates a destination function for a string.

| Parameter | Type | Description |-----------------|-----------------|--------------- | @returns | function(number=):(undefined | string) | Destination function successively called with the next char code. Returns the final string when called without arguments.

fromCodePoint(var_args)

A polyfill for String.fromCodePoint.

| Parameter | Type | Description |-----------------|-----------------|--------------- | var_args | ...number | Code points | @returns | string | JavaScript string | @throws | TypeError | If arguments are invalid or a code point is invalid | @throws | RangeError | If a code point is out of range

codePointAt(s, i)

A polyfill for String#codePointAt.

| Parameter | Type | Description |-----------------|-----------------|--------------- | s | string | JavaScript string | i | number | Index | @returns | number | undefined | Code point or undefined if i is out of range | @throws | TypeError | If arguments are invalid

polyfill(override=)

Installs utfx as a polyfill for String.fromCodePoint and String#codePointAt if not implemented.

| Parameter | Type | Description |-----------------|-----------------|--------------- | override | boolean | Overrides an existing implementation if true, defaults to false | @returns | !Object.<string,>* | utfx namespace

Usage

  • node.js: npm install utfx

    var utfx = require("utfx");
    ...
  • Browser: <script src="/path/to/utfx.min.js"></script>

    var utfx = dcodeIO.utfx;
    ...
  • Require.js/AMD

    require.config({
        "paths": {
            "utfx": "/path/to/utfx.min.js"
        }
    });
    require(["utfx"], function(utfx) {
        ...
    }

Downloads

FAQ and examples

License

Apache License, Version 2.0