diff --git a/web/packages/common/src/utils/datasetQuality.ts b/web/packages/common/src/utils/datasetQuality.ts index a8407d46b7..fa1346227d 100644 --- a/web/packages/common/src/utils/datasetQuality.ts +++ b/web/packages/common/src/utils/datasetQuality.ts @@ -2,6 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 import { findMessagesArray } from '@nemo/common/src/utils/file'; +import { getTextWithCount } from '@nemo/common/src/utils/formatters'; export type DatasetQualityCode = | 'EMPTY_FILE' @@ -41,10 +42,6 @@ const LONG_ENTRY_CHAR_THRESHOLD = 32_768; const PROMPT_KEYS = ['prompt', 'question']; const COMPLETION_KEYS = ['completion', 'ideal_response', 'response', 'output', 'answer']; -function plural(n: number, word: string): string { - return `${n} ${word}${n === 1 ? '' : 's'}`; -} - /** * Runs dataset quality checks on a JSONL file and returns a structured report. * Errors indicate the file should not be uploaded as-is; warnings are advisory. @@ -129,7 +126,7 @@ export async function checkDatasetQuality(file: File): Promise { + it('should return the correct text with count using default suffix', () => { + expect(getTextWithCount('test', 0)).toBe('0 tests'); + expect(getTextWithCount('test', 1)).toBe('1 test'); + expect(getTextWithCount('test', 2)).toBe('2 tests'); + }); + + it('should return the correct text with count using plural', () => { + expect(getTextWithCount('entry', 0, 'entries')).toBe('0 entries'); + expect(getTextWithCount('entry', 1, 'entries')).toBe('1 entry'); + expect(getTextWithCount('entry', 2, 'entries')).toBe('2 entries'); + }); +}); diff --git a/web/packages/common/src/utils/formatters.ts b/web/packages/common/src/utils/formatters.ts index 13eafdfea3..e7949d9d1e 100644 --- a/web/packages/common/src/utils/formatters.ts +++ b/web/packages/common/src/utils/formatters.ts @@ -1,6 +1,23 @@ // SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. // SPDX-License-Identifier: Apache-2.0 +/** + * Returns a formatted string with count and properly pluralized text. + * @param text - The singular form of the word (e.g., "entry", "file") + * @param count - The number to display + * @param plural - Optional custom plural form for irregular words (e.g., "entries", "children"). + * If not provided, defaults to appending 's' to the text. + * @returns Formatted string like "1 entry" or "3 entries" + * @example + * getTextWithCount('file', 1) // "1 file" + * getTextWithCount('file', 3) // "3 files" + * getTextWithCount('entry', 2, 'entries') // "2 entries" + */ +export const getTextWithCount = (text: string, count: number, plural?: string) => { + const pluralForm = plural ?? `${text}s`; + return `${count} ${count !== 1 ? pluralForm : text}`; +}; + /** * Truncates a long string of text to the length specified by `maxCharacters` by replacing a * section of the text with an ellipsis. diff --git a/web/packages/studio/src/util/strings.spec.ts b/web/packages/studio/src/util/strings.spec.ts index b882ff85cb..2ab8b828d7 100644 --- a/web/packages/studio/src/util/strings.spec.ts +++ b/web/packages/studio/src/util/strings.spec.ts @@ -1,21 +1,7 @@ // SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. // SPDX-License-Identifier: Apache-2.0 -import { capitalize, formatKeyLabel, getTextWithCount, parseCSV } from '@studio/util/strings'; - -describe('#getTextWithCount', () => { - it('should return the correct text with count using default suffix', () => { - expect(getTextWithCount('test', 0)).toBe('0 tests'); - expect(getTextWithCount('test', 1)).toBe('1 test'); - expect(getTextWithCount('test', 2)).toBe('2 tests'); - }); - - it('should return the correct text with count using plural', () => { - expect(getTextWithCount('entry', 0, 'entries')).toBe('0 entries'); - expect(getTextWithCount('entry', 1, 'entries')).toBe('1 entry'); - expect(getTextWithCount('entry', 2, 'entries')).toBe('2 entries'); - }); -}); +import { capitalize, formatKeyLabel, parseCSV } from '@studio/util/strings'; describe('#formatKeyLabel', () => { it.each([ diff --git a/web/packages/studio/src/util/strings.ts b/web/packages/studio/src/util/strings.ts index edbb489ad3..3e851ea97c 100644 --- a/web/packages/studio/src/util/strings.ts +++ b/web/packages/studio/src/util/strings.ts @@ -4,22 +4,7 @@ import { Row } from '@studio/util/files'; import Papa from 'papaparse'; -/** - * Returns a formatted string with count and properly pluralized text. - * @param text - The singular form of the word (e.g., "entry", "file") - * @param count - The number to display - * @param plural - Optional custom plural form for irregular words (e.g., "entries", "children"). - * If not provided, defaults to appending 's' to the text. - * @returns Formatted string like "1 entry" or "3 entries" - * @example - * getTextWithCount('file', 1) // "1 file" - * getTextWithCount('file', 3) // "3 files" - * getTextWithCount('entry', 2, 'entries') // "2 entries" - */ -export const getTextWithCount = (text: string, count: number, plural?: string) => { - const pluralForm = plural ?? `${text}s`; - return `${count} ${count !== 1 ? pluralForm : text}`; -}; +export { getTextWithCount } from '@nemo/common/src/utils/formatters'; export const capitalize = (str: string) => { return str.charAt(0).toUpperCase() + str.slice(1);