Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add minSize option which moves small languages into other result #35

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +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('-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('-V|--keepVendored [bool]', 'Prevent skipping over vendored/generated files', false)
Expand Down
24 changes: 24 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,30 @@ async function analyse(rawPaths?: string | string[], opts: T.Options = {}): Prom
results.languages.bytes += fileSize;
}

// Ignore languages with a bytes/% size less than the declared min size
if (opts.minSize) {
const totalSize = results.languages.bytes;
const minSizeAmt = parseFloat(opts.minSize.replace(/[a-z]+$/i, '')); // '2KB' -> 2
const minSizeUnit = opts.minSize.replace(/^\d+/, '').toLowerCase(); // '2KB' -> 'kb'
const conversionFactors: Record<string, (n: number) => number> = {
'b': n => n,
'kb': n => n * 1e3,
'mb': n => n * 1e6,
'%': n => n * totalSize / 100,
};
const minBytesSize = conversionFactors[minSizeUnit](+minSizeAmt);
// Apply specified minimums: delete language results that do not reach the threshold
for (const [lang, data] of Object.entries(results.languages.results)) {
if (data.bytes < minBytesSize) {
// Add data to 'Other' result
results.languages.results.other ??= { type: 'data' /*arbitrary*/, bytes: 0 };
results.languages.results.other.bytes += data.bytes;
// Remove language result
delete results.languages.results[lang];
}
}
}

// Set counts
results.files.count = Object.keys(results.files.results).length;
results.languages.count = Object.keys(results.languages.results).length;
Expand Down
3 changes: 2 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export interface Options {
keepVendored?: boolean
keepBinary?: boolean
relativePaths?: boolean
minSize?: `${number}${Lowercase<'B' | 'KB' | 'MB' | '%'>}`
childLanguages?: boolean
quick?: boolean
offline?: boolean
Expand All @@ -39,7 +40,7 @@ export interface Results {
languages: {
count: Integer
bytes: Bytes
results: Record<Language, {
results: Record<Language | 'other', {
bytes: Bytes
type: Category
parent?: Language
Expand Down
Loading