@spaced-out/ui-design-system
Version:
Sense UI components library
34 lines (28 loc) • 787 B
Flow
// @flow strict
export const capitalize = (word: string): string => {
if (!word) {
return '';
}
return word
.toLowerCase()
.replace(/\w/, (firstLetter) => firstLetter.toUpperCase());
};
export const escapeRegExp = (str: string): string =>
str.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
export const formatWord = (word: string, count: number): string =>
count === 1 ? word : word + 's';
export const truncateString = (
inputString: string,
maxLength: number,
): string => {
if (inputString.length > maxLength) {
return inputString.substring(0, maxLength - 3) + '...';
}
return inputString;
};
export const appendPx = (value?: number | string): string => {
if (typeof value === 'number') {
return `${value}px`;
}
return value ?? '';
};