From b22fe3cf517cc561e6641ae2d7c607d6c1043354 Mon Sep 17 00:00:00 2001 From: Ioannis Brant-Ioannidis Date: Sun, 26 Mar 2023 18:56:18 +0400 Subject: [PATCH 1/2] Leet String to Normal String - Second attempt to address #23 Based on the feedback received on #36 have seen that there is current support for leet symbols as well as a function that transforms a normal string to a leet string. This is a second attempt to address #23 by implementing a method that transforms a string with leet symbols to a string that will contain the equivalent letters. --- lib/string_extensions.dart | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/lib/string_extensions.dart b/lib/string_extensions.dart index b76b53f..55351dc 100644 --- a/lib/string_extensions.dart +++ b/lib/string_extensions.dart @@ -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 stringWithSymbols = this!.split(''); + List 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 From 4a5b5a945f7d93dbbd3879159142926d77abc57c Mon Sep 17 00:00:00 2001 From: Ioannis Brant-Ioannidis Date: Sun, 26 Mar 2023 18:56:36 +0400 Subject: [PATCH 2/2] Adding some unit tests for fromLeet --- test/string_extensions_test.dart | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/test/string_extensions_test.dart b/test/string_extensions_test.dart index 865cba7..d71c744 100644 --- a/test/string_extensions_test.dart +++ b/test/string_extensions_test.dart @@ -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, " "); + }); }