From 4038c3cca6dd5ef6179a4ae8ab2e42c2cf6ca3da Mon Sep 17 00:00:00 2001 From: Eric Amodio Date: Sun, 31 Oct 2021 12:26:36 -0400 Subject: [PATCH] Updates String.pluralize options --- src/commands/git/branch.ts | 5 +---- src/commands/git/search.ts | 2 +- src/system/string.ts | 20 ++++++++++++++++---- src/views/nodes/contributorNode.ts | 8 ++++---- src/views/nodes/searchResultsNode.ts | 2 +- 5 files changed, 23 insertions(+), 14 deletions(-) diff --git a/src/commands/git/branch.ts b/src/commands/git/branch.ts index 7f6a47f..81f39c1 100644 --- a/src/commands/git/branch.ts +++ b/src/commands/git/branch.ts @@ -353,10 +353,7 @@ export class BranchGitCommand extends QuickCommand { } context.title = getTitle( - Strings.pluralize('Branch', state.references.length, { - number: '', - plural: 'Branches', - }).trim(), + Strings.pluralize('Branch', state.references.length, { only: true, plural: 'Branches' }), state.subcommand, ); diff --git a/src/commands/git/search.ts b/src/commands/git/search.ts index d11e59f..08110a7 100644 --- a/src/commands/git/search.ts +++ b/src/commands/git/search.ts @@ -189,7 +189,7 @@ export class SearchGitCommand extends QuickCommand { log == null ? `No results for ${state.pattern}` : `${Strings.pluralize('result', log.count, { - number: log.hasMore ? `${log.count}+` : undefined, + format: c => (log.hasMore ? `${c}+` : undefined), })} for ${state.pattern}`, picked: context.commit?.ref, showInSideBarCommand: new ActionQuickPickItem( diff --git a/src/system/string.ts b/src/system/string.ts index 4a09391..e830f22 100644 --- a/src/system/string.ts +++ b/src/system/string.ts @@ -345,13 +345,25 @@ export function padRightOrTruncate(s: string, max: number, padding?: string, wid export function pluralize( s: string, count: number, - options?: { infix?: string; number?: string; plural?: string; zero?: string }, + options?: { + /** Controls the character/string between the count and the string */ + infix?: string; + /** Formats the count */ + format?: (count: number) => string | undefined; + /** Controls if only the string should be included */ + only?: boolean; + /** Controls the plural version of the string */ + plural?: string; + /** Controls the string for a zero value */ + zero?: string; + }, ) { if (options == null) return `${count} ${s}${count === 1 ? emptyStr : 's'}`; - return `${ - count === 0 ? (options.zero != null ? options.zero : count) : options.number != null ? options.number : count - }${options.infix ?? ' '}${count === 1 ? s : options.plural ?? `${s}s`}`; + const suffix = count === 1 ? s : options.plural ?? `${s}s`; + if (options.only) return suffix; + + return `${count === 0 ? options.zero ?? count : options.format?.(count) ?? count}${options.infix ?? ' '}${suffix}`; } // Removes \ / : * ? " < > | and C0 and C1 control codes diff --git a/src/views/nodes/contributorNode.ts b/src/views/nodes/contributorNode.ts index 2d4655d..6786efc 100644 --- a/src/views/nodes/contributorNode.ts +++ b/src/views/nodes/contributorNode.ts @@ -112,11 +112,11 @@ export class ContributorNode extends ViewNode implements label.resultsType === undefined ? { singular: 'result', plural: 'results' } : label.resultsType; return `${Strings.pluralize(resultsType.singular, count, { - number: log?.hasMore ?? false ? `${count}+` : undefined, + format: c => (log?.hasMore ? `${c}+` : undefined), plural: resultsType.plural, zero: 'No', })} ${label.label}`;