Skip to content

Instantly share code, notes, and snippets.

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