Created
March 21, 2024 22:12
-
-
Save bsidhom/ddad2ab1225517b1eabf5beeb1bc8a9c to your computer and use it in GitHub Desktop.
Convert Vanguard realized lots to the legs of a Beancount transaction. Mostly useful for establishing opening balances given a large set of lots.
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
let main = () => { | |
const datePattern = new RegExp(`(\\d{2})/(\\d{2})/(\\d{4})`); | |
const extractDate = (s) => { | |
const m = s.match(datePattern); | |
const month = m[1]; | |
const day = m[2]; | |
const year = m[3]; | |
return `${year}-${month}-${day}`; | |
}; | |
const extractLot = (row) => { | |
const tds = row.querySelectorAll("td"); | |
const purchaseDate = extractDate(tds[1].innerText.trim()); | |
const quantity = tds[4].innerText.trim(); | |
const cost = tds[5].innerText.trim().substr(1); | |
return { purchaseDate, quantity, cost }; | |
}; | |
const renderLog = | |
(account, units) => | |
({ purchaseDate, quantity, cost }) => { | |
return `${account} ${quantity} ${units} {{ ${cost} USD, ${purchaseDate} }}`; | |
}; | |
const table = document.querySelector("#lots_table tbody"); | |
const rows = [...table.querySelectorAll("tr")]; | |
const lots = rows.map(extractLot); | |
const resultString = lots | |
.map(renderLog("Assets:Vanguard:Taxable:VWSUX", "VWSUX")) | |
.join("\n"); | |
// NOTE: We can't open a top-level frame at a data URL anymore, so the | |
// following does not work: | |
// const url = `data:text/plain;charset=UTF-8,${encodeURIComponent(resultString)}`; | |
// window.open(url, "_blank"); | |
// Instead, we create an object URL and point the browser at it. For some | |
// reason, this _is_ allowed by the browser policy. | |
const blob = new Blob([resultString], { type: "text/plain;charset=UTF-8" }); | |
const url = URL.createObjectURL(blob); | |
window.open(url); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment