-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
69 lines (62 loc) · 1.65 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
'use strict';
var utils = require('./utils');
/**
* Create an html table from a JSON object.
*
* ```js
* var table = {
* // attributes to add to the <table> tag
* attr: 'class="table"',
*
* // thead rows (each row has an array of columns)
* thead: [['Foo', 'Bar', 'Baz']],
*
* // tbody rows (each row has an array of columns)
* tbody: [
* ['foo', 'bar', 'baz'],
* ['FOO', 'BAR', 'BAZ']
* ]
* };
*
* var html = htmlTable(table);
* console.log(html);
* //=> <table class="table">
* //=> <thead>
* //=> <tr>
* //=> <td>Foo</td>
* //=> <td>Bar</td>
* //=> <td>Baz</td>
* //=> </tr>
* //=> </thead>
* //=> <tbody>
* //=> <tr>
* //=> <td>foo</td>
* //=> <td>bar</td>
* //=> <td>baz</td>
* //=> </tr>
* //=> <tr>
* //=> <td>FOO</td>
* //=> <td>BAR</td>
* //=> <td>BAZ</td>
* //=> </tr>
* //=> </tbody>
* //=> </table>
* ```
* @param {Object} `table` object containing properties describing the rows and columns in a table head and body. See [formats](#formats) for additional options.
* @return {String} HTML table generated from configuration object.
* @api public
*/
module.exports = function(table) {
if (typeof table === 'undefined') {
throw new Error('expected first argument to be an object');
}
table = utils.normalizeTable(table);
var html = utils.htmlElement('table', table.attr) + '\n';
utils.sections.forEach(function(section) {
if (table.hasOwnProperty(section)) {
html += utils.htmlSection(table, section, section === 'thead' ? 'th' : 'td');
}
});
html += '</table>\n';
return html;
};