Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save arrowrowe/a480b799400b44a9d745b39651654bb8 to your computer and use it in GitHub Desktop.
Save arrowrowe/a480b799400b44a9d745b39651654bb8 to your computer and use it in GitHub Desktop.
Count Non-English Characters in Files Matching a Given Pattern

Count Non-English Characters

In Files Matching a Given Pattern.

Install

Install Node >= v6 and replem.

Usage

Start a replem session using the command below:

$ replem glob fs-extra:fs bluebird:Promise

then paste the following lines in and press enter:

const plus = (a, b) => a + b;
const sum = s => s.reduce(plus, 0);
const countText = t => (t.match(/[^\x00-\xff]/g) || []).length;
const limitTo = (s, l, t) => s.length <= l ? s : s.substr(0, l - t - 3) + '...' + s.substr(s.length - t);
const str = o => limitTo(JSON.stringify(o), 80, 50);

const read = Promise.promisify(fs.readFile);
const glop = Promise.promisify(glob);
const logPromise = fn => arg => {
  var sArg = str(arg);
  console.log('start', sArg);
  return fn(arg).tap(ret => console.log('  got', sArg, '=', ret));
};

const [countInFile, countInFiles, countByPattern] = [
  file => read(file, 'utf-8').then(countText),
  files => Promise.all(files.map(countInFile)).then(sum),
  pattern => glop(pattern).then(countInFiles)
].map(logPromise);

Then run this, with your own pattern:

countByPattern('/code/private/draft/translation/**/*.md')
@at15
Copy link

at15 commented May 4, 2016

\w/

@at15
Copy link

at15 commented May 4, 2016

got it up and running, thx mie

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment