Skip to content

Commit

Permalink
Support LOC in minSize measurement too
Browse files Browse the repository at this point in the history
  • Loading branch information
Nixinova committed Feb 8, 2025
1 parent 6798cfe commit 4ee0b45
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
3 changes: 3 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## Next
- Added `minSize` CLI option to control a minimum size to return, with results smaller than this threshold put in 'Other' ([#35](https://github.com/Nixinova/LinguistJS/pull/35)).

## 2.8.1
*2024-12-05*
- Fixed gitignore paths being applied as root-relative ([#36](https://github.com/Nixinova/LinguistJS/issues/36)).
Expand Down
11 changes: 8 additions & 3 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ program
.option('-j|--json [bool]', 'Display the output as JSON', false)
.option('-t|--tree <traversal>', 'Which part of the output JSON to display (dot-delimited)')
.option('-F|--listFiles [bool]', 'Whether to list every matching file under the language results', false)
.option('-m|--minSize <size>', 'Minimum file size to show language results for (must have a unit: b, kb, mb, %)')
.option('-m|--minSize <size>', 'Minimum size of file to show language results for (must have a unit: b, kb, mb, %, or loc)')
.option('-q|--quick [bool]', 'Skip complex language analysis (alias for -{A|I|H|S}=false)', false)
.option('-o|--offline [bool]', 'Use packaged data files instead of fetching latest from GitHub', false)
.option('-L|--calculateLines [bool]', 'Calculate lines of code totals', true)
Expand Down Expand Up @@ -67,17 +67,20 @@ if (args.analyze) (async () => {
const totalSize = languages.bytes;
const minSizeAmt = parseFloat(args.minSize.replace(/[a-z]+$/i, '')); // '2KB' -> 2
const minSizeUnit = args.minSize.replace(/^\d+/, '').toLowerCase(); // '2KB' -> 'kb'
const checkBytes = minSizeUnit !== 'loc'; // whether to check bytes or loc
const conversionFactors: Record<string, (n: number) => number> = {
'b': n => n,
'kb': n => n * 1e3,
'mb': n => n * 1e6,
'%': n => n * totalSize / 100,
'loc': n => n,
};
const minBytesSize = conversionFactors[minSizeUnit](+minSizeAmt);
const other = { bytes: 0, lines: { total: 0, content: 0, code: 0 } };
// Apply specified minimums: delete language results that do not reach the threshold
for (const [lang, data] of Object.entries(languages.results)) {
if (data.bytes < minBytesSize) {
const checkUnit = checkBytes ? data.bytes : data.lines.code;
if (checkUnit < minBytesSize) {
// Add to 'other' count
other.bytes += data.bytes;
other.lines.total += data.lines.total;
Expand All @@ -87,7 +90,9 @@ if (args.analyze) (async () => {
delete languages.results[lang];
}
}
languages.results['Other'] = { ...other, type: null! };
if (other.bytes) {
languages.results["Other"] = { ...other, type: null! };
}
}

const sortedEntries = Object.entries(languages.results).sort((a, b) => (a[1].bytes < b[1].bytes ? +1 : -1));
Expand Down

0 comments on commit 4ee0b45

Please sign in to comment.