From 565e48ba76bbcc2a181f7429943562b49866821b Mon Sep 17 00:00:00 2001 From: Eugene Ware Date: Mon, 14 Apr 2014 18:55:59 +0800 Subject: [PATCH 1/2] Failing test, JSON standard should make undefined in arrays be 'null' --- test/index.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/index.js b/test/index.js index 77cce5d..77825ab 100644 --- a/test/index.js +++ b/test/index.js @@ -32,6 +32,9 @@ var examples = { undefined: { empty: undefined }, + undefinedArray: { + array: [undefined, 1, 'two'] + }, fn: { fn: function () {} } From 29620e127465ace21966958d082eb40435237607 Mon Sep 17 00:00:00 2001 From: Eugene Ware Date: Mon, 14 Apr 2014 18:56:12 +0800 Subject: [PATCH 2/2] undefineds in arrays should be null --- index.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index c23c720..63699e9 100644 --- a/index.js +++ b/index.js @@ -19,11 +19,15 @@ exports.stringify = function stringify (o) { for(var k in o) { var isFunction = 'function' == typeof o[k] - if(Object.hasOwnProperty.call(o, k) && o[k] !== void(0) && !isFunction) { + if(Object.hasOwnProperty.call(o, k) && !isFunction) { if(!first) s += ',' first = false - s += array ? stringify(o[k]) : stringify(k) + ':' + stringify(o[k]) + if (array) { + s += stringify(o[k]) + } else if (o[k] !== void(0)) { + s += stringify(k) + ':' + stringify(o[k]) + } } } @@ -32,6 +36,8 @@ exports.stringify = function stringify (o) { return s } else if ('string' === typeof o) { return JSON.stringify(/^:/.test(o) ? ':' + o : o) + } else if ('undefined' === typeof o) { + return 'null'; } else return JSON.stringify(o) }