Skip to content

Commit

Permalink
Add minSize option which adds to other result
Browse files Browse the repository at this point in the history
An implementation of a `minSize` option (proposed in #32).

This allows filtering the output to only show languages with a resulting size greater than what the user inputs.

This is done by moving the results of these too-small languages to an `other` key and deleting the original results.

This may be too ad-hoc - for instance, maybe this should be a CLI-only option and the actual data isn't changed? Is the `other` key needed, or not?

For this reason this feature is not being committed straight to main and will sit as a pull request for proposal.
  • Loading branch information
Nixinova committed Jul 23, 2024
1 parent f7dc8ac commit fef9713
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
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

0 comments on commit fef9713

Please sign in to comment.