jsesc
Advanced tools
Comparing version 0.4.2 to 0.4.3
27
jsesc.js
@@ -1,2 +0,2 @@ | ||
/*! http://mths.be/jsesc v0.4.2 by @mathias */ | ||
/*! http://mths.be/jsesc v0.4.3 by @mathias */ | ||
;(function(root) { | ||
@@ -88,2 +88,3 @@ | ||
'wrap': false, | ||
'es6': false, | ||
'json': false, | ||
@@ -133,3 +134,4 @@ 'compact': true, | ||
// For some values (e.g. `undefined`, `function` objects), | ||
// `JSON.stringify(value)` returns `undefined` instead of `'null'`, | ||
// `JSON.stringify(value)` returns `undefined` (which isn’t valid | ||
// JSON) instead of `'null'`. | ||
return JSON.stringify(argument) || 'null'; | ||
@@ -165,5 +167,24 @@ } | ||
var length = string.length; | ||
var first; | ||
var second; | ||
var codePoint; | ||
result = ''; | ||
while (++index < length) { | ||
var character = string.charAt(index); | ||
if (options.es6) { | ||
first = string.charCodeAt(index); | ||
if ( // check if it’s the start of a surrogate pair | ||
first >= 0xD800 && first <= 0xDBFF && // high surrogate | ||
length > index + 1 // there is a next code unit | ||
) { | ||
second = string.charCodeAt(index + 1); | ||
if (second >= 0xDC00 && second <= 0xDFFF) { // low surrogate | ||
// http://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae | ||
codePoint = (first - 0xD800) * 0x400 + second - 0xDC00 + 0x10000; | ||
result += '\\u{' + codePoint.toString(16).toUpperCase() + '}'; | ||
index++; | ||
continue; | ||
} | ||
} | ||
} | ||
if (!options.escapeEverything) { | ||
@@ -212,3 +233,3 @@ if (regexWhitelist.test(character)) { | ||
jsesc.version = '0.4.2'; | ||
jsesc.version = '0.4.3'; | ||
@@ -215,0 +236,0 @@ /*--------------------------------------------------------------------------*/ |
{ | ||
"name": "jsesc", | ||
"version": "0.4.2", | ||
"version": "0.4.3", | ||
"description": "A JavaScript library for escaping JavaScript strings while generating the shortest possible valid output.", | ||
@@ -46,10 +46,10 @@ "homepage": "http://mths.be/jsesc", | ||
"grunt": "~0.4.1", | ||
"grunt-shell": "~0.3.1", | ||
"grunt-template": "~0.2.0", | ||
"istanbul": "~0.1.42", | ||
"grunt-shell": "~0.5.0", | ||
"grunt-template": "~0.2.1", | ||
"istanbul": "~0.1.44", | ||
"qunit-clib": "~1.3.0", | ||
"qunitjs": "~1.11.0", | ||
"regenerate": "~0.5.2", | ||
"requirejs": "~2.1.8" | ||
"regenerate": "~0.5.4", | ||
"requirejs": "~2.1.9" | ||
} | ||
} |
@@ -5,2 +5,4 @@ # jsesc [![Build status](https://travis-ci.org/mathiasbynens/jsesc.png?branch=master)](https://travis-ci.org/mathiasbynens/jsesc) [![Dependency status](https://gemnasium.com/mathiasbynens/jsesc.png)](https://gemnasium.com/mathiasbynens/jsesc) | ||
This can be used to avoid [mojibake](http://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](http://esdiscuss.org/topic/code-points-vs-unicode-scalar-values#content-14)) to a JavaScript parser or an UTF-8 encoder, respectively. | ||
Feel free to fork if you see possible improvements! | ||
@@ -159,2 +161,24 @@ | ||
#### `es6` | ||
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](http://mathiasbynens.be/notes/javascript-escapes#unicode-code-point) instead of using separate escape sequences for each surrogate half. If backwards compatibility with ES5 environments is a concern, don’t enable this setting. | ||
```js | ||
// By default, the `es6` option is disabled: | ||
jsesc('foo 𝌆 bar 💩 baz'); | ||
// → 'foo \\uD834\\uDF06 bar \\uD83D\\uDCA9 baz' | ||
// To explicitly disable it: | ||
jsesc('foo 𝌆 bar 💩 baz', { | ||
'es6': false | ||
}); | ||
// → 'foo \\uD834\\uDF06 bar \\uD83D\\uDCA9 baz' | ||
// To enable it: | ||
jsesc('foo 𝌆 bar 💩 baz', { | ||
'es6': true | ||
}); | ||
// → 'foo \\u{1D306} bar \\u{1F4A9} baz' | ||
``` | ||
#### `escapeEverything` | ||
@@ -161,0 +185,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
27429
236
376