leaveOnly function
leave_only: Removes all characters except the given ones. Numbers and booleans are
converted to strings, objects and lists are left as they are.
Implementation
LivrRule leaveOnly(List<Object?> args, RuleContext context) {
final chars = stringArg(args, 0, 'leave_only');
// ASCII characters are looked up in a table, others in a set of code points
final asciiChars = Uint8List(128);
Set<int>? otherChars;
for (final char in chars.runes) {
if (char < 128) {
asciiChars[char] = 1;
} else {
(otherChars ??= <int>{}).add(char);
}
}
return (value, allValues) {
if (isNoValue(value) || !isPrimitive(value)) return RuleResult.valid;
return RuleResult.value(
_leaveOnly(livrToString(value), asciiChars, otherChars),
);
};
}