# unescape-unicode

[![Build Status](https://img.shields.io/github/actions/workflow/status/neocotic/unescape-unicode/ci.yml?event=push&style=for-the-badge)](https://github.com/neocotic/unescape-unicode/actions/workflows/ci.yml)
[![Downloads](https://img.shields.io/npm/dw/unescape-unicode?style=for-the-badge)](https://github.com/neocotic/unescape-unicode)
[![Release](https://img.shields.io/npm/v/unescape-unicode?style=for-the-badge)](https://github.com/neocotic/unescape-unicode)
[![License](https://img.shields.io/github/license/neocotic/unescape-unicode?style=for-the-badge)](https://github.com/neocotic/unescape-unicode/blob/main/LICENSE.md)

[unescape-unicode](https://github.com/neocotic/unescape-unicode) is a [Node.js](https://nodejs.org) package for
converting Unicode escapes ("\uxxxx" notation) into their corresponding Unicode characters.

## Install

Install using [npm](https://npmjs.com):

``` sh
npm install --save unescape-unicode
```

## Usage

### `unescapeUnicode(input[, options])`

Converts Unicode escapes within `input` to their corresponding characters.

Characters that are not part of Unicode escapes are included in the returned string as-is. This includes invalid or
incomplete Unicode escapes (e.g. `\uxxxx`) and any other unrelated escape sequences (e.g. `\t`). This can be controlled
by specifying the `replacer` option.

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

#### Options

| Option     | Type       | Default | Description                                                                                                                           |
|------------|------------|---------|---------------------------------------------------------------------------------------------------------------------------------------|
| `replacer` | `Replacer` | *None*  | A function that returns a replacement string for an individual escape character represented by a specific Unicode code point, if any. |

#### Examples

``` javascript
import { replaceChars, unescapeUnicode } from "unescape-unicode";

unescapeUnicode("I \\u2665 Unicode!");
//=> "I ♥ Unicode!"
unescapeUnicode("I\\t\\u2665\tUnicode!", { replacer: replaceChars({ f: "\f", n: "\n", r: "\r", t: "\t" }) });
//=> "I	♥	Unicode!"
unescapeUnicode("\\ud842\\udfb7\\ud842\\udfbe");
//=> "𠮷𠮾"
```

### `Replacer(code, char)`

A function that returns a replacement string for an individual escape character (i.e. the character immediately
following a backslash character) represented by the specified Unicode code point, if any.

If an empty string is returned, the escape sequence will be removed from the returned string. If either `null` or
`undefined` are returned, the escape sequence will be included in the returned string as-is.

This function is only passed `u` if not part of a valid Unicode escape sequence.

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.

``` javascript
import { composeReplacer, replaceChars, replaceUnescaped, unescapeUnicode } from "unescape-unicode";

const replacer = composeReplacer(
  replaceChars({ f: "\f", n: "\n", r: "\r", t: "\t" }),
  replaceUnescaped(),
);
unescapeUnicode("I\\t\\u2665\\tUnicode\\\\!", { replacer });
//=> "I	♥	Unicode\\!"
```

#### `replaceConstant(replacement)`

Returns a `Replacer` that always returns the specified `replacement`.

``` javascript
import { replaceConstant, unescapeUnicode } from "unescape-unicode";

unescapeUnicode("I\\t\\u2665\\tUnicode!", { replacer: replaceConstant("?") });
//=> "I?♥?Unicode!"
```

#### `replaceChar(char, replacement)`

Returns a `Replacer` that returns the specified `replacement` string for the individual escape character (i.e. the
character immediately following a backslash character) provided.

``` javascript
import { replaceChar, unescapeUnicode } from "unescape-unicode";

unescapeUnicode("I\\t\\u2665\\tUnicode!", { replacer: replaceChar("t", "\t") });
//=> "I	♥	Unicode!"
```

#### `replaceChars(replacements)`

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 escape characters (i.e. the character immediately
following a backslash character). It can either be provided as a `Map` or an object.

``` javascript
import { replaceChars, unescapeUnicode } from "unescape-unicode";

const replacements = {
  f: "\f",
  n: "\n",
  r: "\r",
  t: "\t",
};
unescapeUnicode("I\\t\\u2665\\tUnicode!", { replacer: replaceChars(replacements) });
//=> "I	♥	Unicode!"
```

#### `replaceCode(code, replacement)`

Returns a `Replacer` that returns the specified `replacement` string for the Unicode code point representing the
individual escape character (i.e. the character immediately following a backslash character) provided.

``` javascript
import { replaceCode, unescapeUnicode } from "unescape-unicode";

unescapeUnicode("I\\t\\u2665\\tUnicode!", { replacer: replaceCode(0x0074, "\t") });
//=> "I	♥	Unicode!"
```

#### `replaceCodes(replacements)`

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 individual escape characters
(i.e. the character immediately following a backslash character). It can either be provided as a `Map` or an object.

``` javascript
import { replaceCodes, unescapeUnicode } from "unescape-unicode";

const replacements = new Map([
  [0x0066, "\f"],
  [0x006e, "\n"],
  [0x0072, "\r"],
  [0x0074, "\t"],
]);
unescapeUnicode("I\\t\\u2665\\tUnicode!", { replacer: replaceCodes(replacements) });
//=> "I	♥	Unicode!"
```

#### `replaceUnescaped()`

Returns a `Replacer` that always returns the individual escape character (i.e. the character immediately following a
backslash character) as the replacement string.

``` javascript
import { replaceUnescaped, unescapeUnicode } from "unescape-unicode";

unescapeUnicode("I\\t\\u2665\\tUnicode!", { replacer: replaceUnescaped() });
//=> "It♥tUnicode!"
```

## Related

* [escape-unicode](https://github.com/neocotic/escape-unicode)
* [node-native2ascii](https://github.com/neocotic/node-native2ascii)
* [properties-store](https://github.com/neocotic/properties-store)

## Bugs

If you have any problems with this package or would like to see changes currently in development, you can do so
[here](https://github.com/neocotic/unescape-unicode/issues).

## Contributors

If you want to contribute, you're a legend! Information on how you can do so can be found in
[CONTRIBUTING.md](https://github.com/neocotic/unescape-unicode/blob/main/CONTRIBUTING.md). We want your suggestions and
pull requests!

A list of all contributors can be found in
[AUTHORS.md](https://github.com/neocotic/unescape-unicode/blob/main/AUTHORS.md).

## License

Copyright © 2025 neocotic

See [LICENSE.md](https://github.com/neocotic/unescape-unicode/raw/main/LICENSE.md) for more information on our MIT
license.
