truncateLeaves function

Object? truncateLeaves(
  1. Object? input, {
  2. required int maxLength,
})

Recursively truncates string leaves in nested Map/Iterable structures.

Non-string, non-collection values pass through unchanged. Returns null for null input.

Implementation

Object? truncateLeaves(Object? input, {required int maxLength}) {
  if (input == null) return null;
  if (input is String) return truncateString(input, maxLength: maxLength);
  if (input is Map) {
    return input.map(
      (k, v) => MapEntry(k.toString(), truncateLeaves(v, maxLength: maxLength)),
    );
  }
  if (input is Iterable) {
    return input.map((e) => truncateLeaves(e, maxLength: maxLength)).toList();
  }
  return input;
}