Skip to content

Instantly share code, notes, and snippets.

@lucis
Last active August 13, 2019 20:42
Show Gist options
  • Save lucis/c82067b038064aeccc6abfc9acd91b23 to your computer and use it in GitHub Desktop.
Save lucis/c82067b038064aeccc6abfc9acd91b23 to your computer and use it in GitHub Desktop.
// 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