Skip to content

Commit

Permalink
Add defaults() function (issue #3)
Browse files Browse the repository at this point in the history
This function accepts a config object with 'classes' and 'classPrefix' options.
  • Loading branch information
wankdanker committed Sep 15, 2018
1 parent be71095 commit abc6daf
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 101 deletions.
217 changes: 116 additions & 101 deletions index.js
Original file line number Diff line number Diff line change
@@ -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('<table>','<tbody>');
cols = [];

// 2D array is an array of rows
obj.forEach(function (row, ix) {
cols.push(ix);
parents = parents || [];

buf.push('<tr>');
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('<table>','<tbody>');
cols = [];

row.forEach(function (val) {
buf.push('<td' + getClass(val) + '>', tableify(val, cols, parents), '</td>')
// 2D array is an array of rows
obj.forEach(function (row, ix) {
cols.push(ix);

buf.push('<tr>');

row.forEach(function (val) {
buf.push('<td' + getClass(val) + '>', tableify(val, cols, parents), '</td>')
});

buf.push('</tr>');
});

buf.push('</tr>');
});

buf.push('</tbody>','</table>');
}
else if (typeof obj[0] === 'object') {
buf.push('<table>','<thead>','<tr>');

//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('</tbody>','</table>');
}
else if (typeof obj[0] === 'object') {
buf.push('<table>','<thead>','<tr>');

//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('<th' + getClass(obj[0][key]) + '>', key, '</th>');
});

cols.forEach(function (key) {
buf.push('<th' + getClass(obj[0][key]) + '>', key, '</th>');
});
buf.push('</tr>', '</thead>', '<tbody>');

buf.push('</tr>', '</thead>', '<tbody>');
obj.forEach(function (record) {
buf.push('<tr>');
buf.push(tableify(record, cols, parents));
buf.push('</tr>');
});

obj.forEach(function (record) {
buf.push('<tr>');
buf.push(tableify(record, cols, parents));
buf.push('</tr>');
});
buf.push('</tbody></table>');
}
else {
buf.push('<table>','<tbody>');
cols = [];

buf.push('</tbody></table>');
}
else {
buf.push('<table>','<tbody>');
cols = [];
obj.forEach(function (val, ix) {
cols.push(ix);
buf.push('<tr>', '<td' + getClass(val) + '>', tableify(val, cols, parents), '</td>', '</tr>');
});

obj.forEach(function (val, ix) {
cols.push(ix);
buf.push('<tr>', '<td' + getClass(val) + '>', tableify(val, cols, parents), '</td>', '</tr>');
});
buf.push('</tbody>','</table>');
}

buf.push('</tbody>','</table>');
}
else if (obj && typeof obj === 'object' && !Array.isArray(obj) && !(obj instanceof Date)) {
if (!columns) {
buf.push('<table>');

}
else if (obj && typeof obj === 'object' && !Array.isArray(obj) && !(obj instanceof Date)) {
if (!columns) {
buf.push('<table>');
Object.keys(obj).forEach(function (key) {
buf.push('<tr>', '<th' + getClass(obj[key]) + '>', key, '</th>', '<td' + getClass(obj[key]) + '>', tableify(obj[key], false, parents), '</td>', '</tr>');
});

Object.keys(obj).forEach(function (key) {
buf.push('<tr>', '<th' + getClass(obj[key]) + '>', key, '</th>', '<td' + getClass(obj[key]) + '>', tableify(obj[key], false, parents), '</td>', '</tr>');
});
buf.push('</table>');
}
else {
columns.forEach(function (key) {
if (typeof obj[key] === 'object' && !Array.isArray(obj[key])) {
buf.push('<td' + getClass(obj[key]) + '>', tableify(obj[key], false, parents), '</td>');
}
else {
buf.push('<td' + getClass(obj[key]) + '>', tableify(obj[key], columns, parents), '</td>');
}
});
}
}
else {
buf.push(obj);
}

buf.push('</table>');
if (type !== 'object' || obj == null || obj == undefined) {
}
else {
columns.forEach(function (key) {
if (typeof obj[key] === 'object' && !Array.isArray(obj[key])) {
buf.push('<td' + getClass(obj[key]) + '>', tableify(obj[key], false, parents), '</td>');
}
else {
buf.push('<td' + getClass(obj[key]) + '>', tableify(obj[key], columns, parents), '</td>');
}
});
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'
: ''
)
+ '"'
;
}
}
7 changes: 7 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))

0 comments on commit abc6daf

Please sign in to comment.