You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+5-2
Original file line number
Diff line number
Diff line change
@@ -11,12 +11,13 @@ Serialize JavaScript to a _superset_ of JSON that includes regular expressions,
11
11
12
12
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.
13
13
14
-
You're probably wondering: **What about `JSON.stringify()`!?** We've found that sometimes we need to serialize JavaScript **functions**, **regexps**or **dates**. 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. But this module is also great for communicating between node processes.
14
+
You're probably wondering: **What about `JSON.stringify()`!?** We've found that sometimes we need to serialize JavaScript **functions**, **regexps**, **dates**, **sets**or **maps**. 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. But this module is also great for communicating between node processes.
15
15
16
16
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.
17
17
18
18
> **HTML characters and JavaScript line terminators are escaped automatically.**
19
19
20
+
Please note that serialization for ES6 Sets & Maps requires support for `Array.from` (not available in IE or Node < 0.12), or an `Array.from` polyfill.
20
21
21
22
## Installation
22
23
@@ -40,6 +41,8 @@ serialize({
40
41
nil :null,
41
42
undef:undefined,
42
43
date:newDate("Thu, 28 Apr 2016 22:02:17 GMT"),
44
+
map:newMap([['hello', 'world']]),
45
+
set:newSet([123, 456]),
43
46
44
47
fn:functionecho(arg) { return arg; },
45
48
re:/([^\s]+)/g
@@ -49,7 +52,7 @@ serialize({
49
52
The above will produce the following string output:
Note: to produced a beautified string, you can pass an optional second argument to `serialize()` to define the number of spaces to be used for the indentation.
0 commit comments