Last active
January 17, 2016 09:36
-
-
Save vsemozhetbyt/1f965ccd0b84f84e58b3 to your computer and use it in GitHub Desktop.
This file contains 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
'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