export function getStringByFormat()

in web/frontend/src/assets/hd-date/utils.ts [93:135]


export function getStringByFormat(values: string[], format: string, formatString?: string): string {
  const separators = format.match(/[\/,\-,\.,\:,\,,\\,\s]/gi);
  const letters = formatString != null ? formatString.match(REGULAR_LETTERS) : null;
  const arrayFormat: string[] = format.match(REGULAR_LETTERS);
  let str = '';
  let count = 0;
  for (let i = 0; i < arrayFormat.length; i++) {
    if (values[i] != null) {
      if (arrayFormat[i].length > values[i].length) {
        if (
          (arrayFormat[i].length === 3 ||
            arrayFormat[i].length === 6 ||
            arrayFormat[i].length === 9) &&
          arrayFormat[i] !== DateTimeFormat.Www
        ) {
          const val = values[i].length < 3 ? padStart(3 - values[i].length, values[i]) : values[i];
          str += val + padStart(arrayFormat[i].length - val.length, '');
        } else if (
          arrayFormat[i] === DateTimeFormat.MMM ||
          arrayFormat[i] === DateTimeFormat.MMMM ||
          arrayFormat[i] === DateTimeFormat.Www
        ) {
          str += values[i];
        } else {
          str += padStart(arrayFormat[i].length - values[i].length, values[i]);
        }
      } else {
        str += values[i];
      }
    } else {
      if (arrayFormat[i] === DateTimeFormat.tt) {
        str += letters != null ? letters[0] : HOURS > 12 ? 'PM' : 'AM';
      } else {
        str += padStart(arrayFormat[i].length, '');
      }
    }
    if (separators != null && separators[count] != null) {
      str += separators[count];
      count++;
    }
  }
  return str;
}