From e4eece03526ffa4e6fcdb18838a8c012ca46b5d1 Mon Sep 17 00:00:00 2001 From: Elliot DeNolf Date: Sun, 22 Nov 2020 08:24:42 -0500 Subject: [PATCH] handle singular and plural slugs for auto-label --- src/utilities/formatLabels.spec.ts | 14 +++++++------- src/utilities/formatLabels.ts | 23 ++++++++++++++--------- 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/src/utilities/formatLabels.spec.ts b/src/utilities/formatLabels.spec.ts index 464c4699cb..587958df55 100644 --- a/src/utilities/formatLabels.spec.ts +++ b/src/utilities/formatLabels.spec.ts @@ -1,29 +1,29 @@ import formatLabels from './formatLabels'; describe('formatLabels', () => { - it('should format single word', () => { + it('should format singular slug', () => { expect(formatLabels('word')).toMatchObject({ singular: 'Word', plural: 'Words', }); }); - it('should format already plural', () => { + it('should format plural slug', () => { expect(formatLabels('words')).toMatchObject({ - singular: 'Words', + singular: 'Word', plural: 'Words', }); }); it('should format kebab case', () => { - expect(formatLabels('kebab-item')).toMatchObject({ - singular: 'Kebab Item', - plural: 'Kebab Items', + expect(formatLabels('my-slugs')).toMatchObject({ + singular: 'My Slug', + plural: 'My Slugs', }); }); it('should format camelCase', () => { - expect(formatLabels('camelCaseItem')).toMatchObject({ + expect(formatLabels('camelCaseItems')).toMatchObject({ singular: 'Camel Case Item', plural: 'Camel Case Items', }); diff --git a/src/utilities/formatLabels.ts b/src/utilities/formatLabels.ts index 870a9309af..4512d1abf0 100644 --- a/src/utilities/formatLabels.ts +++ b/src/utilities/formatLabels.ts @@ -1,8 +1,8 @@ -import pluralize from 'pluralize'; +import pluralize, { isPlural, singular } from 'pluralize'; -const capitalizeFirstLetter = (string) => string.charAt(0).toUpperCase() + string.slice(1); +const capitalizeFirstLetter = (string: string) => string.charAt(0).toUpperCase() + string.slice(1); -const toWords = (inputString) => { +const toWords = (inputString: string): string => { const notNullString = inputString || ''; const trimmedString = notNullString.trim(); const arrayOfStrings = trimmedString.split(/[\s-]/); @@ -18,12 +18,17 @@ const toWords = (inputString) => { return splitStringsArray.join(' '); }; -const formatLabels = ((input) => { - const words = toWords(input); - return { - singular: words, - plural: pluralize(words), - }; +const formatLabels = ((slug: string): { singular: string, plural: string} => { + const words = toWords(slug); + return (isPlural(slug)) + ? { + singular: singular(words), + plural: words, + } + : { + singular: words, + plural: pluralize(words), + }; }); export default formatLabels;