diff --git a/README.md b/README.md index 657333f..5c84fc3 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,8 @@ assert(stringify('foo') === '"foo"'); assert(stringify('foo\u2028bar\u2029baz') === '"foo\\u2028bar\\u2029baz"'); assert(stringify(new Date('2014-12-19T03:42:00.000Z')) === 'new Date("2014-12-19T03:42:00.000Z")'); assert(stringify({foo: 'bar'}) === '{"foo":"bar"}'); +assert(stringify({foo: 'bar'}, 4) === '{\n "foo": "bar"\n}'); +assert(stringify({foo: 'bar'}, '\t') === '{\n\t"foo": "bar"\n}'); assert(stringify(undefined) === 'undefined'); assert(stringify(null) === 'null'); assert( diff --git a/index.js b/index.js index 8febfc4..d442c9b 100644 --- a/index.js +++ b/index.js @@ -1,14 +1,14 @@ 'use strict'; module.exports = stringify; -function stringify(obj) { +function stringify(obj, indentation) { if (obj instanceof Date) { return 'new Date(' + stringify(obj.toISOString()) + ')'; } if (obj === undefined) { return 'undefined'; } - return JSON.stringify(obj) + return JSON.stringify(obj, undefined, indentation) .replace(/\u2028/g, '\\u2028') .replace(/\u2029/g, '\\u2029') .replace(/