Files
payload/src/utilities/formatLabels.ts
Elliot DeNolf b383eb65c6 feat: autolabel fields when label is omitted (#42)
* feat: autolabel fields when omitted

* feat: handle autolabel in graphql mutation build

* feat: autolabel blocks

* test: add required slug field to blocks

* feat: handle graphql names when label is false

* feat: adds relationship field to test searchable input

* feat: handle block cell type labeling pluralization

* docs: remove all explicit labeling, no longer needed

* fix: falsey column labels, allows false array labels

* fix: client tests

* fix: auto-labels globals

* docs: globals auto-labeling and hooks clarification

* fix; proper object type naming

Co-authored-by: James <james@trbl.design>
2021-04-16 22:37:08 -04:00

40 lines
1.0 KiB
TypeScript

import pluralize, { isPlural, singular } from 'pluralize';
const capitalizeFirstLetter = (string: string): string => string.charAt(0).toUpperCase() + string.slice(1);
const toWords = (inputString: string, joinWords = false): string => {
const notNullString = inputString || '';
const trimmedString = notNullString.trim();
const arrayOfStrings = trimmedString.split(/[\s-]/);
const splitStringsArray = [];
arrayOfStrings.forEach((tempString) => {
if (tempString !== '') {
const splitWords = tempString.split(/(?=[A-Z])/).join(' ');
splitStringsArray.push(capitalizeFirstLetter(splitWords));
}
});
return joinWords
? splitStringsArray.join('').replace(/\s/gi, '')
: splitStringsArray.join(' ');
};
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 {
formatLabels,
toWords,
};