jsesc
Advanced tools
Comparing version 0.4.1 to 0.4.2
44
jsesc.js
@@ -1,3 +0,3 @@ | ||
/*! http://mths.be/jsesc v0.4.1 by @mathias */ | ||
;(function(root, evil) { | ||
/*! http://mths.be/jsesc v0.4.2 by @mathias */ | ||
;(function(root) { | ||
@@ -57,5 +57,2 @@ // Detect free variables `exports` | ||
}; | ||
var isRegExp = function(value) { | ||
return toString.call(value) == '[object RegExp]'; | ||
}; | ||
var isString = function(value) { | ||
@@ -82,3 +79,2 @@ return typeof value == 'string' || | ||
var regexSingleEscape = /["'\\\b\f\n\r\t]/; | ||
var regexEval = /['\n\r\u2028\u2029]/g; | ||
@@ -94,6 +90,12 @@ var regexDigit = /[0-9]/; | ||
'wrap': false, | ||
'json': false, | ||
'compact': true, | ||
'indent': '\t', | ||
'__indent': '', | ||
'__indent__': '' | ||
}; | ||
var json = options && options.json; | ||
if (json) { | ||
defaults.quotes = 'double'; | ||
defaults.wrap = true; | ||
} | ||
options = extend(defaults, options); | ||
@@ -103,7 +105,2 @@ if (options.quotes != 'single' && options.quotes != 'double') { | ||
} | ||
var json = options.json; | ||
if (json) { | ||
options.quotes = 'double'; | ||
options.wrap = true; | ||
} | ||
var quote = options.quotes == 'double' ? '"' : '\''; | ||
@@ -121,5 +118,5 @@ var compact = options.compact; | ||
options.wrap = true; | ||
oldIndent = options.__indent; | ||
oldIndent = options.__indent__; | ||
indent += oldIndent; | ||
options.__indent = indent; | ||
options.__indent__ = indent; | ||
forEach(argument, function(value) { | ||
@@ -137,13 +134,2 @@ isEmpty = false; | ||
(compact ? '' : oldIndent) + ']'; | ||
} else if (!json && isRegExp(argument)) { | ||
return '/' + jsesc( | ||
evil( | ||
'\'' + argument.source.replace(regexEval, jsesc) + '\'' | ||
), | ||
extend(options, { | ||
'wrap': false, | ||
'escapeEverything': false | ||
} | ||
)) + '/' + (argument.global ? 'g' : '') + | ||
(argument.ignoreCase ? 'i' : '') + (argument.multiline ? 'm' : ''); | ||
} else if (!isObject(argument)) { | ||
@@ -159,5 +145,5 @@ if (json) { | ||
options.wrap = true; | ||
oldIndent = options.__indent; | ||
oldIndent = options.__indent__; | ||
indent += oldIndent; | ||
options.__indent = indent; | ||
options.__indent__ = indent; | ||
forOwn(argument, function(key, value) { | ||
@@ -230,3 +216,3 @@ isEmpty = false; | ||
jsesc.version = '0.4.1'; | ||
jsesc.version = '0.4.2'; | ||
@@ -255,2 +241,2 @@ /*--------------------------------------------------------------------------*/ | ||
}(this, eval)); | ||
}(this)); |
{ | ||
"name": "jsesc", | ||
"version": "0.4.1", | ||
"version": "0.4.2", | ||
"description": "A JavaScript library for escaping JavaScript strings while generating the shortest possible valid output.", | ||
@@ -5,0 +5,0 @@ "homepage": "http://mths.be/jsesc", |
@@ -81,9 +81,5 @@ # 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) | ||
Instead of a string, the `value` can also be a regular expression, 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. | ||
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. | ||
```js | ||
// Escaping a regular expression | ||
jsesc(/©𝌆/g); | ||
// → '/\\xA9\\uD834\\uDF06/g' | ||
// Escaping an array | ||
@@ -190,4 +186,2 @@ jsesc([ | ||
This setting has no effect on the output for regular expressions. Those only use escape sequences for non-printable ASCII symbols and non-ASCII symbols, regardless of the value of the `escapeEverything` setting. | ||
#### `compact` | ||
@@ -240,7 +234,7 @@ | ||
This setting has no effect on the output for strings or regular expressions. | ||
This setting has no effect on the output for strings. | ||
#### `json` | ||
The `json` option takes a boolean value (`true` or `false`), and defaults to `false` (disabled). When enabled, the output is always valid JSON. [Hexadecimal character escape sequences](http://mathiasbynens.be/notes/javascript-escapes#hexadecimal) and [the `\v` or `\0` escape sequences](http://mathiasbynens.be/notes/javascript-escapes#single) will not be used. Setting `json: true` implies `quotes: 'double', wrap: true`. | ||
The `json` option takes a boolean value (`true` or `false`), and defaults to `false` (disabled). When enabled, the output is always valid JSON. [Hexadecimal character escape sequences](http://mathiasbynens.be/notes/javascript-escapes#hexadecimal) and [the `\v` or `\0` escape sequences](http://mathiasbynens.be/notes/javascript-escapes#single) will not be used. Setting `json: true` implies `quotes: 'double', wrap: true`, although these values can still be overridden if needed. | ||
@@ -269,5 +263,10 @@ ```js | ||
// → '["foo\\u0000bar",[1,"\\u00A9",{"foo":true,"qux":null}],42]' | ||
// Values that aren’t allowed in JSON are run through `JSON.stringify()`: | ||
jsesc([ undefined, -Infinity ], { | ||
'json': true | ||
}); | ||
// → '[null,null]' | ||
``` | ||
**Note:** Using this option on objects or arrays that contain non-string values relies on `JSON.parse()`. For legacy environments like IE ≤ 7, use [a `JSON` polyfill](http://bestiejs.github.io/json3/). | ||
**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](http://bestiejs.github.io/json3/). | ||
@@ -274,0 +273,0 @@ ### `jsesc.version` |
25163
215
352