Last active
August 13, 2019 20:42
-
-
Save lucis/c82067b038064aeccc6abfc9acd91b23 to your computer and use it in GitHub Desktop.
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
// WORK IN PROGRESS | |
{ | |
const testTable = document.querySelector('table'); | |
const table2json = (table,config)=>{ | |
const ths = table.querySelectorAll('th') | |
if (!ths || !ths.length && !config.headers){ | |
throw new Error('Please provide header values if not in the table') | |
} | |
const preheaders = ths ? [...table.querySelectorAll('th')].map(th => th.innerText) : config.headers | |
const headers = config.firstHeader ? [config.firstHeader, ...preheaders.splice(1)] : preheaders | |
const trs = table.querySelectorAll('tr') | |
const entries = [...trs].splice(1) | |
return entries.reduce((acc, current) => { | |
if (!current.children) return acc | |
return [...acc, [...current.children].reduce((acc, current, index) => ({...acc, [headers[index]]: current.innerText}), {})] | |
}, []) | |
} | |
// TODO | |
const config = { | |
headers: undefined, | |
firstHeader: 'slot', | |
mapHeader: a => a, | |
filterLine: a => true | |
} | |
table2json(testTable, config) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment