Skip to content

Commit a76068c

Browse files
committed
Merge pull request #11 from caridy/beautifier
close #10: adding beautification option to serialize()
2 parents e08289e + 48522ba commit a76068c

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ The above will produce the following string output:
4848
'{"str":"string","num":0,"obj":{"foo":"foo"},"arr":[1,2,3],"bool":true,"nil":null,"fn":function echo(arg) { return arg; },"re":/([^\\s]+)/g}'
4949
```
5050

51+
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.
52+
5153
### Automatic Escaping of HTML Characters
5254

5355
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.

index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ var UNICODE_CHARS = {
2525
'\u2029': '\\u2029'
2626
};
2727

28-
module.exports = function serialize(obj) {
28+
module.exports = function serialize(obj, space) {
2929
var functions = [];
3030
var regexps = [];
3131
var str;
@@ -43,7 +43,7 @@ module.exports = function serialize(obj) {
4343
}
4444

4545
return value;
46-
});
46+
}, space);
4747

4848
// Protects against `JSON.stringify()` returning `undefined`, by serializing
4949
// to the literal string: "undefined".

0 commit comments

Comments
 (0)