Skip to content

Instantly share code, notes, and snippets.

@olivierodo
Last active December 4, 2023 15:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save olivierodo/3c462ec26567aa2b66f5b56408764997 to your computer and use it in GitHub Desktop.
Save olivierodo/3c462ec26567aa2b66f5b56408764997 to your computer and use it in GitHub Desktop.
Krungthai - Bank statement parser
/*
* 1. Create a file names 'input.txt' where you past all the statement from the Krungthai PDF
*
* EXAMPLE:
* 17/11/23 Fee (PCSFE) N-time Fee W/D. 10.00 83,096.75 23
* 10:49
* 21/11/23 Payment EDC (LSSWP) HEALTH LAND 2004 CO. LTD. 3,900.00 79,196.75 23
* 09:55
* 22/11/23 Fee - SMS Alert (SWALFE) Alert Fee,Term branch = 23 20.00 79,176.75 23
* 22:36
* 23/11/23 Withdrawal-ATM (PCSW) S594B101A418 5,000.00 74,176.75 23
*
* ------------
*
* 2. Run the process "npm run dev"
* 3. The file "output.csv" should be created with the file information inside
*/
const fs = require('fs')
const path = require('path')
const { stringify } = require('csv/sync')
const INPUT_FILE = 'input.txt'
const OUTPUT_FILE = 'output.csv'
const RGX = /(\d{2}\/\d{2}\/\d{2}) (.*\)) (.*) ((\d{1,3}(,\d{3})*(\.\d+))) ((\d{1,3}(,\d{3})*(\.\d+))) \d{2,3}\n(\d{2}:\d{2})/g
const content = fs.readFileSync(path.resolve(INPUT_FILE)).toString()
const matches = content.matchAll(RGX)
const result = [...matches]
.map(item => {
const amount = parseFloat(item[4].replace(/,/g, ''))
const type = item[2]
const isDeposit = /Transfer in/.test(type) ? 1 : -1
return {
date: getDate(item[1], item[12]),
type,
name: item[3],
amount: amount * isDeposit,
balance: item[8]
}
})
.sort((a,b) => {
return a.date - b.date
})
.map(item => {
item.date = item.date.toString()
return item
})
const CSV = stringify(result, {header: true })
fs.writeFileSync(path.resolve(OUTPUT_FILE), CSV)
function getDate(date, time) {
const [day,month,year] = date.split('/')
return new Date(['20' + year, month, day].join('-') + 'T' + time)
}
17/11/23 Fee (PCSFE) N-time Fee W/D. 10.00 83,096.75 23
10:49
21/11/23 Payment EDC (LSSWP) HEALTH LAND 2004 CO. LTD. 3,900.00 79,196.75 23
09:55
22/11/23 Fee - SMS Alert (SWALFE) Alert Fee,Term branch = 23 20.00 79,176.75 23
22:36
23/11/23 Withdrawal-ATM (PCSW) S594B101A418 5,000.00 74,176.75 23
16:14
{
"name": "statement",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"dev": "node --watch index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"csv": "^6.3.5"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment