format method

String format(
  1. ILibDateOptions components
)

Format a duration according to this formatter's style/length.

Iterate the style's component list from smallest to largest, skip undefined/zero fields, join with the separator (finalSeparator before the last field at full length), then append the clock time and prepend the RTL marker when needed.

Implementation

String format(ILibDateOptions components) {
  final List<String> list = _complist[_style]!;

  // Field values indexed by component name.
  final Map<String, num?> vals = <String, num?>{
    'year': components.year,
    'month': components.month,
    'week': components.week,
    'day': components.day,
    'hour': components.hour,
    'minute': components.minute,
    'second': components.second,
    'millisecond': components.millisecond,
  };

  String str = '';
  bool secondlast = true;

  for (int i = list.length - 1; i >= 0; i--) {
    final num? value = vals[list[i]];
    if (value != null && value != 0) {
      if (str.isNotEmpty) {
        str = ((_length == 'full' && secondlast)
                ? _components['finalSeparator']!
                : _components['separator']!) +
            str;
        secondlast = false;
      }
      str = _formatChoice(_components[list[i]]!, value, _mapDigits(value)) +
          str;
    }
  }

  if (_style == 'clock') {
    final ILibDateFmt? fmt;
    if (components.hour != null) {
      fmt = (components.second != null) ? _timeFmtHMS : _timeFmtHM;
    } else {
      fmt = _timeFmtMS;
    }
    if (fmt != null) {
      if (str.isNotEmpty) {
        str += _components['separator']!;
      }
      str += fmt.format(
        ILibDateOptions(
          hour: components.hour ?? 0,
          minute: components.minute ?? 0,
          second: components.second ?? 0,
        ),
      );
    }
  }

  if (_scriptDirection == 'rtl') {
    str = '\u200F$str';
  }
  return str;
}