From a7512049b2755800ae4e90e7ef8def7ea653715a Mon Sep 17 00:00:00 2001 From: Lisa Kim Date: Wed, 23 Oct 2024 18:52:02 -0700 Subject: [PATCH] Remove brackets around resource kinds --- .../RequestCheckout/RequestCheckout.tsx | 12 +++--- web/packages/shared/utils/text.test.ts | 38 ++++++++++++++++++- web/packages/shared/utils/text.ts | 25 ++++++++++++ 3 files changed, 68 insertions(+), 7 deletions(-) diff --git a/web/packages/shared/components/AccessRequests/NewRequest/RequestCheckout/RequestCheckout.tsx b/web/packages/shared/components/AccessRequests/NewRequest/RequestCheckout/RequestCheckout.tsx index 8e28c59e7d080..a29deb7786799 100644 --- a/web/packages/shared/components/AccessRequests/NewRequest/RequestCheckout/RequestCheckout.tsx +++ b/web/packages/shared/components/AccessRequests/NewRequest/RequestCheckout/RequestCheckout.tsx @@ -42,10 +42,11 @@ import { Danger } from 'design/Alert'; import Validation, { useRule, Validator } from 'shared/components/Validation'; import { Attempt } from 'shared/hooks/useAttemptNext'; -import { pluralize } from 'shared/utils/text'; +import { listToSentence, pluralize } from 'shared/utils/text'; import { Option } from 'shared/components/Select'; import { FieldCheckbox } from 'shared/components/FieldCheckbox'; import { mergeRefs } from 'shared/libs/mergeRefs'; +import { TextSelectCopyMulti } from 'shared/components/TextSelectCopy'; import { RequestableResourceKind } from 'shared/components/AccessRequests/NewRequest/resource'; import { CreateRequest } from '../../Shared/types'; @@ -66,8 +67,6 @@ import { CrossIcon } from './CrossIcon'; import type { TransitionStatus } from 'react-transition-group'; import type { AccessRequest } from 'shared/services/accessRequests'; -import { P } from 'design/Text/Text'; -import { TextSelectCopyMulti } from 'shared/components/TextSelectCopy'; export const RequestCheckoutWithSlider = forwardRef< HTMLDivElement, @@ -299,9 +298,10 @@ export function RequestCheckout({ {hasUnsupportedKubeRequestModes && ( - You can only request Kubernetes resource kind [ - {unsupportedKubeRequestModes.join(', ')}] for cluster{' '} - {affectedKubeClusterName}. Requesting those + You can only request Kubernetes resource{' '} + {pluralize(unsupportedKubeRequestModes.length, 'kind')}{' '} + {listToSentence(unsupportedKubeRequestModes)} for + cluster {affectedKubeClusterName}. Requesting those resource kinds is currently only supported through the{' '} . */ -import { pluralize, capitalizeFirstLetter } from './text'; +import { pluralize, capitalizeFirstLetter, listToSentence } from './text'; test('pluralize', () => { expect(pluralize(0, 'apple')).toBe('apples'); @@ -28,3 +28,39 @@ test('capitalizeFirstLetter', () => { expect(capitalizeFirstLetter('hello')).toBe('Hello'); expect(capitalizeFirstLetter('')).toBe(''); }); + +describe('listToSentence()', () => { + const testCases = [ + { + name: 'no words', + list: [], + expected: '', + }, + { + name: 'one word', + list: ['a'], + expected: 'a', + }, + { + name: 'two words', + list: ['a', 'b'], + expected: 'a and b', + }, + { + name: 'three words', + list: ['a', 'b', 'c'], + expected: 'a, b and c', + }, + { + name: 'lost of words', + list: ['a', 'b', 'c', 'd', 'e', 'f', 'g'], + expected: 'a, b, c, d, e, f and g', + }, + ]; + + test.each(testCases)('$name', ({ list, expected }) => { + const originalList = [...list]; + expect(listToSentence(list)).toEqual(expected); + expect(list).toEqual(originalList); + }); +}); diff --git a/web/packages/shared/utils/text.ts b/web/packages/shared/utils/text.ts index a424ea5bd9b14..37635c4c0bad2 100644 --- a/web/packages/shared/utils/text.ts +++ b/web/packages/shared/utils/text.ts @@ -39,3 +39,28 @@ export function capitalizeFirstLetter(str: string) { } return str[0].toUpperCase() + str.slice(1); } + +/** + * Takes a list of words and converts it into a sentence. + * eg: given list ["apple", "banana", "carrot"], converts + * to string "apple, banana and carrot" + * + * Does not modify original list. + */ +export function listToSentence(listOfWords: string[]) { + if (!listOfWords || !listOfWords.length) { + return ''; + } + + if (listOfWords.length == 1) { + return listOfWords[0]; + } + + if (listOfWords.length == 2) { + return `${listOfWords[0]} and ${listOfWords[1]}`; + } + + const copiedList = [...listOfWords]; + const lastWord = copiedList.pop(); + return `${copiedList.join(', ')} and ${lastWord}`; +}