Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# don't check in installed node_modules
node_modules/
.idea
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,29 @@ var html = tableify({
console.log(html);
```

reducing json depth
-----

You can additionally reduce JSON depth on some level by using additional tableify options:

```javascript
var html = tableify(json, {reduceAt: 3, reducer: () => {
// write some reducer function
}});
```

In the example above all elements on 3rd level of depth will be reduced to the content returned from `reducer` function.

You can also use default `reducer` function:

```javascript
var html = tableify(json, {reduceAt: 3});
```

This function returns:
1. For arrays - message with array length.
1. For object - if object has `value` property or has only one property, returns this value, otherwise returns message with amount of object properties.

command line usage
------------------

Expand Down
58 changes: 43 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,17 @@ function init(config) {
var classes = config.classes === false ? false : true;
var classPrefix = config.classPrefix || "";

return function tableify(obj, columns, parents) {
/**
* @param {object} options `reduceAt: number` reduces table at some depth
* `reducer: function(obj)` function used to reduce object
*/
return function tableify(obj, options = {}, columns, parents) {
let {reduceAt = null, reducer = defaultReduce} = options;

// this element should be reduced
if (parents && parents.length === reduceAt)
obj = reducer(obj);

var buf = [];
var type = typeof obj;
var cols;
Expand All @@ -30,20 +40,20 @@ function init(config) {
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);

buf.push('<tr>');

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

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

buf.push('</tbody>','</table>');
}
else if (typeof obj[0] === 'object') {
Expand All @@ -69,7 +79,7 @@ function init(config) {

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

Expand All @@ -81,7 +91,7 @@ function init(config) {

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

buf.push('</tbody>','</table>');
Expand All @@ -93,18 +103,18 @@ function init(config) {
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>');
buf.push('<tr>', '<th' + getClass(obj[key]) + '>', key, '</th>', '<td' + getClass(obj[key]) + '>', tableify(obj[key], options, 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>');
buf.push('<td' + getClass(obj[key]) + '>', tableify(obj[key], options, false, parents), '</td>');
}
else {
buf.push('<td' + getClass(obj[key]) + '>', tableify(obj[key], columns, parents), '</td>');
buf.push('<td' + getClass(obj[key]) + '>', tableify(obj[key], options, columns, parents), '</td>');
}
});
}
Expand All @@ -130,15 +140,33 @@ function init(config) {
return ' class="'
+ classPrefix
+ ((obj && obj.constructor && obj.constructor.name)
? obj.constructor.name
: typeof obj || ''
? obj.constructor.name
: typeof obj || ''
).toLowerCase()
+ ((obj === null)
? ' null'
: ''
? ' null'
: ''
)
+ '"'
;
}

// default reduce: array to array length message, object to object count of properties message, object having `value` property to this value
function defaultReduce(obj) {
if (Array.isArray(obj)) {
if (obj.length === 1)
return defaultReduce(obj[0]);
return `(${obj.length} elements)`;
} else if (typeof obj === "object") {
if (obj.hasOwnProperty("value"))
return defaultReduce(obj.value);
let keys = Object.keys(obj);
if (keys.length === 1)
return defaultReduce(obj[keys[0]]);
return `(${keys.length} properties)`;
}

return obj;
}

}
6 changes: 6 additions & 0 deletions test-data.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion test.html

This file was deleted.

19 changes: 15 additions & 4 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
var tableify = require('./');
var assert = require('assert');
var fs = require('fs');
const testData = require('./test-data');

var html = fs.readFileSync('test.html','utf8');
var obj = {
test : {
data : [
Expand Down Expand Up @@ -48,12 +47,24 @@ obj.fish[0].b = obj.fish;
//console.log(tableify(obj));
//console.log(html);

assert.equal(tableify(obj) + '\n', html);
assert.equal(tableify(obj) + '\n', testData.default);

t = tableify.defaults({ classes : false })

console.log(t(obj))

t = tableify.defaults({ classPrefix : 'tblfy-' })

console.log(t(obj))
console.log(t(obj))

// reducer tests
console.log('reducer tests');
t = tableify(obj, {reduceAt: 3});
assert.equal(t + '\n', testData.reduce3);
console.log(t);
t = tableify(obj, {reduceAt: 2});
assert.equal(t + '\n', testData.reduce2);
console.log(t);
t = tableify(obj, {reduceAt: 1});
assert.equal(t + '\n', testData.reduce1);
console.log(t);