truncateLeaves function
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;
}