| 1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- /**
- * String utility functions
- */
- import { type CountryCode, parsePhoneNumberWithError } from 'libphonenumber-js'
- /**
- * Convert a string to a slug format
- * Removes special characters, replaces spaces with hyphens, and converts to lowercase
- *
- * @param text The text to convert to slug format
- * @returns The slugified text
- */
- export function slugify(text: string): string {
- return text
- .toString()
- .normalize('NFD') // Split accented characters
- .replace(/[\u0300-\u036F]/g, '') // Remove diacritics
- .toLowerCase()
- .trim()
- .replace(/\s+/g, '-') // Replace spaces with -
- .replace(/[^\w-]+/g, '') // Remove all non-word chars
- .replace(/--+/g, '-') // Replace multiple - with single -
- .replace(/^-+/, '') // Trim - from start of text
- .replace(/-+$/, '') // Trim - from end of text
- .substring(0, 30) // Limit to 30 characters
- }
- // Function to convert phone number to international format
- export function convertPhoneNumberToInternationalFormat(
- phone: string,
- countryCode: CountryCode = 'FR'
- ): string {
- try {
- // Use the provided country code or default to FR
- const phoneNumber = parsePhoneNumberWithError(phone, countryCode)
- if (phoneNumber && phoneNumber.isValid()) {
- return phoneNumber.format('E.164') // format: +33123456789
- }
- return phone
- } catch (error) {
- console.error('Error converting phone number:', error)
- return phone
- }
- }
|