export function capitalize(string) {
  return string.replace(/^\w/, (c) => c.toUpperCase());
}

export function anchorLink(string) {
  return string.replace(/\s/g, '-').toLowerCase();
}

export function leftTrim(str) {
  if (!str) return str;
  // remove space from begining
  const trim = str.replace(/^\s+/g, '');
  // remove multiple spaces
  const output = trim.replace(/ +(?= )/g, '');
  return output;
}

export function truncateString(str, num, dots = '...') {
  if (!str || str.length <= num) {
    return str;
  }
  let trimmedString = str.substr(0, num);

  if (str.charAt(num) === ' ') {
    return `${trimmedString}${dots}`;
  }
  const hasSpace = trimmedString.lastIndexOf(' ') !== -1;
  if (!hasSpace) {
    return `${trimmedString}${dots}`;
  }
  trimmedString = trimmedString.substr(0, trimmedString.lastIndexOf(' '));
  return `${trimmedString}${dots}`;
}



export const getLaunchDate = (foundedDate) => {
  if (foundedDate) {
    const arr = foundedDate.split('-');
    if (arr.length === 3) {
      return `${arr[0]}`;
    }
  }

  return null;
};

export const removeSymbols = (string) => string.replace(/[!?@Â£#$%^&*():;"'|/.,~`]/g, '');

const monthNames = [
  'January',
  'February',
  'March',
  'April',
  'May',
  'June',
  'July',
  'August',
  'September',
  'October',
  'November',
  'December',
];

export const getDate = () => {
  const dateTime = new Date();
  const date = dateTime.getDate();
  const month = monthNames[dateTime.getMonth()];
  const year = dateTime.getFullYear();

  return {
    date,
    month,
    year,
  };
};
