transform method

  1. @override
String transform(
  1. String input,
  2. String locale
)
override

Transforms input into an instance of U using the provided locale.

Implementation

@override
String transform(String input, String locale) {
  assert(length >= 0);

  final inputRunes = input.runes;

  if (inputRunes.length <= length) {
    return input;
  } else if (truncationIndicator.runes.length > length) {
    // Can't use the truncation indicator because it's longer than the desired length, so we'll just truncate without
    // it.
    final result = truncateAt == TruncateAt.end
        ? input.substring(0, length)
        : input.substring(input.length - length);
    return result;
  } else {
    // We need to incorporate the truncation indicator and take its length into account during truncation.
    if (truncateAt == TruncateAt.start) {
      final suffix = String.fromCharCodes(inputRunes.skip(
          inputRunes.length - length + truncationIndicator.runes.length));
      final result = '$truncationIndicator$suffix';
      return result;
    } else {
      final prefix = String.fromCharCodes(
          inputRunes.take(length - truncationIndicator.runes.length));
      final result = '$prefix$truncationIndicator';
      return result;
    }
  }
}