Skip to content

Commit baba96b

Browse files
author
M.safaeian
committed
refactor: complete documentation
1 parent 82dcca7 commit baba96b

File tree

9 files changed

+91
-9
lines changed

9 files changed

+91
-9
lines changed

common_utilities/.dart_tool/package_config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@
152152
"languageVersion": "2.15"
153153
}
154154
],
155-
"generated": "2022-02-02T09:25:10.719696Z",
155+
"generated": "2022-02-02T11:37:17.574453Z",
156156
"generator": "pub",
157157
"generatorVersion": "2.15.1"
158158
}

common_utilities/.packages

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#
44
# For more info see: https://dart.dev/go/dot-packages-deprecation
55
#
6-
# Generated by pub on 2022-02-02 12:55:10.704075.
6+
# Generated by pub on 2022-02-02 15:07:17.558859.
77
async:file:///D:/Programs/Flutter/flutter/.pub-cache/hosted/pub.dartlang.org/async-2.8.2/lib/
88
boolean_selector:file:///D:/Programs/Flutter/flutter/.pub-cache/hosted/pub.dartlang.org/boolean_selector-2.1.0/lib/
99
characters:file:///D:/Programs/Flutter/flutter/.pub-cache/hosted/pub.dartlang.org/characters-1.2.0/lib/

common_utilities/CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
## version 0.0.6
1+
## version 0.0.10
22
* Support null-safety
33
* Add wrap method
4+
* Add URL validation method
5+
* Add random string generator
46
* Complete documentation
57

68
## version 0.0.5

common_utilities/README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,23 @@ A Dart Common Utility Package . That contain useful functions for different clas
44

55
Includes a set of **String** , **Int** , **List** functions and so on. that make your work much faster and easier.
66

7-
**version 0.0.4 Includes only String utils**.
7+
**version 0.0.10 Includes only String utils**.
8+
9+
## Usage
10+
11+
add this line to your `pubspec.yaml` :
12+
13+
```yaml
14+
dependencies:
15+
commmon_utilities: ^0.0.10
16+
```
17+
18+
or enter this to your command line:
19+
20+
```yaml
21+
flutter pub add common_utilities
22+
```
23+
824

925
## String Utils
1026
Includes over 30 useful string functions such as :

common_utilities/example/.dart_tool/package_config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@
164164
"languageVersion": "2.15"
165165
}
166166
],
167-
"generated": "2022-02-02T11:02:56.180478Z",
167+
"generated": "2022-02-02T11:37:19.980152Z",
168168
"generator": "pub",
169169
"generatorVersion": "2.15.1"
170170
}

common_utilities/example/.packages

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#
44
# For more info see: https://dart.dev/go/dot-packages-deprecation
55
#
6-
# Generated by pub on 2022-02-02 14:32:56.180478.
6+
# Generated by pub on 2022-02-02 15:07:19.964534.
77
async:file:///D:/Programs/Flutter/flutter/.pub-cache/hosted/pub.dartlang.org/async-2.8.2/lib/
88
boolean_selector:file:///D:/Programs/Flutter/flutter/.pub-cache/hosted/pub.dartlang.org/boolean_selector-2.1.0/lib/
99
characters:file:///D:/Programs/Flutter/flutter/.pub-cache/hosted/pub.dartlang.org/characters-1.2.0/lib/

common_utilities/example/pubspec.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ packages:
4949
path: ".."
5050
relative: true
5151
source: path
52-
version: "0.0.6"
52+
version: "0.0.10"
5353
cupertino_icons:
5454
dependency: "direct main"
5555
description:

common_utilities/lib/src/string_utils.dart

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
import 'dart:math';
12
import 'package:intl/intl.dart';
2-
import 'dart:convert' show utf8, base64;
3+
import 'dart:convert' show base64, base64UrlEncode, utf8;
34

45
class CommonStringUtils {
56
static final String _exceptionTag = 'Exception on StringUtils : ';
@@ -8,10 +9,12 @@ class CommonStringUtils {
89

910
CommonStringUtils._();
1011

12+
/// Set a string value to [_mainStr] as the variable that should process.
1113
void construct(String mainStr) {
1214
this._mainStr = mainStr;
1315
}
1416

17+
/// make a sub-string of given string by deleting [startStr] and [endStr].
1518
String subStringBetween(String startStr, String endStr) {
1619
String result = '';
1720
try {
@@ -25,6 +28,7 @@ class CommonStringUtils {
2528
return result;
2629
}
2730

31+
/// make a [List<String>] of given string by deleting [startStr] and [endStr].
2832
List<String> subStringsBetween(String startStr, String endStr) {
2933
List<String> results = [];
3034
_mainStr.split('$endStr').forEach((element) {
@@ -36,6 +40,7 @@ class CommonStringUtils {
3640
return results;
3741
}
3842

43+
/// find [targetStr] in given string and return the sub-string after it.
3944
String subStringAfter(String targetStr, [bool lastIndex = false]) {
4045
String result = '';
4146
try {
@@ -51,6 +56,7 @@ class CommonStringUtils {
5156
return result;
5257
}
5358

59+
/// find [targetStr] in given string and return the sub-string before it.
5460
String subStringBefore(String targetStr, [bool lastIndex = false]) {
5561
String result = '';
5662
try {
@@ -63,35 +69,45 @@ class CommonStringUtils {
6369
return result;
6470
}
6571

72+
/// add [wordStr] in [index] of the given string.
6673
String insertAt(int index, String wordStr) {
6774
var selectStr = _mainStr[index];
6875
return _mainStr.replaceFirst('$selectStr', '$selectStr' + '$wordStr');
6976
}
7077

78+
/// remove the character in [index] of the given string.
7179
String removeAt(int index) {
7280
var selectStr = _mainStr[index];
7381
return _mainStr.replaceFirst('$selectStr', '');
7482
}
7583

84+
/// find the first [targetStr] in the given string and add the [word] to it.
85+
/// then return the result as string
7686
String insertAfter(String targetStr, String word) {
7787
return _mainStr.replaceFirst('$targetStr', '$targetStr' + '$word');
7888
}
7989

90+
/// find every [targetStr] in the given string and add the [word] to it.
91+
/// then return the result as string
8092
String insertAfterEvery(String targetStr, String word) {
8193
return _mainStr.replaceAll('$targetStr', '$targetStr' + '$word');
8294
}
8395

96+
/// find all the characters after [targetStr] and remove them from the given string.
8497
String removeAfter(String targetStr) {
8598
var tempTarget =
8699
_mainStr.substring(_mainStr.indexOf('$targetStr') + targetStr.length);
87100
return _mainStr.replaceFirst('$tempTarget', '');
88101
}
89102

103+
/// find all the characters before [targetStr] and remove them from the given string.
90104
String removeBefore(String targetStr) {
91105
var tempTarget = _mainStr.substring(0, _mainStr.indexOf('$targetStr'));
92106
return _mainStr.replaceFirst('$tempTarget', '');
93107
}
94108

109+
/// return true if the string is castable to [int] type,
110+
/// if it's not, return false.
95111
bool isNumericInt() {
96112
final validCharacters = RegExp(r'^[0-9]+$');
97113

@@ -102,13 +118,17 @@ class CommonStringUtils {
102118
}
103119
}
104120

121+
/// return true if the string is castable to [double] type,
122+
/// if it's not, return false.
105123
bool isNumericDouble() {
106124
if (_mainStr.isEmpty) {
107125
return false;
108126
}
109127
return double.tryParse(_mainStr) != null;
110128
}
111129

130+
/// return true if all the characters of the given string is alphabetic characters,
131+
/// if it's not, return false.
112132
bool isAlphabetic(bool withSpace) {
113133
final validCharacters =
114134
withSpace ? RegExp(r'^[a-zA-Z ]+$') : RegExp(r'^[a-zA-Z]+$');
@@ -120,6 +140,8 @@ class CommonStringUtils {
120140
}
121141
}
122142

143+
/// return true if all the characters of the given string is uppercase,
144+
/// if it's not, return false.
123145
bool isUpperCase() {
124146
final validCharacters = _mainStr.toUpperCase();
125147
if (_mainStr == validCharacters &&
@@ -130,6 +152,8 @@ class CommonStringUtils {
130152
}
131153
}
132154

155+
/// return true if all the characters of the given string is combination of alphabetic and numeric characters,
156+
/// if it's not, return false.
133157
bool isAlphaNumeric(bool withSpace) {
134158
final validCharacters =
135159
withSpace ? RegExp(r'^[a-zA-Z0-9 ]+$') : RegExp(r'^[a-zA-Z0-9]+$');
@@ -141,6 +165,8 @@ class CommonStringUtils {
141165
}
142166
}
143167

168+
/// return true if the given string is blank
169+
/// if it's not, return false.
144170
bool isBlank() {
145171
final validCharacters = RegExp(r'^[ ]+$');
146172

@@ -151,6 +177,8 @@ class CommonStringUtils {
151177
}
152178
}
153179

180+
/// return true if all the characters of the given string contains special characters,
181+
/// if it's not, return false.
154182
bool isContainSpecialChar() {
155183
final validCharacters = RegExp(r'^[a-zA-Z0-9 ]+$');
156184

@@ -161,6 +189,8 @@ class CommonStringUtils {
161189
}
162190
}
163191

192+
/// return true if all the characters of the given string is valid characters,
193+
/// if it's not, return false.
164194
bool isValidCharacters(RegExp validCharacters) {
165195
try {
166196
if (validCharacters.hasMatch(_mainStr)) {
@@ -174,6 +204,8 @@ class CommonStringUtils {
174204
}
175205
}
176206

207+
/// convert all the english numbers to persian in the given string,
208+
/// then return the result.
177209
String convertEnglishNumberToPersian() {
178210
const english = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
179211
const farsi = ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹'];
@@ -184,6 +216,8 @@ class CommonStringUtils {
184216
return _mainStr;
185217
}
186218

219+
/// convert all the persian numbers to english in the given string,
220+
/// then return the result.
187221
String convertPersianNumberToEnglish() {
188222
const english = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
189223
const farsi = ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹'];
@@ -194,6 +228,8 @@ class CommonStringUtils {
194228
return _mainStr;
195229
}
196230

231+
/// add comma character to the given string to make the money format,
232+
/// then return the result string.
197233
String convertToMoneyFormat() {
198234
final formatter = new NumberFormat("#,###", "en_US");
199235
if (isNumericDouble()) {
@@ -206,30 +242,37 @@ class CommonStringUtils {
206242
}
207243
}
208244

245+
/// return the characters count of the given string
209246
int countWords(String wordStr) {
210247
int wordCount = 0;
211248
wordCount = '$wordStr'.allMatches('$_mainStr').length;
212249
return wordCount;
213250
}
214251

252+
/// return the lines count of the given string
215253
int countLines() {
216254
int lineCount = 0;
217255
lineCount = '\n'.allMatches('$_mainStr').length;
218256
return lineCount + 1;
219257
}
220258

259+
/// reverse string from the end to the start
221260
String reverse() {
222261
return _mainStr.split('').reversed.join();
223262
}
224263

264+
/// reverse the words of the given string
225265
String reverseWords() {
226266
return _mainStr.split(' ').reversed.join(' ');
227267
}
228268

269+
/// remove the blank lines from the given string,
270+
/// the return the result string.
229271
String removeBlankLines() {
230272
return _mainStr.replaceAll(new RegExp(r'(?:[\t ]*(?:\r?\n|\r))+'), '\n');
231273
}
232274

275+
/// remove the lines that contains [wordStr] from the given string.
233276
String removeLinesThatContain(String wordStr) {
234277
var lines = _mainStr.split('\n');
235278
for (var lineItem in _mainStr.split('\n')) {
@@ -240,6 +283,8 @@ class CommonStringUtils {
240283
return lines.join('\n');
241284
}
242285

286+
/// return true if the given string is valid Email,
287+
/// if it's not, return false.
243288
bool isEmail() {
244289
var emailValidation = RegExp(
245290
r"^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,253}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,253}[a-zA-Z0-9])?)*$");
@@ -250,10 +295,14 @@ class CommonStringUtils {
250295
}
251296
}
252297

298+
/// convert the given string to [base64]
299+
/// then return the result string.
253300
String convertToBase64() {
254301
return base64.encode(utf8.encode(_mainStr));
255302
}
256303

304+
/// convert the given [base64] to string
305+
/// then return the result string.
257306
String convertBase64ToString() {
258307
return utf8.decode(base64.decode(_mainStr));
259308
}
@@ -262,7 +311,22 @@ class CommonStringUtils {
262311
return utf8.encode('$_mainStr');
263312
}
264313

314+
/// wrap the given string between the [wrapWith]
265315
String wrap(String wrapWith) {
266316
return '$wrapWith$_mainStr$wrapWith';
267317
}
318+
319+
/// return true if the given string is valid URL,
320+
/// if it's not, return false.
321+
bool isValidUrl() {
322+
if (_mainStr.isEmpty) return false;
323+
return Uri.tryParse(_mainStr)?.hasAbsolutePath ?? false;
324+
}
325+
326+
/// generate a secure random string
327+
String getRandomString(int len) {
328+
var random = Random.secure();
329+
var values = List<int>.generate(len, (i) => random.nextInt(255));
330+
return base64UrlEncode(values);
331+
}
268332
}

common_utilities/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: common_utilities
22
description: A Dart common utility package, currently supports string processing methods that makes your code cleaner.
3-
version: 0.0.6
3+
version: 0.0.10
44
homepage: https://github.com/m-r-davari/Flutter-Packages
55

66
environment:

0 commit comments

Comments
 (0)