Skip to content

Commit 4ee0b45

Browse files
committed
Support LOC in minSize measurement too
1 parent 6798cfe commit 4ee0b45

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

changelog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Changelog
22

3+
## Next
4+
- 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)).
5+
36
## 2.8.1
47
*2024-12-05*
58
- Fixed gitignore paths being applied as root-relative ([#36](https://github.com/Nixinova/LinguistJS/issues/36)).

src/cli.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ program
2222
.option('-j|--json [bool]', 'Display the output as JSON', false)
2323
.option('-t|--tree <traversal>', 'Which part of the output JSON to display (dot-delimited)')
2424
.option('-F|--listFiles [bool]', 'Whether to list every matching file under the language results', false)
25-
.option('-m|--minSize <size>', 'Minimum file size to show language results for (must have a unit: b, kb, mb, %)')
25+
.option('-m|--minSize <size>', 'Minimum size of file to show language results for (must have a unit: b, kb, mb, %, or loc)')
2626
.option('-q|--quick [bool]', 'Skip complex language analysis (alias for -{A|I|H|S}=false)', false)
2727
.option('-o|--offline [bool]', 'Use packaged data files instead of fetching latest from GitHub', false)
2828
.option('-L|--calculateLines [bool]', 'Calculate lines of code totals', true)
@@ -67,17 +67,20 @@ if (args.analyze) (async () => {
6767
const totalSize = languages.bytes;
6868
const minSizeAmt = parseFloat(args.minSize.replace(/[a-z]+$/i, '')); // '2KB' -> 2
6969
const minSizeUnit = args.minSize.replace(/^\d+/, '').toLowerCase(); // '2KB' -> 'kb'
70+
const checkBytes = minSizeUnit !== 'loc'; // whether to check bytes or loc
7071
const conversionFactors: Record<string, (n: number) => number> = {
7172
'b': n => n,
7273
'kb': n => n * 1e3,
7374
'mb': n => n * 1e6,
7475
'%': n => n * totalSize / 100,
76+
'loc': n => n,
7577
};
7678
const minBytesSize = conversionFactors[minSizeUnit](+minSizeAmt);
7779
const other = { bytes: 0, lines: { total: 0, content: 0, code: 0 } };
7880
// Apply specified minimums: delete language results that do not reach the threshold
7981
for (const [lang, data] of Object.entries(languages.results)) {
80-
if (data.bytes < minBytesSize) {
82+
const checkUnit = checkBytes ? data.bytes : data.lines.code;
83+
if (checkUnit < minBytesSize) {
8184
// Add to 'other' count
8285
other.bytes += data.bytes;
8386
other.lines.total += data.lines.total;
@@ -87,7 +90,9 @@ if (args.analyze) (async () => {
8790
delete languages.results[lang];
8891
}
8992
}
90-
languages.results['Other'] = { ...other, type: null! };
93+
if (other.bytes) {
94+
languages.results["Other"] = { ...other, type: null! };
95+
}
9196
}
9297

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

0 commit comments

Comments
 (0)