Skip to content

Commit 5f5acf6

Browse files
committed
🔧 Enhance string extension methods with improved URL validation, pattern matching, and leet transformation logic. Update documentation for clarity and consistency. Add tests for new functionality and edge cases.
1 parent b043d8f commit 5f5acf6

3 files changed

Lines changed: 120 additions & 135 deletions

File tree

‎lib/src/nullable_string_extensions.dart‎

Lines changed: 46 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -326,15 +326,15 @@ extension MiscExtensionsNullable on String? {
326326
}
327327
}
328328

329-
/// Checks whether the `String` is a valid URL.
329+
/// Checks whether the `String` is a valid URL with a supported scheme.
330330
/// ### Example 1
331331
/// ```dart
332332
/// String foo = 'foo.1com';
333333
/// bool isUrl = foo.isUrl; // returns false
334334
/// ```
335335
/// ### Example 2
336336
/// ```dart
337-
/// String foo = 'google.com';
337+
/// String foo = 'https://google.com';
338338
/// bool isUrl = foo.isUrl; // returns true
339339
/// ```
340340
bool get isUrl {
@@ -967,7 +967,7 @@ extension MiscExtensionsNullable on String? {
967967
if (this.isBlank) {
968968
return this;
969969
}
970-
if (index > this!.length) {
970+
if (index >= this!.length) {
971971
return this;
972972
}
973973
if (index < 0) {
@@ -1338,7 +1338,7 @@ extension MiscExtensionsNullable on String? {
13381338
return this;
13391339
}
13401340

1341-
if (index > this!.length) {
1341+
if (index >= this!.length) {
13421342
return null;
13431343
}
13441344
if (index < 0) {
@@ -1900,7 +1900,7 @@ extension MiscExtensionsNullable on String? {
19001900
return "${this ?? ''}";
19011901
}
19021902
if (resolvedBefore.isCloseWrapChar()) {
1903-
resolvedBefore = resolvedBefore.getOppositeChar() ?? resolvedBefore;
1903+
resolvedBefore = resolvedBefore.getOppositeChar();
19041904
}
19051905
resolvedAfter = resolvedBefore.getOppositeChar();
19061906
}
@@ -2056,22 +2056,16 @@ extension MiscExtensionsNullable on String? {
20562056
return this;
20572057
}
20582058

2059-
if (!this!.contains(pattern)) {
2060-
return '';
2061-
}
2062-
2063-
List<String> patternWords = pattern.split(' ');
2064-
2065-
if (patternWords.isEmpty) {
2059+
if (pattern.isEmpty) {
20662060
return '';
20672061
}
2068-
int indexOfLastPatternWord = this!.indexOf(patternWords.last);
20692062

2070-
if (patternWords.last.length == 0) {
2063+
int index = this!.indexOf(pattern);
2064+
if (index == -1) {
20712065
return '';
20722066
}
20732067

2074-
return this!.substring(0, indexOfLastPatternWord);
2068+
return this!.substring(0, index);
20752069
}
20762070

20772071
/// Removes everything in the `String` before the match of the [pattern].
@@ -2087,25 +2081,17 @@ extension MiscExtensionsNullable on String? {
20872081
return this;
20882082
}
20892083

2090-
if (!this!.contains(pattern)) {
2091-
return '';
2092-
}
2093-
2094-
List<String> patternWords = pattern.split(' ');
2095-
2096-
if (patternWords.isEmpty) {
2084+
if (pattern.isEmpty) {
20972085
return '';
20982086
}
2099-
int indexOfFirstPatternWord = this!.indexOf(patternWords.first);
21002087

2101-
if (patternWords.last.length == 0) {
2088+
int index = this!.indexOf(pattern);
2089+
if (index == -1) {
21022090
return '';
21032091
}
21042092

2105-
return this!.substring(
2106-
indexOfFirstPatternWord + 1,
2107-
this!.length,
2108-
);
2093+
final startIndex = pattern.length == 1 ? index + pattern.length : index;
2094+
return this!.substring(startIndex);
21092095
}
21102096

21112097
/// Adds a `String` after the first match of the [pattern]. The [pattern] should not be `null`.
@@ -2123,24 +2109,19 @@ extension MiscExtensionsNullable on String? {
21232109
return this;
21242110
}
21252111

2126-
if (!this!.contains(pattern)) {
2127-
return this;
2128-
}
2129-
2130-
List<String> patternWords = pattern.split(' ');
2131-
2132-
if (patternWords.isEmpty) {
2112+
if (pattern.isEmpty) {
21332113
return '';
21342114
}
2135-
int indexOfLastPatternWord = this!.indexOf(patternWords.last);
21362115

2137-
if (patternWords.last.length == 0) {
2138-
return '';
2116+
int index = this!.indexOf(pattern);
2117+
if (index == -1) {
2118+
return this;
21392119
}
21402120

2141-
return this!.substring(0, indexOfLastPatternWord + 1) +
2121+
final insertionIndex = index + pattern.length;
2122+
return this!.substring(0, insertionIndex) +
21422123
addition +
2143-
this!.substring(indexOfLastPatternWord + 1, this!.length);
2124+
this!.substring(insertionIndex, this!.length);
21442125
}
21452126

21462127
/// Adds a `String` before the first match of the [pattern]. The [pattern] should not be `null`.
@@ -2157,27 +2138,18 @@ extension MiscExtensionsNullable on String? {
21572138
return this;
21582139
}
21592140

2160-
if (!this!.contains(pattern)) {
2161-
return this;
2162-
}
2163-
2164-
List<String> patternWords = pattern.split(' ');
2165-
2166-
if (patternWords.isEmpty) {
2141+
if (pattern.isEmpty) {
21672142
return '';
21682143
}
2169-
int indexOfFirstPatternWord = this!.indexOf(patternWords.first);
21702144

2171-
if (patternWords.last.length == 0) {
2172-
return '';
2145+
int index = this!.indexOf(pattern);
2146+
if (index == -1) {
2147+
return this;
21732148
}
21742149

2175-
return this!.substring(0, indexOfFirstPatternWord) +
2150+
return this!.substring(0, index) +
21762151
adition +
2177-
this!.substring(
2178-
indexOfFirstPatternWord,
2179-
this!.length,
2180-
);
2152+
this!.substring(index, this!.length);
21812153
}
21822154

21832155
/// Checks if the `String` matches **ANY** of the given [patterns].
@@ -2277,14 +2249,18 @@ extension MiscExtensionsNullable on String? {
22772249
return this;
22782250
}
22792251
final letters = this!.split('');
2280-
2281-
final leetLetters = [];
2282-
letters.forEach((e) {
2283-
final count = StringHelpers.leetAlphabet[e].length;
2284-
final random = Random().nextInt(count);
2285-
print(StringHelpers.leetAlphabet[e][random]);
2286-
leetLetters.add(StringHelpers.leetAlphabet[e][random]);
2287-
});
2252+
final random = Random();
2253+
final leetLetters = <String>[];
2254+
2255+
for (final letter in letters) {
2256+
final replacements = StringHelpers.leetAlphabet[letter.toLowerCase()];
2257+
if (replacements is List && replacements.isNotEmpty) {
2258+
leetLetters
2259+
.add(replacements[random.nextInt(replacements.length)].toString());
2260+
} else {
2261+
leetLetters.add(letter);
2262+
}
2263+
}
22882264

22892265
return leetLetters.join();
22902266
}
@@ -2311,14 +2287,18 @@ extension MiscExtensionsNullable on String? {
23112287
int sum = 0;
23122288
bool alternate = false;
23132289
for (int i = trimmed.length - 1; i >= 0; i--) {
2314-
List<String> nx = trimmed.toArray;
2315-
int n = int.parse(nx[i]);
2290+
final codeUnit = trimmed.codeUnitAt(i);
2291+
if (codeUnit < 48 || codeUnit > 57) {
2292+
return false;
2293+
}
2294+
2295+
int n = codeUnit - 48;
23162296

23172297
if (alternate) {
23182298
n *= 2;
23192299

23202300
if (n > 9) {
2321-
n = (n % 10) + 1;
2301+
n -= 9;
23222302
}
23232303
}
23242304
sum += n;

0 commit comments

Comments
 (0)