Package Exports
- serialize-javascript
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (serialize-javascript) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Serialize JavaScript
Serialize JavaScript to a superset of JSON that includes regular expressions and functions.
Overview
The code in this package began its life as an internal module to express-state. To expand its usefulness, it now lives as serialize-javascript — an independent package on npm.
You're probably wondering: What about JSON.stringify()!? We've found that sometimes we need to serialize JavaScript functions and regexps. A great example is a web app that uses client-side URL routing where the route definitions are regexps that need to be shared from the server to the client.
The string returned from this package's single export function is literal JavaScript which can be saved to a .js file, or be embedded into an HTML document by making the content of a <script> element. HTML charaters and JavaScript line terminators are escaped automatically.
Installation
Install using npm:
$ npm install serialize-javascriptUsage
var serialize = require('serialize');
serialize({
str : 'string',
num : 0,
obj : {foo: 'foo'},
arr : [1, 2, 3],
bool : true,
nil : null,
undef: undefined,
fn: function echo(arg) { return arg; },
re: /([^\s]+)/g
});The above will produce the following output:
'{"str":"string","num":0,"obj":{"foo":"foo"},"arr":[1,2,3],"bool":true,"nil":null,"fn":function echo(arg) { return arg; },"re":/([^\\s]+)/g}'Automatic Escaping of HTML Characters
A primary feature of this package is to serialize code to a string of literal JavaScript which can be embedded in an HTML document by adding it as the contents of the <script> element. In order to make this safe, HTML characters and JavaScript line terminators are escaped automatically.
serialize({
haxorXSS: '</script>'
});The above will produce the following, HTML-escaped output which is safe to put into an HTML document as it will not cause the inline script element to terminate:
'{"haxorXSS":"\\u003C\\u002Fscript\\u003E"}'License
This software is free to use under the Yahoo! Inc. BSD license. See the LICENSE file for license text and copyright information.