jsesc
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|
jsesc is a highly useful JavaScript library that takes any kind of data and returns a stringified representation of it. Unlike JSON.stringify(), it can generate JavaScript-based outputs and provide ASCII-safe outputs by default, thanks to its usage of escape sequences when necessary. jsesc is able to generate the shortest possible valid printable-ASCII-only output and can be used as an alternative to JSON.stringify() to avoid encoding issues or errors when transferring JSON-formatted data. Particularly, jsesc can ensure smooth handling of specific Unicode characters that may cause issues with JSON, such as U+2028 LINE SEPARATOR, U+2029 PARAGRAPH SEPARATOR, or lone surrogates. Above all, it is highly customizable offering many options to customize the output representation.
To use jsesc in your Node.js program, you first need to install it via npm using the command npm install jsesc
. Once it is installed, you can require it in your program using const jsesc = require('jsesc');
. Sending data through the jsesc()
function will return a stringified version where all non-printable ASCII symbols are escaped.
Here are some examples:
const jsesc = require('jsesc');
jsesc('Ich ♥ Bücher');
// Output: 'Ich \\u2665 B\\xFCcher'
jsesc('foo 𝌆 bar');
// Output: 'foo \\uD834\\uDF06 bar'
// For arrays, objects, map, set, or buffer, jsesc acts similarly:
// For an array
jsesc([
'Ich ♥ Bücher', 'foo 𝌆 bar'
]);
// Output: '[\'Ich \\u2665 B\\xFCcher\',\'foo \\uD834\\uDF06 bar\']'
// For an object
jsesc({
'Ich ♥ Bücher': 'foo 𝌆 bar'
});
// Output: '{\'Ich \\u2665 B\\xFCcher\':\'foo \\uD834\\uDF06 bar\'}'
The complete documentation and usage guidelines for jsesc are included in the readme on the jsesc GitHub page. This contains a detailed explanation of each option and its usage along with examples. Series of executable examples are also included in the readme for a better understanding of the library's functionalities.