sfcode
An Online Competing and Development Environment
|
This is a JavaScript library for escaping JavaScript strings while generating the shortest possible valid ASCII-only output. Here’s an online demo.
This can be used to avoid mojibake and other encoding issues, or even to avoid errors when passing JSON-formatted data (which may contain U+2028 LINE SEPARATOR, U+2029 PARAGRAPH SEPARATOR, or lone surrogates) to a JavaScript parser or an UTF-8 encoder, respectively.
Feel free to fork if you see possible improvements!
Via Bower:
Via Component:
Via npm:
In a browser:
In Narwhal:
In Rhino:
Using an AMD loader like RequireJS:
This function takes a value and returns an escaped version of the value where any characters that are not printable ASCII symbols are escaped using the shortest possible (but valid) escape sequences for use in JavaScript strings. The first supported value type is strings:
Instead of a string, the value
can also be an array, or an object. In such cases, jsesc
will return a stringified version of the value where any characters that are not printable ASCII symbols are escaped in the same way.
The optional options
argument accepts an object with the following options:
The default value for the quotes
option is ‘'single’. This means that any occurences of
'in the input string will be escaped as
\'`, so that the output can be used in a string literal wrapped in single quotes.
If you want to use the output as part of a string literal wrapped in double quotes, set the quotes
option to ‘'double’`.
This setting also affects the output for arrays and objects:
The wrap
option takes a boolean value (true
or false
), and defaults to false
(disabled). When enabled, the output will be a valid JavaScript string literal wrapped in quotes. The type of quotes can be specified through the quotes
setting.
The es6
option takes a boolean value (true
or false
), and defaults to false
(disabled). When enabled, any astral Unicode symbols in the input will be escaped using ECMAScript 6 Unicode code point escape sequences instead of using separate escape sequences for each surrogate half. If backwards compatibility with ES5 environments is a concern, don’t enable this setting. If the json
setting is enabled, the value for the es6
setting is ignored (as if it was false
).
The escapeEverything
option takes a boolean value (true
or false
), and defaults to false
(disabled). When enabled, all the symbols in the output will be escaped, even printable ASCII symbols.
This setting also affects the output for arrays and objects:
The compact
option takes a boolean value (true
or false
), and defaults to true
(enabled). When enabled, the output for arrays and objects will be as compact as possible; it won’t be formatted nicely.
This setting has no effect on the output for strings.
The indent
option takes a string value, and defaults to ‘’\t'. When the
compactsetting is enabled (
true), the value of the
indent` option is used to format the output for arrays and objects.
This setting has no effect on the output for strings.
The json
option takes a boolean value (true
or false
), and defaults to false
(disabled). When enabled, the output is valid JSON. Hexadecimal character escape sequences and the \v
or \0
escape sequences will not be used. Setting json: true
implies ‘quotes: 'double’, wrap: true, es6: false`, although these values can still be overridden if needed — but in such cases, the output won’t be valid JSON anymore.
Note: Using this option on objects or arrays that contain non-string values relies on JSON.stringify()
. For legacy environments like IE ≤ 7, use a JSON
polyfill.
A string representing the semantic version number.
To use the jsesc
binary in your shell, simply install jsesc globally using npm:
After that you will be able to escape strings from the command line:
To escape arrays or objects containing string values, use the -o
/--object
option:
To prettify the output in such cases, use the -p
/--pretty
option:
For valid JSON output, use the -j
/--json
option:
Read a local JSON file, escape any non-ASCII symbols, and save the result to a new file:
Or do the same with an online JSON file:
See jsesc --help
for the full list of options.
This library has been tested in at least Chrome 27-29, Firefox 3-22, Safari 4-6, Opera 10-12, IE 6-10, Node.js v0.10.0, Narwhal 0.3.2, RingoJS 0.8-0.9, PhantomJS 1.9.0, and Rhino 1.7RC4.
Note: Using the json
option on objects or arrays that contain non-string values relies on JSON.parse()
. For legacy environments like IE ≤ 7, use a JSON
polyfill.
After cloning this repository, run npm install
to install the dependencies needed for development and testing. You may want to install Istanbul globally using npm install istanbul -g
.
Once that’s done, you can run the unit tests in Node using npm test
or node tests/tests.js
. To run the tests in Rhino, Ringo, Narwhal, and web browsers as well, use grunt test
.
To generate the code coverage report, use grunt cover
.
Mathias Bynens |
This library is available under the MIT license.