interpolate static method
interpolates value of the string, e.g.:
final helloWorldString = VoyagerUtils.interpolate("Hello %{myField}!", {"myField" : "world" })
print(helloWorldString);
Will evaluate to "Hello world!"
Implementation
static String? interpolate(String format, Map<String, dynamic> values) {
if (isNullOrBlank(format) || !format.contains(_VARIABLE_PREFIX))
return format;
var convFormat = format;
final keys = values.keys.iterator;
final valueList = <String>[];
var currentPos = 1;
while (keys.moveNext()) {
final key = keys.current,
formatKey = "$_VARIABLE_PREFIX$key$_VARIABLE_SUFFIX",
formatPos = "%$currentPos\$s";
var index = 0;
var replaced = false;
while ((index = convFormat.indexOf(formatKey, index)) != -1) {
convFormat =
convFormat.replaceRange(index, index + formatKey.length, formatPos);
index += formatPos.length;
replaced = true;
}
if (replaced) {
valueList.add(values[key]);
++currentPos;
}
}
try {
return sprintf(convFormat, valueList);
} catch (e) {
return null;
}
}