'use strict'; | |
/******************************************************************************/ | |
const path = process.argv[2]; | |
if (!path) { | |
console.log('Add the file path, please.'); | |
process.exit(); | |
} | |
/******************************************************************************/ | |
const fs = require('fs'); | |
const rl = require('readline').createInterface({ | |
input: fs.createReadStream(path, {encoding: 'utf8'}), //ascii utf8 utf16le | |
terminal: false, | |
historySize: 0 | |
}); | |
const formatNumberRE = /\B(?=(?:\d{3})+$)/g; | |
const headwordsRE = /^\S/; | |
const cardsRE = /\[b\]1\./; | |
const interpretationsRE = /\[\/b\]/; | |
let headwords = 0; | |
let cards = 0; | |
let interpretations = 0; | |
let lines = 0; | |
/******************************************************************************/ | |
rl.on('line', function(line) { | |
lines++; | |
if (lines === 1) line = line.replace(/^\uFEFF/, ''); | |
if (!line.startsWith('#')) { | |
if (headwordsRE.test(line)) { | |
headwords++; | |
} else { | |
if (cardsRE.test(line)) cards++; | |
if (interpretationsRE.test(line)) interpretations++; | |
} | |
} | |
}).on('close', () => { | |
console.log( | |
//////////////////////////////////////////////////////////////////////////////// | |
` | |
Headwords: ${ headwords.toString().replace(formatNumberRE, ' ') } | |
Cards: ${ cards.toString().replace(formatNumberRE, ' ') } | |
Interpretations: ${ interpretations.toString().replace(formatNumberRE, ' ') } | |
Lines: ${ lines.toString().replace(formatNumberRE, ' ') } | |
` | |
//////////////////////////////////////////////////////////////////////////////// | |
); | |
}); | |
/******************************************************************************/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment