Skip to content

Commit 3dc7c1e

Browse files
committed
Adds number formatting to pluralize
1 parent 8291a3d commit 3dc7c1e

File tree

3 files changed

+41
-23
lines changed

3 files changed

+41
-23
lines changed

src/system/date.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,14 @@ export function formatNumeric(
358358
style?: 'decimal' | 'currency' | 'percent' | 'unit' | null | undefined,
359359
locale?: string,
360360
): string {
361+
const format = getNumericFormat(style, locale);
362+
return format(value);
363+
}
364+
365+
export function getNumericFormat(
366+
style?: 'decimal' | 'currency' | 'percent' | 'unit' | null | undefined,
367+
locale?: string,
368+
): Intl.NumberFormat['format'] {
361369
if (style == null) {
362370
style = 'decimal';
363371
}
@@ -381,5 +389,5 @@ export function formatNumeric(
381389
numberFormatCache.set(key, formatter);
382390
}
383391

384-
return formatter.format(value);
392+
return formatter.format;
385393
}

src/system/string.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type {
66
} from '@gk-nzaytsev/fast-string-truncated-width';
77
import getTruncatedStringWidth from '@gk-nzaytsev/fast-string-truncated-width';
88
import { CharCode } from '../constants';
9+
import { getNumericFormat } from './date';
910

1011
export { fromBase64, base64 } from '@env/base64';
1112

@@ -479,14 +480,16 @@ export function padOrTruncateEnd(s: string, maxLength: number, fillString?: stri
479480
return s.padEnd(maxLength, fillString);
480481
}
481482

483+
let numericFormat: ReturnType<typeof getNumericFormat> | undefined;
484+
482485
export function pluralize(
483486
s: string,
484487
count: number,
485488
options?: {
486489
/** Controls the character/string between the count and the string */
487490
infix?: string;
488491
/** Formats the count */
489-
format?: (count: number) => string | undefined;
492+
format?: false | ((count: number) => string | undefined);
490493
/** Controls if only the string should be included */
491494
only?: boolean;
492495
/** Controls the plural version of the string */
@@ -495,12 +498,27 @@ export function pluralize(
495498
zero?: string;
496499
},
497500
) {
498-
if (options == null) return `${count} ${s}${count === 1 ? '' : 's'}`;
501+
if (options == null) {
502+
numericFormat ??= getNumericFormat();
503+
return `${numericFormat(count)} ${s}${count === 1 ? '' : 's'}`;
504+
}
499505

500506
const suffix = count === 1 ? s : options.plural ?? `${s}s`;
501507
if (options.only) return suffix;
502508

503-
return `${count === 0 ? options.zero ?? count : options.format?.(count) ?? count}${options.infix ?? ' '}${suffix}`;
509+
let result;
510+
if (count === 0) {
511+
result = options.zero ?? count;
512+
} else if (options.format === false) {
513+
result = count;
514+
} else if (options.format != null) {
515+
result = options.format(count);
516+
} else {
517+
numericFormat ??= getNumericFormat();
518+
result = numericFormat(count);
519+
}
520+
521+
return `${result}${options.infix ?? ' '}${suffix}`;
504522
}
505523

506524
// Removes \ / : * ? " < > | and C0 and C1 control codes

src/views/nodes/contributorNode.ts

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { GlyphChars } from '../../constants';
44
import type { GitUri } from '../../git/gitUri';
55
import type { GitContributor } from '../../git/models/contributor';
66
import type { GitLog } from '../../git/models/log';
7+
import { formatNumeric } from '../../system/date';
78
import { gate } from '../../system/decorators/gate';
89
import { debug } from '../../system/decorators/log';
910
import { map } from '../../system/iterable';
@@ -85,17 +86,15 @@ export class ContributorNode extends ViewNode<'contributor', ViewsWithContributo
8586
async getTreeItem(): Promise<TreeItem> {
8687
const presence = this.options?.presence?.get(this.contributor.email!);
8788

88-
const numberFormatter = new Intl.NumberFormat();
89-
9089
const shortStats =
9190
this.contributor.stats != null
92-
? ` (${pluralize('file', this.contributor.stats.files, {
93-
format: numberFormatter.format,
94-
})}, +${numberFormatter.format(this.contributor.stats.additions)} -${numberFormatter.format(
91+
? ` (${pluralize('file', this.contributor.stats.files)}, +${formatNumeric(
9592
this.contributor.stats.additions,
96-
)} ${pluralize('line', this.contributor.stats.additions + this.contributor.stats.deletions, {
97-
only: true,
98-
})})`
93+
)} -${formatNumeric(this.contributor.stats.additions)} ${pluralize(
94+
'line',
95+
this.contributor.stats.additions + this.contributor.stats.deletions,
96+
{ only: true },
97+
)})`
9998
: '';
10099

101100
const item = new TreeItem(
@@ -113,9 +112,6 @@ export class ContributorNode extends ViewNode<'contributor', ViewsWithContributo
113112
}${this.contributor.date != null ? `${this.contributor.formatDateFromNow()}, ` : ''}${pluralize(
114113
'commit',
115114
this.contributor.count,
116-
{
117-
format: numberFormatter.format,
118-
},
119115
)}${shortStats}`;
120116

121117
let avatarUri;
@@ -146,13 +142,10 @@ export class ContributorNode extends ViewNode<'contributor', ViewsWithContributo
146142

147143
const stats =
148144
this.contributor.stats != null
149-
? `\\\n${pluralize('file', this.contributor.stats.files, {
150-
format: numberFormatter.format,
151-
})} changed, ${pluralize('addition', this.contributor.stats.additions, {
152-
format: numberFormatter.format,
153-
})}, ${pluralize('deletion', this.contributor.stats.deletions, {
154-
format: numberFormatter.format,
155-
})}`
145+
? `\\\n${pluralize('file', this.contributor.stats.files)} changed, ${pluralize(
146+
'addition',
147+
this.contributor.stats.additions,
148+
)}, ${pluralize('deletion', this.contributor.stats.deletions)}`
156149
: '';
157150

158151
const link = this.contributor.email
@@ -168,7 +161,6 @@ export class ContributorNode extends ViewNode<'contributor', ViewsWithContributo
168161
`${avatarMarkdown != null ? avatarMarkdown : ''} &nbsp;${link} \n\n${lastCommitted}${pluralize(
169162
'commit',
170163
this.contributor.count,
171-
{ format: numberFormatter.format },
172164
)}${stats}`,
173165
);
174166
markdown.supportHtml = true;

0 commit comments

Comments
 (0)