Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions lib/string_extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2202,6 +2202,41 @@ extension MiscExtensions on String? {
return leetLetters.join();
}

/// Transforms the leet symbols of a string to their equivalent letters.
///
/// For example:
/// ```
/// th!s !s just @ t3st
/// ```
/// will produce:
/// ```
/// this is just a test
/// ```
String? get fromLeet {
List<String> stringWithSymbols = this!.split('');
List<String> letters = [];

for (int i = 0; i < stringWithSymbols.length; i++) {
String currentCharacter = stringWithSymbols[i].toLowerCase().trim();
if (currentCharacter.isEmpty) {
letters.add(" ");
}

if (StringHelpers.leetAlphabet.containsKey(currentCharacter)) {
letters.add(currentCharacter);
} else {
// The current character is a leet symbol - find it's equivalent letter.
StringHelpers.leetAlphabet.forEach((key, value) {
if (value.contains(currentCharacter)) {
letters.add(key);
}
});
}
}

return letters.join();
}

/// Checks if the `String` provided is a valid credit card number using Luhn Algorithm.
///
/// ### Example
Expand Down
9 changes: 9 additions & 0 deletions test/string_extensions_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1412,4 +1412,13 @@ void main() {
expect("HELLOworld!".isMixedCase(), equals(true));
expect("HelloWORLD!".isMixedCase(), equals(true));
});

test(
"Transforms a string with leet symbols to a string with their equivalent letters",
() {
expect("th!s !s just @ t3st".fromLeet, "this is just a test");
expect("this is just a test".fromLeet, "this is just a test");
expect("".fromLeet, "");
expect(" ".fromLeet, " ");
});
}