Skip to content

Instantly share code, notes, and snippets.

@bjouhier
Created January 3, 2018 17:51
Show Gist options
  • Save bjouhier/0fa9b067270bbac97fb63c0affd0653c to your computer and use it in GitHub Desktop.
Save bjouhier/0fa9b067270bbac97fb63c0affd0653c to your computer and use it in GitHub Desktop.
const fs = require('fs');
const fsp = require('path');
function scan(dir, results, mod) {
fs.readdirSync(dir).forEach(name => {
const path = fsp.join(dir, name);
let m;
if (fs.statSync(path).isDirectory()) {
scan(path, results, name === 'node_modules' ? null : mod === null ? name : mod);
} else if (mod && (m = /(.*)\.[jt]s$/.exec(name)) && !/\.(d|min|debug)$/.test(m[1])) {
const counters = results[mod] || (results[mod] = {});
if (/^_?[a-z0-9]+$/.test(m[1])) {
counters.neutral = (counters.neutral || 0) + 1;
} else if (/^[A-Z0-9_]+$/.test(m[1])) {
counters.upper = (counters.upper || 0) + 1;
} else if (/^_?[a-z][A-Za-z0-9]+$/.test(m[1])) {
counters.camel = (counters.camel || 0) + 1;
} else if (/^_?[A-Z][A-Za-z0-9]+$/.test(m[1])) {
counters.pascal = (counters.pascal || 0) + 1;
} else if (/^[a-z0-9-]+$/.test(m[1])) {
counters.kebab = (counters.kebab || 0) + 1;
} else if (/^[a-z0-9_]+$/.test(m[1])) {
counters.snake = (counters.snake || 0) + 1;
} else {
counters.other = (counters.other || 0) + 1;
}
}
});
return results;
}
const root = process.argv[2];
function main() {
if (!root) throw new Error('no root');
const results = scan(root, {});
const totals = {
upper: [],
camel: [],
pascal: [],
kebab: [],
snake: [],
other: [],
neutral: [],
multiple: [],
}
Object.keys(results).sort().forEach(k => {
const r = results[k];
const flags = !!r.upper + !!r.camel + !!r.pascal + !!r.kebab + !!r.snake + !!r.other;
//console.log(flags);
if (flags === 0) {
totals.neutral.push(k);
} else if (flags === 1) {
if (r.upper) totals.upper.push(k);
else if (r.camel) totals.camel.push(k);
else if (r.pascal) totals.pascal.push(k);
else if (r.kebab) totals.kebab.push(k);
else if (r.snake) totals.snake.push(k);
else if (r.other) totals.other.push(k);
else throw new Error();
} else {
totals.multiple.push(k);
}
});
console.log(totals);
console.log(Object.keys(totals).map(k => k + ': ' + totals[k].length));
}
main();
@bjouhier
Copy link
Author

bjouhier commented Jan 3, 2018

Small utility to get basic stats of filename syntax. Usage:

$ node file-syntax rootdir

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