pax_global_header00006660000000000000000000000064126704155340014521gustar00rootroot0000000000000052 comment=66d3aff579a45fae65c3d9f8740e44009cdbda26 js-string-escape-1.0.1/000077500000000000000000000000001267041553400146765ustar00rootroot00000000000000js-string-escape-1.0.1/.gitignore000066400000000000000000000000451267041553400166650ustar00rootroot00000000000000# Installed npm modules node_modules js-string-escape-1.0.1/.travis.yml000066400000000000000000000001101267041553400167770ustar00rootroot00000000000000sudo: false language: node_js node_js: - "0.10" - "0.12" - "iojs" js-string-escape-1.0.1/CHANGELOG.md000066400000000000000000000002651267041553400165120ustar00rootroot00000000000000# master # 1.0.1 * Exclude unused files from npm distribution # 1.0.0 * No change; version bumped to indicate that this package is considered stable # 0.0.1 * Initial release js-string-escape-1.0.1/LICENSE000066400000000000000000000020621267041553400157030ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) 2013 Jo Liss Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. js-string-escape-1.0.1/README.md000066400000000000000000000022461267041553400161610ustar00rootroot00000000000000# js-string-escape [![Build Status](https://travis-ci.org/joliss/js-string-escape.png?branch=master)](https://travis-ci.org/joliss/js-string-escape) Escape any string to be a valid JavaScript string literal between double quotes or single quotes. ## Installation ``` npm install js-string-escape ``` ## Example If you need to generate JavaScript output, this library will help you safely put arbitrary data in JavaScript strings: ```js jsStringEscape = require('js-string-escape') console.log('"' + jsStringEscape('Quotes (\", \'), newlines (\n), etc.') + '"') // => "Quotes (\", \'), newlines (\n), etc." ``` In other words, given any string `s`, the following invariants hold: ```js eval('"' + jsStringEscape(s) + '"') === s eval("'" + jsStringEscape(s) + "'") === s ``` These `eval` expressions are safe with untrusted strings `s`. Non-strings will be cast to strings. ## Compliance This library has been checked against [ECMAScript 5.1](http://www.ecma-international.org/ecma-262/5.1/#sec-7.8.4) and tested against all Unicode code points. Note that the returned string is not necessarily valid JSON, since JSON disallows control characters, and `\'` is illegal in JSON. js-string-escape-1.0.1/index.js000066400000000000000000000012041267041553400163400ustar00rootroot00000000000000module.exports = function (string) { return ('' + string).replace(/["'\\\n\r\u2028\u2029]/g, function (character) { // Escape all characters not included in SingleStringCharacters and // DoubleStringCharacters on // http://www.ecma-international.org/ecma-262/5.1/#sec-7.8.4 switch (character) { case '"': case "'": case '\\': return '\\' + character // Four possible LineTerminator characters need to be escaped: case '\n': return '\\n' case '\r': return '\\r' case '\u2028': return '\\u2028' case '\u2029': return '\\u2029' } }) } js-string-escape-1.0.1/package.json000066400000000000000000000013161267041553400171650ustar00rootroot00000000000000{ "name": "js-string-escape", "version": "1.0.1", "description": "Escape strings for use as JavaScript string literals", "main": "index.js", "scripts": { "test": "tap test" }, "repository": { "type": "git", "url": "https://github.com/joliss/js-string-escape" }, "keywords": [ "string", "escape", "backslash", "javascript", "ecmascript" ], "author": "Jo Liss ", "contributors": [ { "name": "Mathias Bynens", "url": "http://mathiasbynens.be/" } ], "license": "MIT", "devDependencies" : { "tap": "~> 0.4.2", "punycode": "~> 1.2.1" }, "engines": { "node": ">= 0.8" }, "files": [ "index.js" ] } js-string-escape-1.0.1/test/000077500000000000000000000000001267041553400156555ustar00rootroot00000000000000js-string-escape-1.0.1/test/test.js000066400000000000000000000026471267041553400172030ustar00rootroot00000000000000var test = require('tap').test var jsStringEscape = require('../') // Require the local copy of Punycode.js, as we don't want to use the outdated // version that shipped with Node v0.8: var punycode = require('./../node_modules/punycode/punycode.js') test('basic use', function (t) { t.equal(jsStringEscape('"Hello World!"'), '\\"Hello World!\\"') t.end() }) test('invariants', function (t) { var allCharacters = '' var i // The Punycode.js version that ships with Node v0.8 won't create unmatched // surrogate halves, so let's use `String.fromCharCode` for BMP code points. for (i = 0; i <= 0x00FFFF; i++) { allCharacters += String.fromCharCode(i) } // Generate strings based on astral code points. Trickier than it seems: // http://mathiasbynens.be/notes/javascript-encoding for (i = 0x010000; i <= 0x10FFFF; i++) { allCharacters += punycode.ucs2.encode([ i ]) } var escaped = jsStringEscape(allCharacters); // Do not use .equal; mega-diffs in the output are not helpful. t.ok(eval("'" + escaped + "'") === allCharacters) t.ok(eval('"' + escaped + '"') === allCharacters) t.end() }) test('supports arbitrary objects', function (t) { t.equal(jsStringEscape(null), 'null') t.equal(jsStringEscape(undefined), 'undefined') t.equal(jsStringEscape(false), 'false') t.equal(jsStringEscape(0.0), '0') t.equal(jsStringEscape({}), '[object Object]') t.equal(jsStringEscape(''), '') t.end() })