|
| 1 | +//.title |
| 2 | +// ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ |
| 3 | +// |
| 4 | +// 🇽🇾🇿 & Dev |
| 5 | +// |
| 6 | +// Copyright Ⓒ Robert Mollentze, xyzand.dev |
| 7 | +// |
| 8 | +// Licensing details can be found in the LICENSE file in the root directory. |
| 9 | +// |
| 10 | +// ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ |
| 11 | +//.title~ |
| 12 | + |
| 13 | +/// Replaces placeholders in a string with corresponding values from a provided |
| 14 | +/// map, supporting default values and custom delimiters. |
| 15 | +String replacePatterns( |
| 16 | + String input, |
| 17 | + Map data, { |
| 18 | + ReplacePatternsSettings settings = const ReplacePatternsSettings(), |
| 19 | +}) { |
| 20 | + var output = input; |
| 21 | + final regex = RegExp( |
| 22 | + '${RegExp.escape(settings.opening)}(.*?)${RegExp.escape(settings.closing)}', |
| 23 | + ); |
| 24 | + final matches = regex.allMatches(input); |
| 25 | + for (final match in matches) { |
| 26 | + final fullMatch = match.group(0)!; |
| 27 | + final keyWithDefault = match.group(1)!; |
| 28 | + final parts = keyWithDefault.split(settings.delimiter); |
| 29 | + final e0 = parts.elementAtOrNull(0); |
| 30 | + final e1 = parts.elementAtOrNull(1); |
| 31 | + final key = (e1 ?? e0)!; |
| 32 | + final defaultValue = e0 ?? key; |
| 33 | + final data1 = |
| 34 | + settings.caseSensitive ? data : data.map((k, v) => MapEntry(k.toString().toLowerCase(), v)); |
| 35 | + final key1 = settings.caseSensitive ? key : key.toLowerCase(); |
| 36 | + final suggestedReplacementValue = data1[key1]; |
| 37 | + final replacementValue = |
| 38 | + settings.callback?.call(key, suggestedReplacementValue, defaultValue) ?? |
| 39 | + suggestedReplacementValue?.toString() ?? |
| 40 | + defaultValue; |
| 41 | + output = output.replaceFirst(fullMatch, replacementValue); |
| 42 | + } |
| 43 | + |
| 44 | + return output; |
| 45 | +} |
| 46 | + |
| 47 | +// ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ |
| 48 | + |
| 49 | +extension ReplaceAllPatternsOnStringExtension on String { |
| 50 | + /// Replaces placeholders in this string with corresponding values from a |
| 51 | + /// provided map, supporting default values and custom delimiters. |
| 52 | + String replacePatterns( |
| 53 | + Map data, { |
| 54 | + ReplacePatternsSettings settings = const ReplacePatternsSettings(), |
| 55 | + }) { |
| 56 | + return _replacePatterns( |
| 57 | + this, |
| 58 | + data, |
| 59 | + settings: settings, |
| 60 | + ).toString(); |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +// ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ |
| 65 | + |
| 66 | +const _replacePatterns = replacePatterns; |
| 67 | + |
| 68 | +// ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ |
| 69 | + |
| 70 | +class ReplacePatternsSettings { |
| 71 | + // |
| 72 | + // |
| 73 | + // |
| 74 | + |
| 75 | + final String opening; |
| 76 | + final String closing; |
| 77 | + final String separator; |
| 78 | + final String delimiter; |
| 79 | + final bool caseSensitive; |
| 80 | + final String? Function( |
| 81 | + String key, |
| 82 | + dynamic suggestedReplacementValue, |
| 83 | + String defaultValue, |
| 84 | + )? callback; |
| 85 | + |
| 86 | + // |
| 87 | + // |
| 88 | + // |
| 89 | + |
| 90 | + const ReplacePatternsSettings({ |
| 91 | + this.opening = '<<<', |
| 92 | + this.closing = '>>>', |
| 93 | + this.separator = '.', |
| 94 | + this.delimiter = '||', |
| 95 | + this.caseSensitive = true, |
| 96 | + this.callback, |
| 97 | + }); |
| 98 | +} |
0 commit comments