Skip to content

Commit

Permalink
🧪
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanc1 committed Oct 20, 2023
1 parent ffd6a1e commit 9c9b5dd
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 23 deletions.
3 changes: 2 additions & 1 deletion packages/myst-cli-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ export {
createNpmLogger,
} from './logger.js';
export { exec, makeExecutable } from './exec.js';
export { clirun, tic, plural } from './utils.js';
export { clirun, tic } from './utils.js';
export { plural } from './plural.js';
export { isUrl } from './isUrl.js';
export { Session, getSession } from './session.js';
export {
Expand Down
18 changes: 18 additions & 0 deletions packages/myst-cli-utils/src/plural.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { describe, expect, it } from 'vitest';
import { plural } from './plural';

describe('isUrl', () => {
it.each([
['%s book(s)', 0, '0 books'],
['%s book(s)', 1, '1 book'],
['%s book(s)', 2, '2 books'],
['%s dependenc(y|ies)', 0, '0 dependencies'],
['%s dependenc(y|ies)', 1, '1 dependency'],
['%s dependenc(y|ies)', 2, '2 dependencies'],
['%s stitch(es)', 0, '0 stitches'],
['%s stitch(es)', 1, '1 stitch'],
['%s stitch(es)', 2, '2 stitches'],
])('"%s" + %s ➡️ "%s"', (t, n, p) => {
expect(plural(t, n)).toEqual(p);
});
});
23 changes: 23 additions & 0 deletions packages/myst-cli-utils/src/plural.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Creates a plural version of a string to log to the console.
*
* `plural('%s book(s)', books)`
*
* `plural('%s stitch(es)', 3)`
*
* `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 {
const num =
(typeof count === 'number'
? count
: Array.isArray(count)
? count?.length
: Object.keys(count ?? {}).length) ?? 0;
return f
.replace('%s', String(num))
.replace(/\((?:([a-z0-9A-Z-]*)\|)?([a-z0-9A-Z-]*)\)/g, num === 1 ? '$1' : '$2');
}
22 changes: 0 additions & 22 deletions packages/myst-cli-utils/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,25 +45,3 @@ export function tic() {
}
return toc;
}

/**
* Creates a plural version of a string to log to the console.
*
* `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 {
const num =
(typeof count === 'number'
? count
: Array.isArray(count)
? count?.length
: Object.keys(count ?? {}).length) ?? 0;
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');
}

0 comments on commit 9c9b5dd

Please sign in to comment.