All files / src/domain/i18n/useCases getDateFormat.ts

91.66% Statements 11/12
100% Branches 0/0
100% Functions 0/0
91.66% Lines 11/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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      2x 2x       2x       2x       2x   1x       2x   2x 2x 2x 2x    
import getText from './getText';
 
function replaceMonth (pattern: string, monthIndex: number): string {
  const months = getText('date', 'months');
  return pattern.replace('MM', months[monthIndex]);
}
 
function replaceDay (pattern: string, day: string): string {
  return pattern.replace('DD', day);
}
 
function replaceYear (pattern: string, year: string): string {
  return pattern.replace('YYYY', year);
}
 
export default function getDateFormat (date: string): string {
  const pattern = getText('date', 'pattern');
  if (date.slice(-1) === 'Z') {
    date = date.slice(0, date.length - 1);
  } else if (date.includes('+')) {
    date = date.split('+')[0];
  }
  const objDate = new Date(date);
 
  let formattedDate = replaceMonth(pattern, objDate.getMonth());
  formattedDate = replaceDay(formattedDate, objDate.getDate().toString(10));
  formattedDate = replaceYear(formattedDate, objDate.getFullYear().toString(10));
  return formattedDate;
}