handle singular and plural slugs for auto-label

This commit is contained in:
Elliot DeNolf
2020-11-22 08:24:42 -05:00
parent 2a4b821c34
commit e4eece0352
2 changed files with 21 additions and 16 deletions

View File

@@ -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',
});

View File

@@ -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;