File size: 1,791 Bytes
56b61e0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// utils.js

import axios from 'axios';

/**
 * Fetches the country name from a given country code.
 * @param {string} code - The country code (e.g., 'ind', 'usa').
 * @returns {Promise<string>} - The country name.
 */
export const getCountryName = async (code) => {
  try {
    const response = await axios.get(`https://restcountries.com/v3.1/alpha/${code}`);
    let name = response.data[0]?.name?.common || "Unknown Country";
    console.log(name);
    return name
  } catch (error) {
    console.error("Failed to fetch country name:", error);
    return "Unknown Country";
  }
};

/**
 * Formats a date string into a more readable format.
 * @param {string} dateStr - The date string (e.g., '2023-08-04').
 * @returns {string} - The formatted date.
 */
export const formatDate = (dateStr) => {
  const options = { year: 'numeric', month: 'long', day: 'numeric' };
  return new Date(dateStr).toLocaleDateString(undefined, options);
};

/**
 * Capitalizes the first letter of a string.
 * @param {string} str - The string to capitalize.
 * @returns {string} - The capitalized string.
 */
export const capitalizeFirstLetter = (str) => {
  if (!str) return '';
  return str.charAt(0).toUpperCase() + str.slice(1);
};

/**
 * Checks if a value is a valid email address.
 * @param {string} email - The email address to validate.
 * @returns {boolean} - True if valid, false otherwise.
 */
export const isValidEmail = (email) => {
  const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
  return emailRegex.test(email);
};

/**
 * Converts a string to a URL-friendly slug.
 * @param {string} text - The text to convert.
 * @returns {string} - The slugified text.
 */
export const slugify = (text) => {
  return text
    .toString()
    .toLowerCase()
    .trim()
    .replace(/[\s\W-]+/g, '-');
};