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 🙏

© 2025 – Pkg Stats / Ryan Hefner

escape-unicode

v0.3.0

Published

Library to escape Unicode characters

Readme

escape-unicode

Build Status Downloads Release License

escape-unicode is a Node.js package for converting Unicode characters into their corresponding Unicode escapes ("\uxxxx" notation).

Install

Install using npm:

npm install --save escape-unicode

Usage

escapeUnicode(input[, options])

Converts characters within input to Unicode escapes.

The filter option can be specified to control which characters are converted, and the replacer option can be specified for more granular control over how specific characters are escaped.

Characters within the Basic Multilingual Plane (BMP) as well as surrogate pairs for characters outside BMP are supported.

Options

| Option | Type | Default | Description | |------------|------------|---------|----------------------------------------------------------------------------------------------------------------------------------------| | filter | Filter | None | A function used to determine which Unicode code points should be converted to Unicode escapes. | | replacer | Replacer | None | A function that returns a replacement string for an individual Unicode character represented by a specific Unicode code point, if any. |

Examples

import { escapeUnicode, isNotAscii, replaceChars } from "escape-unicode";

escapeUnicode("I love Unicode!");
//=> "\\u0049\\u0020\\u006c\\u006f\\u0076\\u0065\\u0020\\u0055\\u006e\\u0069\\u0063\\u006f\\u0064\\u0065\\u0021"
escapeUnicode("I ♥ Unicode!");
//=> "\\u0049\\u0020\\u2665\\u0020\\u0055\\u006e\\u0069\\u0063\\u006f\\u0064\\u0065\\u0021"
escapeUnicode("I ♥ Unicode!", { filter: isNotAscii });
//=> "I \\u2665 Unicode!"
escapeUnicode("I	♥	Unicode!", { filter: isNotAscii, replacer: replaceChars({ "\t": "\\t" }) });
//=> "I\\t\\u2665\\tUnicode!"
escapeUnicode("𠮷𠮾");
//=> "\\ud842\\udfb7\\ud842\\udfbe"

Filter(code, char)

A function that returns whether the specified Unicode code point should be converted to a Unicode escape.

There are a several built-in Filter functions provided.

composeFilter(...filters)

Returns a Filter composed of the specified filters that returns true only if any of the filters provided return a truthy value.

import { composeFilter, escapeUnicode, isNotAscii } from "escape-unicode";

const filter = composeFilter(isNotAscii, (code) => code === 0x0020);
escapeUnicode("I ♥ Unicode!", { filter });
//=> "I\\u0020\\u2665\\u0020Unicode!"

isAscii(code, char)

A Filter that returns whether the specified Unicode code point is valid in ASCII encoding.

ASCII covers code points 0x00-0x7F (0-127).

isNotAscii(code, char)

A Filter that returns whether the specified Unicode code point is not valid in ASCII encoding.

ASCII covers code points 0x00-0x7F (0-127).

isBmp(code, char)

A Filter that returns whether the specified Unicode code point is in the Basic Multilingual Plane (BMP).

BMP covers code points 0x0000-0xFFFF (0-65535) and represents characters that can be encoded in a single UTF-16 code unit.

isNotBmp(code, char)

A Filter that returns whether the specified Unicode code point is not in the Basic Multilingual Plane (BMP).

BMP covers code points 0x0000-0xFFFF (0-65535) and represents characters that can be encoded in a single UTF-16 code unit.

isLatin1(code, char)

A Filter that returns whether the specified Unicode code point is valid in Latin-1 (ISO 8859-1) encoding.

Latin-1 covers code points 0x00-0xFF (0-255).

isNotLatin1(code, char)

A Filter that returns whether the specified Unicode code point is not valid in Latin-1 (ISO 8859-1) encoding.

Latin-1 covers code points 0x00-0xFF (0-255).

Replacer(code, char)

A function that returns a replacement string for an individual Unicode character represented by a specific Unicode code point, if any.

If a non-empty string is returned, the Unicode character will be replaced by that string in the returned string instead of its Unicode escape.

If an empty string is returned, the Unicode character will be removed from the returned string. If either null or undefined are returned, the Unicode character will be replaced with its Unicode escape in the returned string.

There are a several built-in Replacer functions provided.

composeReplacer(...replacers)

Returns a Replacer composed of the specified replacers that returns the replacement string returned from the first Replacer to return a string, where possible.

import { composeReplacer, escapeUnicode, replaceChars, replaceCodes } from "escape-unicode";

const replacer = composeReplacer(
  replaceChars({ "\f": "\\f", "\n": "\\n", "\r": "\\r" }),
  replaceCodes({ 0x009: "\\t" }),
);
escapeUnicode("I	♥	Unicode!", { replacer });
//=> "\\u0049\\t\\u2665\\t\\u0055\\u006e\\u0069\\u0063\\u006f\\u0064\\u0065\\u0021"

replaceChar(char, replacement)

Returns a Replacer that returns the specified replacement string for the individual Unicode character provided.

import { escapeUnicode, replaceChar } from "escape-unicode";

escapeUnicode("I	♥	Unicode!", { replacer: replaceChar("\t", "\\t") });
//=> "\\u0049\\t\\u2665\\t\\u0055\\u006e\\u0069\\u0063\\u006f\\u0064\\u0065\\u0021"

replaceChars(...replacers)

Returns a Replacer that returns replacement strings looked up from the specified replacements, where possible.

The keys within replacements are expected to be the individual Unicode character.

import { escapeUnicode, replaceChars } from "escape-unicode";

const replacements = new Map([
  ["\f", "\\f"],
  ["\n", "\\n"],
  ["\r", "\\r"],
  ["\t", "\\t"],
]);
escapeUnicode("I	♥	Unicode!", { replacer: replaceChars(replacements) });
//=> "\\u0049\\t\\u2665\\t\\u0055\\u006e\\u0069\\u0063\\u006f\\u0064\\u0065\\u0021"

replaceCode(code, replacement)

Returns a Replacer that returns the specified replacement string for the Unicode code point representing the individual Unicode character provided.

import { escapeUnicode, replaceCode } from "escape-unicode";

escapeUnicode("I	♥	Unicode!", { replacer: replaceCode(0x0009, "\\t") });
//=> "\\u0049\\t\\u2665\\t\\u0055\\u006e\\u0069\\u0063\\u006f\\u0064\\u0065\\u0021"

replaceCodes(...replacers)

Returns a Replacer that returns replacement strings looked up from the specified replacements, where possible.

The keys within replacements are expected to be Unicode code points representing the Unicode characters.

import { escapeUnicode, replaceCodes } from "escape-unicode";

const replacements = {
  0x000c: "\\f",
  0x000a: "\\n",
  0x000d: "\\r",
  0x0009: "\\t",
};
escapeUnicode("I	♥	Unicode!", { replacer: replaceCodes(replacements) });
//=> "\\u0049\\t\\u2665\\t\\u0055\\u006e\\u0069\\u0063\\u006f\\u0064\\u0065\\u0021"

Related

Bugs

If you have any problems with this package or would like to see changes currently in development, you can do so here.

Contributors

If you want to contribute, you're a legend! Information on how you can do so can be found in CONTRIBUTING.md. We want your suggestions and pull requests!

A list of all contributors can be found in AUTHORS.md.

License

Copyright © 2025 neocotic

See LICENSE.md for more information on our MIT license.