From 9188b9138138508613b2b31b07f85726b0867734 Mon Sep 17 00:00:00 2001 From: Ian Ornelas Date: Sun, 28 Feb 2016 18:27:44 -0300 Subject: [PATCH] Implement indentation handling --- index.js | 47 ++++++++++++++++++++++++++++++++++------------- test/index.js | 10 ++++++++-- 2 files changed, 42 insertions(+), 15 deletions(-) diff --git a/index.js b/index.js index 57a5a82..0e579d7 100644 --- a/index.js +++ b/index.js @@ -1,43 +1,64 @@ -//TODO: handle reviver/dehydrate function like normal -//and handle indentation, like normal. +//TODO: handle reviver/dehydrate function like normal. //if anyone needs this... please send pull request. -exports.stringify = function stringify (o) { +function stringify (o, gap, indentation) { if(o && Buffer.isBuffer(o)) - return JSON.stringify(':base64:' + o.toString('base64')) + return JSON.stringify(':base64:' + o.toString('base64'), null, gap) if(o && o.toJSON) o = o.toJSON() if(o && 'object' === typeof o) { - var s = '' var array = Array.isArray(o) - s = array ? '[' : '{' + var s = array ? '[' : '{' var first = true + var childstr = '' + var breakindent = '\n' + gap.repeat(indentation + 1) for(var k in o) { var ignore = 'function' == typeof o[k] || (!array && 'undefined' === typeof o[k]) if(Object.hasOwnProperty.call(o, k) && !ignore) { - if(!first) - s += ',' + if(!first) { + childstr += ',' + if(gap) childstr += breakindent + } + first = false + if (array) { - s += stringify(o[k]) + childstr += stringify(o[k], gap, indentation + 1) } else if (o[k] !== void(0)) { - s += stringify(k) + ':' + stringify(o[k]) + childstr += stringify(k, gap, indentation + 1) + ':' + (gap ? ' ' : '') + stringify(o[k], gap, indentation + 1) } } } + if(childstr) { + if(gap) s += breakindent + s += childstr + if(gap) s += '\n' + gap.repeat(indentation) + } + s += array ? ']' : '}' return s } else if ('string' === typeof o) { - return JSON.stringify(/^:/.test(o) ? ':' + o : o) + return JSON.stringify(/^:/.test(o) ? ':' + o : o, null, gap) } else if ('undefined' === typeof o) { return 'null'; } else - return JSON.stringify(o) + return JSON.stringify(o, null, gap) +} + +exports.stringify = function(o, replacer, space) { + var gap = '' + if(typeof space == 'number') { + gap = ' '.repeat(Math.min(10, space)) + } else if(typeof space == 'string') { + gap = space.slice(0, 10) + } + + return stringify(o, gap, 0) } exports.parse = function (s) { @@ -46,7 +67,7 @@ exports.parse = function (s) { if(/^:base64:/.test(value)) return new Buffer(value.substring(8), 'base64') else - return /^:/.test(value) ? value.substring(1) : value + return /^:/.test(value) ? value.substring(1) : value } return value }) diff --git a/test/index.js b/test/index.js index 01dd831..a5f7166 100644 --- a/test/index.js +++ b/test/index.js @@ -38,17 +38,23 @@ var examples = { array: [undefined, 1, 'two'] }, fn: { - fn: function () {} + fn: function () {} } } for(k in examples) -(function (value, k) { +(function (value, k) { test(k, function (t) { var s = _JSON.stringify(value) console.log(s) var _value = _JSON.parse(s) t.deepEqual(clone(_value), clone(value)) + + s = _JSON.stringify(value, undefined, 4) + console.log(s) + _value = _JSON.parse(s) + t.deepEqual(clone(_value), clone(value)) + t.end() }) })(examples[k], k)