From abc6daf2db54b3845fa96edb087a69138f243283 Mon Sep 17 00:00:00 2001 From: Dan VerWeire Date: Sat, 15 Sep 2018 13:42:24 -0400 Subject: [PATCH] Add defaults() function (issue #3) This function accepts a config object with 'classes' and 'classPrefix' options. --- index.js | 217 +++++++++++++++++++++++++++++-------------------------- test.js | 7 ++ 2 files changed, 123 insertions(+), 101 deletions(-) diff --git a/index.js b/index.js index 4241d90..68b32fb 100644 --- a/index.js +++ b/index.js @@ -1,129 +1,144 @@ "use strict"; -module.exports = tableify; +module.exports = init({ -function tableify(obj, columns, parents) { - var buf = []; - var type = typeof obj; - var cols; +}); - parents = parents || []; +module.exports.defaults = init; - if (type !== 'object' || obj == null || obj == undefined) { - } - else if (~parents.indexOf(obj)) { - return "[Circular]"; - } - else { - parents.push(obj); - } +function init(config) { + var classes = config.classes === false ? false : true; + var classPrefix = config.classPrefix || ""; + + return function tableify(obj, columns, parents) { + var buf = []; + var type = typeof obj; + var cols; - if (Array.isArray(obj)) { - if (Array.isArray(obj[0]) && obj.every(Array.isArray)) { - buf.push('',''); - cols = []; - - // 2D array is an array of rows - obj.forEach(function (row, ix) { - cols.push(ix); + parents = parents || []; - buf.push(''); + if (type !== 'object' || obj == null || obj == undefined) { + } + else if (~parents.indexOf(obj)) { + return "[Circular]"; + } + else { + parents.push(obj); + } + + if (Array.isArray(obj)) { + if (Array.isArray(obj[0]) && obj.every(Array.isArray)) { + buf.push('
',''); + cols = []; - row.forEach(function (val) { - buf.push('', tableify(val, cols, parents), '') + // 2D array is an array of rows + obj.forEach(function (row, ix) { + cols.push(ix); + + buf.push(''); + + row.forEach(function (val) { + buf.push('', tableify(val, cols, parents), '') + }); + + buf.push(''); }); - buf.push(''); - }); - - buf.push('','
'); - } - else if (typeof obj[0] === 'object') { - buf.push('','',''); - - //loop through every object and get unique keys - var keys = {}; - obj.forEach(function (o) { - if (typeof o === 'object' && !Array.isArray(o)) { - Object.keys(o).forEach(function (k) { - keys[k] = true; - }); - } - }); + buf.push('','
'); + } + else if (typeof obj[0] === 'object') { + buf.push('','',''); + + //loop through every object and get unique keys + var keys = {}; + obj.forEach(function (o) { + if (typeof o === 'object' && !Array.isArray(o)) { + Object.keys(o).forEach(function (k) { + keys[k] = true; + }); + } + }); + + cols = Object.keys(keys); - cols = Object.keys(keys); + cols.forEach(function (key) { + buf.push('', key, ''); + }); - cols.forEach(function (key) { - buf.push('', key, ''); - }); + buf.push('', '', ''); - buf.push('', '', ''); + obj.forEach(function (record) { + buf.push(''); + buf.push(tableify(record, cols, parents)); + buf.push(''); + }); - obj.forEach(function (record) { - buf.push(''); - buf.push(tableify(record, cols, parents)); - buf.push(''); - }); + buf.push('
'); + } + else { + buf.push('',''); + cols = []; - buf.push('
'); - } - else { - buf.push('',''); - cols = []; + obj.forEach(function (val, ix) { + cols.push(ix); + buf.push('', '', tableify(val, cols, parents), '', ''); + }); - obj.forEach(function (val, ix) { - cols.push(ix); - buf.push('', '', tableify(val, cols, parents), '', ''); - }); + buf.push('','
'); + } - buf.push('',''); } + else if (obj && typeof obj === 'object' && !Array.isArray(obj) && !(obj instanceof Date)) { + if (!columns) { + buf.push(''); - } - else if (obj && typeof obj === 'object' && !Array.isArray(obj) && !(obj instanceof Date)) { - if (!columns) { - buf.push('
'); + Object.keys(obj).forEach(function (key) { + buf.push('', '', key, '', '', tableify(obj[key], false, parents), '', ''); + }); - Object.keys(obj).forEach(function (key) { - buf.push('', '', key, '', '', tableify(obj[key], false, parents), '', ''); - }); + buf.push('
'); + } + else { + columns.forEach(function (key) { + if (typeof obj[key] === 'object' && !Array.isArray(obj[key])) { + buf.push('', tableify(obj[key], false, parents), ''); + } + else { + buf.push('', tableify(obj[key], columns, parents), ''); + } + }); + } + } + else { + buf.push(obj); + } - buf.push(''); + if (type !== 'object' || obj == null || obj == undefined) { } else { - columns.forEach(function (key) { - if (typeof obj[key] === 'object' && !Array.isArray(obj[key])) { - buf.push('', tableify(obj[key], false, parents), ''); - } - else { - buf.push('', tableify(obj[key], columns, parents), ''); - } - }); + parents.pop(obj); } - } - else { - buf.push(obj); - } - if (type !== 'object' || obj == null || obj == undefined) { + return buf.join(''); } - else { - parents.pop(obj); + + function getClass(obj) { + if (!classes) { + return ''; + } + + return ' class="' + + classPrefix + + ((obj && obj.constructor && obj.constructor.name) + ? obj.constructor.name + : typeof obj || '' + ).toLowerCase() + + ((obj === null) + ? ' null' + : '' + ) + + '"' + ; } - return buf.join(''); -} - -function getClass(obj) { - return ' class="' - + ((obj && obj.constructor && obj.constructor.name) - ? obj.constructor.name - : typeof obj || '' - ).toLowerCase() - + ((obj === null) - ? ' null' - : '' - ) - + '"' - ; -} +} \ No newline at end of file diff --git a/test.js b/test.js index 55b15ef..e2993d7 100644 --- a/test.js +++ b/test.js @@ -50,3 +50,10 @@ obj.fish[0].b = obj.fish; assert.equal(tableify(obj) + '\n', html); +t = tableify.defaults({ classes : false }) + +console.log(t(obj)) + +t = tableify.defaults({ classPrefix : 'tblfy-' }) + +console.log(t(obj)) \ No newline at end of file