├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── SECURITY.md ├── index.js ├── package.json └── test └── index.js /.gitignore: -------------------------------------------------------------------------------- 1 | lib-cov 2 | *.seed 3 | *.log 4 | *.csv 5 | *.dat 6 | *.out 7 | *.pid 8 | *.gz 9 | pids 10 | logs 11 | results 12 | npm-debug.log 13 | node_modules 14 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "0.10" 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2014 Forbes Lindesay 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in 11 | all copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # js-stringify 2 | 3 | Stringify an object so it can be safely inlined in JavaScript code 4 | 5 | [![Build Status](https://img.shields.io/travis/jadejs/js-stringify/master.svg)](https://travis-ci.org/jadejs/js-stringify) 6 | [![Dependency Status](https://img.shields.io/gemnasium/jadejs/js-stringify.svg)](https://gemnasium.com/jadejs/js-stringify) 7 | [![NPM version](https://img.shields.io/npm/v/js-stringify.svg)](https://www.npmjs.org/package/js-stringify) 8 | 9 | ## Installation 10 | 11 | npm install js-stringify 12 | 13 | ## Usage 14 | 15 | ```js 16 | 17 | var assert = require('assert'); 18 | var stringify = require('js-stringify'); 19 | 20 | assert(stringify('foo') === '"foo"'); 21 | assert(stringify('foo\u2028bar\u2029baz') === '"foo\\u2028bar\\u2029baz"'); 22 | assert(stringify(new Date('2014-12-19T03:42:00.000Z')) === 'new Date("2014-12-19T03:42:00.000Z")'); 23 | assert(stringify({foo: 'bar'}) === '{"foo":"bar"}'); 24 | assert(stringify(undefined) === 'undefined'); 25 | assert(stringify(null) === 'null'); 26 | assert( 27 | stringify({val: ""}) === 28 | '{"val":"\\u003C\\u002Fscript\\u003E\\u003Cscript\\u003Ealert(\'bad actor\')\\u003C\\u002Fscript\\u003E"}' 29 | ); 30 | ``` 31 | 32 | ## License 33 | 34 | MIT 35 | -------------------------------------------------------------------------------- /SECURITY.md: -------------------------------------------------------------------------------- 1 | ## Security contact information 2 | 3 | To report a security vulnerability, please use the 4 | [Tidelift security contact](https://tidelift.com/security). 5 | Tidelift will coordinate the fix and disclosure. 6 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = stringify; 4 | function stringify(obj) { 5 | if (obj instanceof Date) { 6 | return 'new Date(' + stringify(obj.toISOString()) + ')'; 7 | } 8 | if (obj === undefined) { 9 | return 'undefined'; 10 | } 11 | return JSON.stringify(obj) 12 | .replace(/\u2028/g, '\\u2028') 13 | .replace(/\u2029/g, '\\u2029') 14 | .replace(//g, '\\u003E') 16 | .replace(/\//g, '\\u002F'); 17 | } 18 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "js-stringify", 3 | "version": "1.0.2", 4 | "description": "Stringify an object so it can be safely inlined in JavaScript code", 5 | "keywords": [], 6 | "dependencies": {}, 7 | "devDependencies": {}, 8 | "scripts": { 9 | "test": "node test" 10 | }, 11 | "repository": { 12 | "type": "git", 13 | "url": "https://github.com/jadejs/js-stringify.git" 14 | }, 15 | "author": "ForbesLindesay", 16 | "license": "MIT" 17 | } -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var assert = require('assert'); 4 | var stringify = require('../'); 5 | 6 | assert(stringify('foo') === '"foo"'); 7 | assert(stringify('foo\u2028bar\u2029baz') === '"foo\\u2028bar\\u2029baz"'); 8 | assert(stringify(new Date('2014-12-19T03:42:00.000Z')) === 'new Date("2014-12-19T03:42:00.000Z")'); 9 | assert(stringify({foo: 'bar'}) === '{"foo":"bar"}'); 10 | assert(stringify(undefined) === 'undefined'); 11 | assert(stringify(null) === 'null'); 12 | assert( 13 | stringify({val: ""}) === 14 | '{"val":"\\u003C\\u002Fscript\\u003E\\u003Cscript\\u003Ealert(\'bad actor\')\\u003C\\u002Fscript\\u003E"}' 15 | ); 16 | 17 | console.log('tests passed'); 18 | --------------------------------------------------------------------------------