apply method
Implementation
@override
Future<String> apply(String content, Map<String, dynamic> context) async {
return content.replaceAllMapped(_switchRegex, (match) {
final switchValue = _evaluate(match.group(1)!.trim(), context);
final switchBody = match.group(2)!;
// Process cases
var processedBody = switchBody;
var defaultContent = '';
// Extract default first
final defaultMatch = _defaultRegex.firstMatch(processedBody);
if (defaultMatch != null) {
defaultContent = defaultMatch.group(1)!;
processedBody = processedBody.replaceFirst(_defaultRegex, '');
}
// Process cases
processedBody = processedBody.replaceAllMapped(_caseRegex, (caseMatch) {
final caseValue = _evaluate(caseMatch.group(1)!.trim(), context);
final caseBody = caseMatch.group(2)!;
if (caseValue == switchValue) {
return caseBody;
}
return '';
});
// If no case matched, return default
if (processedBody.trim().isEmpty && defaultContent.isNotEmpty) {
return defaultContent;
}
return processedBody;
});
}