Created
August 6, 2017 20:22
-
-
Save Woodsphreaker/5bbb78f59a1ba669dadd816ec36c7ad3 to your computer and use it in GitHub Desktop.
Make Table
This file contains hidden or 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
const makeTableAPI = (() => { | |
const makeRows = (data) => { | |
const rows = data.reduce((acc, row) => { | |
acc += ` <tr>\n${makeCols(row)} </tr>\n` | |
return acc | |
}, '') | |
return rows | |
} | |
const makeCols = (data) => { | |
const cols = Object.keys(data) | |
.reduce((acc, col) => { | |
acc += ` <td>${data[col]}</td>\n` | |
return acc | |
}, '') | |
return cols | |
} | |
const makeTable = (template = 'tableDefault', data = {}) => { | |
return getTemplate(template, data) | |
} | |
const makeHeader = (data) => { | |
return makeRows(data.headers) | |
} | |
const makeBody = (data) => { | |
return makeRows(data.values) | |
} | |
const getTemplate = (type, data) => { | |
const tableDefault = () => `<table class="table table-striped">\n <thead>\n ${makeHeader(data)}</thead>\n<body>\n${makeBody(data)}</body>\n</table>` | |
return { | |
tableDefault | |
}[type]() | |
} | |
return { | |
makeTable | |
} | |
})() | |
const data = { | |
"headers": [{ | |
"0": "Nome", | |
"1": "Pergunta 1", | |
"2": "Pergunta 2", | |
"3": "Pergunta 3", | |
"4": "Pergunta 4", | |
"5": "Pergunta 5", | |
"6": "CPF" | |
}], | |
"values": | |
[...Array(100).keys()].reduce(acc => { | |
acc.push({ | |
"0": "Nome do Candidato", | |
"1": "Resposta 1", | |
"2": "Resposta 2", | |
"3": "Resposta 3", | |
"4": "Resposta 4", | |
"5": "Resposta 5", | |
"6": "000.000.000-00" | |
}) | |
return acc | |
}, []) | |
} | |
console.log( | |
makeTableAPI.makeTable('tableDefault', data) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment