Skip to content

Commit

Permalink
πŸ‘―β€β™€οΈ Improve the plural logging function
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanc1 committed Oct 20, 2023
1 parent 52da08e commit ffd6a1e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
6 changes: 6 additions & 0 deletions .changeset/fluffy-dots-decide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'myst-cli-utils': patch
'myst-cli': patch
---

Improve plural function to add `y|ies` endings for words.
7 changes: 6 additions & 1 deletion packages/myst-cli-utils/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ export function tic() {
*
* `plural('%s book(s)', books)`
*
* `plural('%s dependenc(y|ies)', deps)`
*
* If passed an object as the second argument, the number of keys will be used.
*/
export function plural(f: string, count?: number | any[] | Record<any, any>): string {
Expand All @@ -60,5 +62,8 @@ export function plural(f: string, count?: number | any[] | Record<any, any>): st
: Array.isArray(count)
? count?.length
: Object.keys(count ?? {}).length) ?? 0;
return f.replace('%s', String(num)).replace(/\(s\)/g, num === 1 ? '' : 's');
return f
.replace('%s', String(num))
.replace(/\(s\)/g, num === 1 ? '' : 's')
.replace(/\(([a-z0-9A-Z-]*)\|([a-z0-9A-Z-]*)\)/g, num === 1 ? '$1' : '$2');
}
9 changes: 5 additions & 4 deletions packages/myst-cli/src/build/utils/getFileContent.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { resolve } from 'node:path';
import { tic } from 'myst-cli-utils';
import { plural, tic } from 'myst-cli-utils';
import type { LinkTransformer } from 'myst-transforms';
import type { ISession } from '../../session/types.js';
import {
Expand Down Expand Up @@ -92,9 +92,10 @@ export async function getFileContent(
);
session.log.info(
toc(
`πŸ“š Built ${allFiles.length} pages for export (including ${
allFiles.length - files.length
} dependencies) from ${projectPath} in %s.`,
`πŸ“š Built ${plural('%s page(s)', allFiles)} for export (including ${plural(
'%s dependenc(y|ies)',
allFiles.length - files.length,
)}) from ${projectPath} in %s.`,
),
);
return selectedFiles;
Expand Down

0 comments on commit ffd6a1e

Please sign in to comment.