Skip to content
This repository was archived by the owner on Oct 31, 2018. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 34 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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
})
Expand Down
10 changes: 8 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down