-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtable.js
107 lines (100 loc) · 2.82 KB
/
table.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/**
* Table class.
* @class
*/
class Table {
constructor() {}
/**
* The builder of table.
* @param {Object} type
* @param {Array} data
*/
build(type, data) {
if (!type || data.length < 0) {
throw Error('Provide invalid params');
}
switch (Object.keys(type)[0]) {
case 'caption':
this.table = captionTable(type, data);
break;
case 'tbody':
this.table = tBodyTable(type, data);
break;
case 'table':
this.table = tabularTable(type, data);
break;
}
return this;
}
}
/**
* Defines the title of a <table>.
*
* @param {Object} type.caption The tittle of a table
* @param {Array} data The data of a table
* @param {String} data.key The data key of a table
* @param {String} data.value The data value of a table
*/
function captionTable(type, data) {
var htmlStart = `<table><caption>${type.caption}</caption>`;
var htmlCenter = '';
var htmlEnds = '</table>';
data.forEach((value) => {
htmlCenter += insert(
htmlCenter,
htmlCenter.length,
`<tr><td>${value.key}</td><td>${value.value}</td></tr>`
);
});
return htmlStart + htmlCenter + htmlEnds;
}
/**
* Defines a group of table rows <tr>.
*
* @param {Object} type.tbody
* @param {String} type.tbody.key The titles key of a table
* @param {String} type.tbody.value The titles value of a table
* @param {Array} data The data of a table
* @param {String} data.key The data key of a table
* @param {String} data.value The data value of a table
*/
function tBodyTable(type, data) {
var htmlStart = `<table><thead><tr><th>${type.tbody.key}</th><th>${type.tbody.value}</th></tr></thead>`;
var htmlCenter = '';
var htmlEnds = '</table>';
data.forEach((value) => {
htmlCenter += insert(
htmlCenter,
htmlCenter.length,
`<tbody><tr><td>${value.key}</td><td>${value.value}</td></tr></tbody>`
);
});
return htmlStart + htmlCenter + htmlEnds;
}
/**
* Defines a container for tabular data.
*
* @param {Object} type.table
* @param {String} type.table.key The titles key of a table
* @param {String} type.table.value The titles value of a table
* @param {Array} data The data of a table
* @param {String} data.key The data key of a table
* @param {String} data.value The data value of a table
*/
function tabularTable(type, data) {
var htmlStart = `<table><thead><tr><th>${type.table.key}</th><th>${type.table.value}</th></tr></thead><tbody>`;
var htmlCenter = '';
var htmlEnds = '</tbody></table>';
data.forEach((value) => {
htmlCenter += insert(
htmlCenter,
htmlCenter.length,
`<tr><td>${value.key}</td><td>${value.value}</td></tr>`
);
});
return htmlStart + htmlCenter + htmlEnds;
}
function insert(str, index, value) {
return str.substr(0, index) + value + str.substr(index);
}
module.exports = Table;