README
js-name-escape
Escape strings to be used as javascript names.
Installation
npm i js-name-escape
Format
All characters except a-z
, A-Z
and _
are replaced with $x_
where x is the hexadecimal unicode code point.
Usage
import {escape, unescape} from 'js-name-escape'
escape('Hello World!') // -> Hello$20_World$21_
unescape('Hello$20_World$21_') // -> Hello World!
Inversion
Please note that unescape
is not the full inverse of escape
since invalid or partial escape codes are ignored when unescaping.
// For any given string, the following is true:
unescape(escape(x)) === x
// Example: foo! --> foo$21_ --> foo!
// but the following may be false:
escape(unescape(x)) === x
// Example: $bar --> $bar --> $21_bar
Benchmarks
The following are the latest benchmark results:
Node 10.9 (Windows 10 / 4.8GHz)
escape 32 chars x 314,639 ops/sec ±4.75% (93 runs sampled)
unescape 32 chars x 609,117 ops/sec ±1.19% (96 runs sampled)
Node 10.9 (Ubuntu 18 / 4.8GHz)
escape 32 chars x 472,569 ops/sec ±4.84% (93 runs sampled)
unescape 32 chars x 687,177 ops/sec ±1.01% (99 runs sampled)