mc_gallery/lib/features/core/data/extensions/string_extensions.dart

21 lines
672 B
Dart
Raw Normal View History

2023-01-01 12:04:22 +00:00
/// String extensions
extension StringExtensions on String {
/// Returns true if given word contains atleast all the characters in [targetChars], and `false` otherwise
///
/// Very efficient `O(n)` instead of naive `O(n*m)`
2022-12-26 18:39:47 +00:00
bool containsAllCharacters({
required String targetChars,
bool ignoreCase = true,
}) {
2023-01-01 12:04:22 +00:00
final characterSet = ignoreCase
? Set<String>.from(targetChars.toLowerCase().split(''))
: Set<String>.from(targetChars.split(''));
2022-12-26 18:39:47 +00:00
for (final testChar in ignoreCase ? toLowerCase().split('') : split('')) {
characterSet.remove(testChar);
if (characterSet.isEmpty) return true;
}
return false;
}
}