diff --git a/src/utils/strings.ts b/src/utils/strings.ts index 0128b2a8..9252a62e 100644 --- a/src/utils/strings.ts +++ b/src/utils/strings.ts @@ -1,5 +1,22 @@ // eslint-disable-next-line import/prefer-default-export export const singularise = (str: string) => { - const lastTwoChars = str.slice(-2); - return lastTwoChars === 'es' ? `${str.slice(0, -2)}y` : str.slice(0, -1); + // Non-exhaustive singulariser, only handles some common English cases so use with care / add more rules. + // Consider adding a string store if really needed. + if (str.endsWith('ies')) { + // e.g. assemblies, but will fail on species + return `${str.slice(0, -3)}y`; + } + if (str.endsWith('ses')) { + // e.g. analyses, but will fail on busses + return `${str.slice(0, -3)}sis`; + } + if (str.endsWith('s')) { + // e.g. projects, but will fail on boxes + return `${str.slice(0, -1)}`; + } + if (str.endsWith('a')) { + // e.g. data, but will fail on criteria + return `${str.slice(0, -1)}um`; + } + return str; };