Vercel/src/utility/string.ts
2023-12-11 20:36:01 -06:00

27 lines
690 B
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

export const convertStringToArray = (
string?: string,
shouldParameterize = true,
) => string
? string.split(',').map(tag => shouldParameterize
? parameterize(tag)
: tag.trim())
: undefined;
export const capitalize = (string: string) =>
string.charAt(0).toUpperCase() + string.slice(1);
export const capitalizeWords = (string = '') =>
string
.split(' ')
.map(capitalize)
.join(' ');
export const parameterize = (string: string) =>
string
.trim()
// Replaces spaces, underscores, and dashes with dashes
.replaceAll(/[\s_—]/gi, '-')
// Removes all non-alphanumeric characters
.replaceAll(/([^a-z0-9-])/gi, '')
.toLowerCase();