From 08e1d71f2bfc9e35878e9c6194c9d876b88736d7 Mon Sep 17 00:00:00 2001 From: DungSaga Date: Fri, 24 Sep 2021 21:39:31 +0700 Subject: [PATCH] detect 'null' & 'undefined' as separate types to allow displaying 'null' & 'undefined' values differently --- JSONGrid.js | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/JSONGrid.js b/JSONGrid.js index 71fb5bf..1af9bed 100644 --- a/JSONGrid.js +++ b/JSONGrid.js @@ -80,10 +80,7 @@ JSONGrid.prototype.processArray = function () { keys.forEach(function (key, keyIdx) { var td = DOMHelper.createElement('td', typeof obj, 'table-wrapper'); - var value = (obj[key] === undefined || obj[key] === null) - ? '' + obj[key] - : obj[key] - ; + var value = obj[key]; td.appendChild(new JSONGrid(value).generateDOM()); tr.appendChild(td); }); @@ -140,14 +137,15 @@ JSONGrid.prototype.processObject = function () { JSONGrid.prototype.generateDOM = function () { var dom; + var dataType = this.data === null ? 'null' : typeof this.data; // typeof null is also 'object' if (Array.isArray(this.data)) { dom = this.processArray(); - } else if (typeof this.data === 'object') { + } else if (dataType === 'object') { dom = this.processObject(); } else { // -- Create a span element and early return since this is a "leaf" - var span = DOMHelper.createElement('span', typeof this.data); + var span = DOMHelper.createElement('span', dataType); span.textContent = '' + this.data; return span; } @@ -185,4 +183,4 @@ JSONGrid.prototype.render = function () { this.container.classList.add(DOMHelper.JSON_GRID_CONTAINER_CLASSNAME); }; -window.JSONGrid = JSONGrid; \ No newline at end of file +window.JSONGrid = JSONGrid;