transform method

dynamic transform(
  1. dynamic value,
  2. dynamic pattern,
  3. dynamic replacement
)

Implementation

dynamic transform(dynamic value, dynamic /* String | RegExp */ pattern,
    dynamic /* Function | String */ replacement) {
  if (value == null) {
    return value;
  }
  if (!_supportedInput(value)) {
    throw InvalidPipeArgumentException(ReplacePipe, value);
  }
  var input = value.toString();
  if (!_supportedPattern(pattern)) {
    throw InvalidPipeArgumentException(ReplacePipe, pattern);
  }
  if (!_supportedReplacement(replacement)) {
    throw InvalidPipeArgumentException(ReplacePipe, replacement);
  }
  // template fails with literal RegExp e.g /pattern/igm
  if (replacement is String Function(Match)) {
    var rgxPattern =
        pattern is String ? RegExp(pattern) : (pattern as RegExp);
    return input.replaceAllMapped(rgxPattern, replacement);
  }
  if (pattern is RegExp) {
    // use the replaceAll variant
    return input.replaceAll(pattern, replacement as String);
  }
  return input.replaceFirst(pattern as String, replacement as String);
}