From 1ad8b9e54fe96c72c3225b63e4d389cbaa7a3843 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Sun, 28 Jul 2013 23:09:20 -0700 Subject: [PATCH] added case of empty object and empty array --- example.json | 4 +++- index.js | 10 ++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/example.json b/example.json index ff217aa..b681c1c 100644 --- a/example.json +++ b/example.json @@ -8,5 +8,7 @@ "grault": 1, "garply": true, "waldo": "false", - "fred": "undefined" + "fred": "undefined", + "emptyarr": [], + "emptyobj": {} } diff --git a/index.js b/index.js index 7ae30ca..c152b5f 100644 --- a/index.js +++ b/index.js @@ -34,6 +34,11 @@ var output = '' ; + if (0 === x.length) { + output += '[]'; + return output; + } + indentLevel = indentLevel.replace(/$/, ' '); x.forEach(function (y) { // TODO how should `undefined` be handled? @@ -55,6 +60,11 @@ var output = '' ; + if (0 === Object.keys(x).length) { + output += '{}'; + return output; + } + indentLevel = indentLevel.replace(/$/, ' '); Object.keys(x).forEach(function (k) { var val = x[k]