Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

jsesc

Package Overview
Dependencies
Maintainers
1
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jsesc - npm Package Compare versions

Comparing version 2.4.0 to 2.5.0

22

jsesc.js

@@ -105,6 +105,15 @@ 'use strict';

options = extend(defaults, options);
if (options.quotes != 'single' && options.quotes != 'double') {
if (
options.quotes != 'single' &&
options.quotes != 'double' &&
options.quotes != 'backtick'
) {
options.quotes = 'single';
}
const quote = options.quotes == 'double' ? '"' : '\'';
const quote = options.quotes == 'double' ?
'"' :
(options.quotes == 'backtick' ?
'`' :
'\''
);
const compact = options.compact;

@@ -267,2 +276,6 @@ const lowercaseHex = options.lowercaseHex;

}
if (character == '`') {
result += quote == character ? '\\`' : character;
continue;
}
if (character == '\'') {

@@ -304,2 +317,5 @@ result += quote == character ? '\\\'' : character;

}
if (quote == '`') {
result = result.replace(/\$\{/g, '\\\$\{');
}
if (options.isScriptContext) {

@@ -314,4 +330,4 @@ // https://mathiasbynens.be/notes/etago

jsesc.version = '2.4.0';
jsesc.version = '2.5.0';
module.exports = jsesc;

11

package.json
{
"name": "jsesc",
"version": "2.4.0",
"description": "A JavaScript library for escaping JavaScript strings while generating the shortest possible valid output.",
"version": "2.5.0",
"description": "Given some data, jsesc returns the shortest possible stringified & ASCII-safe representation of that data.",
"homepage": "https://mths.be/jsesc",

@@ -13,5 +13,10 @@ "engines": {

"keywords": [
"string",
"buffer",
"escape",
"javascript",
"json",
"map",
"set",
"string",
"stringify",
"tool"

@@ -18,0 +23,0 @@ ],

# jsesc [![Build status](https://travis-ci.org/mathiasbynens/jsesc.svg?branch=master)](https://travis-ci.org/mathiasbynens/jsesc) [![Code coverage status](https://coveralls.io/repos/mathiasbynens/jsesc/badge.svg)](https://coveralls.io/r/mathiasbynens/jsesc) [![Dependency status](https://gemnasium.com/mathiasbynens/jsesc.svg)](https://gemnasium.com/mathiasbynens/jsesc)
This is a JavaScript library for [escaping JavaScript strings](https://mathiasbynens.be/notes/javascript-escapes) while generating the shortest possible valid ASCII-only output. [Here’s an online demo.](https://mothereff.in/js-escapes)
Given some data, _jsesc_ returns a stringified representation of that data. jsesc is similar to `JSON.stringify()` except:
This can be used to avoid [mojibake](https://en.wikipedia.org/wiki/Mojibake) and other encoding issues, or even to [avoid errors](https://twitter.com/annevk/status/380000829643571200) when passing JSON-formatted data (which may contain U+2028 LINE SEPARATOR, U+2029 PARAGRAPH SEPARATOR, or [lone surrogates](https://esdiscuss.org/topic/code-points-vs-unicode-scalar-values#content-14)) to a JavaScript parser or an UTF-8 encoder, respectively.
1. it outputs JavaScript instead of JSON [by default](#json), enabling support for data structures like ES6 maps and sets;
2. it offers [many options](#api) to customize the output;
3. its output is ASCII-safe [by default](#minimal), thanks to its use of [escape sequences](https://mathiasbynens.be/notes/javascript-escapes) where needed.
Feel free to fork if you see possible improvements!
For any input, jsesc generates the shortest possible valid printable-ASCII-only output. [Here’s an online demo.](https://mothereff.in/js-escapes)
jsesc’s output can be used instead of `JSON.stringify`’s to avoid [mojibake](https://en.wikipedia.org/wiki/Mojibake) and other encoding issues, or even to [avoid errors](https://twitter.com/annevk/status/380000829643571200) when passing JSON-formatted data (which may contain U+2028 LINE SEPARATOR, U+2029 PARAGRAPH SEPARATOR, or [lone surrogates](https://esdiscuss.org/topic/code-points-vs-unicode-scalar-values#content-14)) to a JavaScript parser or an UTF-8 encoder.
## Installation

@@ -60,10 +64,10 @@

```js
jsesc('Lorem ipsum "dolor" sit \'amet\' etc.');
jsesc('`Lorem` ipsum "dolor" sit \'amet\' etc.');
// → 'Lorem ipsum "dolor" sit \\\'amet\\\' etc.'
jsesc('Lorem ipsum "dolor" sit \'amet\' etc.', {
jsesc('`Lorem` ipsum "dolor" sit \'amet\' etc.', {
'quotes': 'single'
});
// → 'Lorem ipsum "dolor" sit \\\'amet\\\' etc.'
// → "Lorem ipsum \"dolor\" sit \\'amet\\' etc."
// → '`Lorem` ipsum "dolor" sit \\\'amet\\\' etc.'
// → "`Lorem` ipsum \"dolor\" sit \\'amet\\' etc."
```

@@ -74,9 +78,20 @@

```js
jsesc('Lorem ipsum "dolor" sit \'amet\' etc.', {
jsesc('`Lorem` ipsum "dolor" sit \'amet\' etc.', {
'quotes': 'double'
});
// → 'Lorem ipsum \\"dolor\\" sit \'amet\' etc.'
// → "Lorem ipsum \\\"dolor\\\" sit 'amet' etc."
// → '`Lorem` ipsum \\"dolor\\" sit \'amet\' etc.'
// → "`Lorem` ipsum \\\"dolor\\\" sit 'amet' etc."
```
If you want to use the output as part of a template literal (i.e. wrapped in backticks), set the `quotes` option to `'backtick'`.
```js
jsesc('`Lorem` ipsum "dolor" sit \'amet\' etc.', {
'quotes': 'backtick'
});
// → '\\`Lorem\\` ipsum "dolor" sit \'amet\' etc.'
// → "\\`Lorem\\` ipsum \"dolor\" sit 'amet' etc."
// → `\\\`Lorem\\\` ipsum "dolor" sit 'amet' etc.`
```
This setting also affects the output for arrays and objects:

@@ -83,0 +98,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc