Skip to content

Commit

Permalink
Add plural util
Browse files Browse the repository at this point in the history
  • Loading branch information
martpie committed Dec 7, 2024
1 parent 1941066 commit 27782b5
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
19 changes: 19 additions & 0 deletions src/lib/__tests__/localization.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { describe, expect, test } from 'bun:test';
import { plural } from '../localization';

describe('localization', () => {
describe('plural', () => {
test("should add a 's' to strings with a count of 0 or 2 or more", () => {
expect(plural('album', 0)).toEqual('albums');
expect(plural('album', 2)).toEqual('albums');
expect(plural('album', 3)).toEqual('albums');
expect(plural('album', 10)).toEqual('albums');
expect(plural('track', 10)).toEqual('tracks');
});

test("should not add a 's' to strings with a count of 1", () => {
expect(plural('album', 1)).toEqual('album');
expect(plural('track', 1)).toEqual('track');
});
});
});
6 changes: 6 additions & 0 deletions src/lib/localization.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* Pluralize a word
*/
export function plural(str: string, count: number) {
return count === 1 ? str : `${str}s`;
}
5 changes: 3 additions & 2 deletions src/lib/utils-library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import uniq from 'lodash-es/uniq';
import type { SortBy, SortOrder, Track } from '../generated/typings';
import { parseDuration } from '../hooks/useFormattedDuration';
import type { Path } from '../types/museeks';
import { plural } from './localization';

/**
* Filter an array of tracks by string
Expand Down Expand Up @@ -39,10 +40,10 @@ export function sortTracks(
* Format a list of tracks to a nice status
*/
export function getStatus(tracks: Track[]): string {
const status = parseDuration(
const duration = parseDuration(
tracks.map((d) => d.duration).reduce((a, b) => a + b, 0),
);
return `${tracks.length} track${tracks.length !== 1 ? 's' : ''}, ${status}`;
return `${tracks.length} ${plural('track', tracks.length)}, ${duration}`;
}

/**
Expand Down

0 comments on commit 27782b5

Please sign in to comment.