From: Mauro Scomparin Date: Thu, 7 Jan 2021 20:06:53 +0000 (+0100) Subject: fix for bad format in old data X-Git-Url: https://git.scompo.it/?a=commitdiff_plain;ds=sidebyside;p=money.git fix for bad format in old data --- diff --git a/utils/fix-bad-dates.js b/utils/fix-bad-dates.js new file mode 100644 index 0000000..26705c3 --- /dev/null +++ b/utils/fix-bad-dates.js @@ -0,0 +1,42 @@ +const fs = require('fs') + +function badDate(line) { + // -20.00;2016/8/27;;old stadio + const tokens = line.split(';') + const date = tokens[1] + const dateTokens = date.split('/') + return dateTokens[2].length !== 4 +} + +function fixDate(line) { + // -20.00;2016/8/27;;old stadio + const tokens = line.split(';') + const date = tokens[1] + const dateTokens = date.split('/') + const dateOk = dateTokens[2].padStart(2, '0') + '/' + dateTokens[1].padStart(2, '0') + '/' + dateTokens[0].padStart(4, '0') + const output = tokens[0] + ';' + dateOk + ';' + tokens[2] + ';' + tokens[3] + return output +} + +async function main() { + const inputFile = process.argv.slice(2)[0] + fs.readFile(inputFile, 'utf-8', (err, data) => { + if(err){ + console.error(err) + return err + } + const lines = data.split(/\r?\n/) + for(line of lines) { + if(line) { + const dateIsBad = badDate(line) + if(badDate(line)) { + console.log(fixDate(line)) + }else{ + console.log(line) + } + } + } + }) +} + +main()