Created
September 7, 2017 20:11
-
-
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
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 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