-
Notifications
You must be signed in to change notification settings - Fork 22
/
get-size-table.js
44 lines (37 loc) · 1.01 KB
/
get-size-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
const Table = require("table-builder");
const SIZE_TABLE_HEADER = "라벨 사이즈";
const parseHeaders = (data) => {
const firstRow = Object.values(data)[0];
return Object.keys(firstRow).reduce(
(acc, key, index) => ({ ...acc, [index + 1]: key }),
{ 0: SIZE_TABLE_HEADER },
);
};
const parseData = (headers, data) =>
Object.entries(data).map(([size, measurements]) =>
Object.entries(headers).reduce(
(acc, [headerKey, headerValue]) => ({
...acc,
[headerKey]:
headerValue === SIZE_TABLE_HEADER ? size : measurements[headerValue],
}),
{},
),
);
const getSizeTable = (rawData) => {
if (rawData === undefined) {
return "";
}
const headers = parseHeaders(rawData);
const data = parseData(headers, rawData);
try {
return new Table({ class: "size-table" })
.setHeaders(headers)
.setData(data)
.render();
} catch (e) {
console.error("Fail to create Table");
return undefined;
}
};
module.exports = { getSizeTable };