Skip to content

Instantly share code, notes, and snippets.

@JodiWarren
Created September 7, 2017 20:11
Show Gist options
  • Select an option

  • Save JodiWarren/99e4dd2b6a8bdcb4f5c3871465108b97 to your computer and use it in GitHub Desktop.

Select an option

Save JodiWarren/99e4dd2b6a8bdcb4f5c3871465108b97 to your computer and use it in GitHub Desktop.
Output CSV based on the HTML of a John Lewis Gift List page
const guests = [].slice.call(document.querySelectorAll('tr.guest'));
const itemText = element => element.querySelector('td[align=left]').textContent.replace(/\(\d*\)/g, '').trim()
const guestData = guests.map(guestElement => {
const name = itemText(guestElement);
const message = itemText(guestElement.nextElementSibling)
let giftEl = guestElement.nextElementSibling.nextElementSibling;
let gifts = [];
while ( giftEl.classList.contains('item') ) {
gifts = gifts.concat(itemText(giftEl));
giftEl = giftEl.nextElementSibling;
}
return {
name,
message,
gifts,
}
})
const guestCSV = guestData.map(guestItem => {
const guestLine = [`"${guestItem.gifts.shift()}","${guestItem.name}","${guestItem.message}"`];
const otherGiftLines = guestItem.gifts.map(gift => `"${gift}","",""`);
return guestLine.concat(otherGiftLines).join("\n")
}).join("\n")
console.log(guestCSV);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment