-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This function accepts a config object with 'classes' and 'classPrefix' options.
- Loading branch information
1 parent
be71095
commit abc6daf
Showing
2 changed files
with
123 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
: '' | ||
) | ||
+ '"' | ||
; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters