Line data Source code
1 : import 'package:get/src/regex/regex.dart';
2 :
3 : class GetUtils {
4 : /// Checks if data is null.
5 0 : static bool isNull(dynamic s) => s == null;
6 :
7 : /// Checks if data is null or blank (empty or only contains whitespace).
8 0 : static bool isNullOrBlank(dynamic s) {
9 0 : if (isNull(s)) return true;
10 0 : switch (s.runtimeType) {
11 0 : case String:
12 0 : case List:
13 0 : case Map:
14 0 : case Set:
15 0 : case Iterable:
16 0 : return s.isEmpty;
17 : break;
18 : default:
19 0 : return s.toString() == 'null' || s.toString().trim().isEmpty;
20 : }
21 : }
22 :
23 : /// Checks if string is int or double.
24 0 : static bool isNum(String s) {
25 0 : if (isNull(s)) return false;
26 0 : return num.tryParse(s) is num ?? false;
27 : }
28 :
29 : /// Checks if string consist only numeric.
30 : /// Numeric only doesnt accepting "." which double data type have
31 0 : static bool isNumericOnly(String s) =>
32 0 : RegexValidation.hasMatch(s, regex.numericOnly);
33 :
34 : /// Checks if string consist only Alphabet. (No Whitespace)
35 0 : static bool isAlphabetOnly(String s) =>
36 0 : RegexValidation.hasMatch(s, regex.alphabetOnly);
37 :
38 : /// Checks if string is boolean.
39 0 : static bool isBool(String s) {
40 0 : if (isNull(s)) return false;
41 0 : return (s == 'true' || s == 'false');
42 : }
43 :
44 : /// Checks if string is an vector file.
45 0 : static bool isVector(String s) => RegexValidation.hasMatch(s, regex.vector);
46 :
47 : /// Checks if string is an image file.
48 0 : static bool isImage(String s) => RegexValidation.hasMatch(s, regex.image);
49 :
50 : /// Checks if string is an audio file.
51 0 : static bool isAudio(String s) => RegexValidation.hasMatch(s, regex.audio);
52 :
53 : /// Checks if string is an video file.
54 0 : static bool isVideo(String s) => RegexValidation.hasMatch(s, regex.video);
55 :
56 : /// Checks if string is an txt file.
57 0 : static bool isTxt(String s) => RegexValidation.hasMatch(s, regex.txt);
58 :
59 : /// Checks if string is an Doc file.
60 0 : static bool isDocument(String s) => RegexValidation.hasMatch(s, regex.doc);
61 :
62 : /// Checks if string is an Excel file.
63 0 : static bool isExcel(String s) => RegexValidation.hasMatch(s, regex.excel);
64 :
65 : /// Checks if string is an PPT file.
66 0 : static bool isPPT(String s) => RegexValidation.hasMatch(s, regex.ppt);
67 :
68 : /// Checks if string is an APK file.
69 0 : static bool isAPK(String s) => RegexValidation.hasMatch(s, regex.apk);
70 :
71 : /// Checks if string is an video file.
72 0 : static bool isPDF(String s) => RegexValidation.hasMatch(s, regex.pdf);
73 :
74 : /// Checks if string is an HTML file.
75 0 : static bool isHTML(String s) => RegexValidation.hasMatch(s, regex.html);
76 :
77 : /// Checks if string is URL.
78 0 : static bool isURL(String s) => RegexValidation.hasMatch(s, regex.url);
79 :
80 : /// Checks if string is email.
81 0 : static bool isEmail(String s) => RegexValidation.hasMatch(s, regex.email);
82 :
83 : /// Checks if string is phone number.
84 0 : static bool isPhoneNumber(String s) =>
85 0 : RegexValidation.hasMatch(s, regex.phone);
86 :
87 : /// Checks if string is DateTime (UTC or Iso8601).
88 0 : static bool isDateTime(String s) =>
89 0 : RegexValidation.hasMatch(s, regex.basicDateTime);
90 :
91 : /// Checks if string is MD5 hash.
92 0 : static bool isMD5(String s) => RegexValidation.hasMatch(s, regex.md5);
93 :
94 : /// Checks if string is SHA1 hash.
95 0 : static bool isSHA1(String s) => RegexValidation.hasMatch(s, regex.sha1);
96 :
97 : /// Checks if string is SHA256 hash.
98 0 : static bool isSHA256(String s) => RegexValidation.hasMatch(s, regex.sha256);
99 :
100 : /// Checks if string is ISBN 10 or 13.
101 0 : static bool isISBN(String s) => RegexValidation.hasMatch(s, regex.isbn);
102 :
103 : /// Checks if string is SSN (Social Security Number).
104 0 : static bool isSSN(String s) => RegexValidation.hasMatch(s, regex.ssn);
105 :
106 : /// Checks if string is binary.
107 0 : static bool isBinary(String s) => RegexValidation.hasMatch(s, regex.binary);
108 :
109 : /// Checks if string is IPv4.
110 0 : static bool isIPv4(String s) => RegexValidation.hasMatch(s, regex.ipv4);
111 :
112 : /// Checks if string is IPv6.
113 0 : static bool isIPv6(String s) => RegexValidation.hasMatch(s, regex.ipv6);
114 :
115 : /// Checks if string is hexadecimal.
116 : /// Example: HexColor => #12F
117 0 : static bool isHexadecimal(String s) =>
118 0 : RegexValidation.hasMatch(s, regex.hexadecimal);
119 :
120 : /// Checks if string is Palindrom.
121 0 : static bool isPalindrom(String s) {
122 : bool isPalindrom = true;
123 0 : for (var i = 0; i < s.length; i++) {
124 0 : if (s[i] != s[s.length - i - 1]) isPalindrom = false;
125 : }
126 : return isPalindrom;
127 : }
128 :
129 : /// Checks if all data have same value.
130 : /// Example: 111111 -> true, wwwww -> true, [1,1,1,1] -> true
131 0 : static bool isOneAKind(dynamic s) {
132 0 : if ((s is String || s is List) && !isNullOrBlank(s)) {
133 0 : var first = s[0];
134 : var isOneAKind = true;
135 0 : for (var i = 0; i < s.length; i++) {
136 0 : if (s[i] != first) isOneAKind = false;
137 : }
138 : return isOneAKind;
139 : }
140 0 : if (s is int) {
141 0 : String value = s.toString();
142 0 : var first = value[0];
143 : var isOneAKind = true;
144 0 : for (var i = 0; i < value.length; i++) {
145 0 : if (value[i] != first) isOneAKind = false;
146 : }
147 : return isOneAKind;
148 : }
149 : return false;
150 : }
151 :
152 : /// Checks if string is Passport No.
153 0 : static bool isPassport(String s) =>
154 0 : RegexValidation.hasMatch(s, regex.passport);
155 :
156 : /// Checks if string is Currency.
157 0 : static bool isCurrency(String s) =>
158 0 : RegexValidation.hasMatch(s, regex.currency);
159 :
160 : /// Checks if length of data is LOWER than maxLength.
161 0 : static bool isLengthLowerThan(dynamic s, int maxLength) {
162 0 : if (isNull(s)) return (maxLength <= 0) ? true : false;
163 0 : switch (s.runtimeType) {
164 0 : case String:
165 0 : case List:
166 0 : case Map:
167 0 : case Set:
168 0 : case Iterable:
169 0 : return s.length < maxLength;
170 : break;
171 0 : case int:
172 0 : return s.toString().length < maxLength;
173 : break;
174 0 : case double:
175 0 : return s.toString().replaceAll('.', '').length < maxLength;
176 : break;
177 : default:
178 : return false;
179 : }
180 : }
181 :
182 : /// Checks if length of data is GREATER than maxLength.
183 0 : static bool isLengthGreaterThan(dynamic s, int maxLength) {
184 0 : if (isNull(s)) return false;
185 0 : switch (s.runtimeType) {
186 0 : case String:
187 0 : case List:
188 0 : case Map:
189 0 : case Set:
190 0 : case Iterable:
191 0 : return s.length > maxLength;
192 : break;
193 0 : case int:
194 0 : return s.toString().length > maxLength;
195 : break;
196 0 : case double:
197 0 : return s.toString().replaceAll('.', '').length > maxLength;
198 : break;
199 : default:
200 : return false;
201 : }
202 : }
203 :
204 : /// Checks if length of data is GREATER OR EQUAL to maxLength.
205 0 : static bool isLengthGreaterOrEqual(dynamic s, int maxLength) {
206 0 : if (isNull(s)) return false;
207 0 : switch (s.runtimeType) {
208 0 : case String:
209 0 : case List:
210 0 : case Map:
211 0 : case Set:
212 0 : case Iterable:
213 0 : return s.length >= maxLength;
214 : break;
215 0 : case int:
216 0 : return s.toString().length >= maxLength;
217 : break;
218 0 : case double:
219 0 : return s.toString().replaceAll('.', '').length >= maxLength;
220 : break;
221 : default:
222 : return false;
223 : }
224 : }
225 :
226 : /// Checks if length of data is LOWER OR EQUAL to maxLength.
227 0 : static bool isLengthLowerOrEqual(dynamic s, int maxLength) {
228 0 : if (isNull(s)) return false;
229 0 : switch (s.runtimeType) {
230 0 : case String:
231 0 : case List:
232 0 : case Map:
233 0 : case Set:
234 0 : case Iterable:
235 0 : return s.length <= maxLength;
236 : break;
237 0 : case int:
238 0 : return s.toString().length <= maxLength;
239 : break;
240 0 : case double:
241 0 : return s.toString().replaceAll('.', '').length <= maxLength;
242 : default:
243 : return false;
244 : }
245 : }
246 :
247 : /// Checks if length of data is EQUAL to maxLength.
248 0 : static bool isLengthEqualTo(dynamic s, int maxLength) {
249 0 : if (isNull(s)) return false;
250 0 : switch (s.runtimeType) {
251 0 : case String:
252 0 : case List:
253 0 : case Map:
254 0 : case Set:
255 0 : case Iterable:
256 0 : return s.length == maxLength;
257 : break;
258 0 : case int:
259 0 : return s.toString().length == maxLength;
260 : break;
261 0 : case double:
262 0 : return s.toString().replaceAll('.', '').length == maxLength;
263 : break;
264 : default:
265 : return false;
266 : }
267 : }
268 :
269 : /// Checks if length of data is BETWEEN minLength to maxLength.
270 0 : static bool isLengthBetween(dynamic s, int minLength, int maxLength) {
271 0 : if (isNull(s)) return false;
272 0 : return isLengthGreaterOrEqual(s, minLength) &&
273 0 : isLengthLowerOrEqual(s, maxLength);
274 : }
275 :
276 : /// Checks if a contains b (Treating or interpreting upper- and lowercase letters as being the same).
277 0 : static bool isCaseInsensitiveContains(String a, String b) =>
278 0 : a.toLowerCase().contains(b.toLowerCase());
279 :
280 : /// Checks if a contains b or b contains a (Treating or interpreting upper- and lowercase letters as being the same).
281 0 : static bool isCaseInsensitiveContainsAny(String a, String b) {
282 0 : String lowA = a.toLowerCase();
283 0 : String lowB = b.toLowerCase();
284 0 : return lowA.contains(lowB) || lowB.contains(lowA);
285 : }
286 :
287 : /// Checks if num a LOWER than num b.
288 0 : static bool isLowerThan(num a, num b) => a < b;
289 :
290 : /// Checks if num a GREATER than num b.
291 0 : static bool isGreaterThan(num a, num b) => a > b;
292 :
293 : /// Checks if num a EQUAL than num b.
294 0 : static bool isEqual(num a, num b) => a == b;
295 :
296 : /// Capitalize each word inside string
297 : /// Example: your name => Your Name, your name => Your name
298 : ///
299 : /// If First Only is `true`, the only letter get uppercase is the first letter
300 0 : static String capitalize(String s, {bool firstOnly = false}) {
301 0 : if (isNullOrBlank(s)) return null;
302 0 : if (firstOnly) return capitalizeFirst(s);
303 :
304 0 : List lst = s.split(' ');
305 : String newStr = '';
306 0 : for (var s in lst) newStr += capitalizeFirst(s);
307 : return newStr;
308 : }
309 :
310 : /// Uppercase first letter inside string and let the others lowercase
311 : /// Example: your name => Your name
312 0 : static String capitalizeFirst(String s) {
313 0 : if (isNullOrBlank(s)) return null;
314 0 : return s[0].toUpperCase() + s.substring(1).toLowerCase();
315 : }
316 :
317 : /// Remove all whitespace inside string
318 : /// Example: your name => yourname
319 0 : static String removeAllWhitespace(String s) {
320 0 : if (isNullOrBlank(s)) return null;
321 0 : return s.replaceAll(' ', '');
322 : }
323 :
324 : /// Camelcase string
325 : /// Example: your name => yourName
326 0 : static String camelCase(String s) {
327 0 : if (isNullOrBlank(s)) return null;
328 0 : return s[0].toLowerCase() + removeAllWhitespace(capitalize(s)).substring(1);
329 : }
330 :
331 : /// Extract numeric value of string
332 : /// Example: OTP 12312 27/04/2020 => 1231227042020ß
333 : /// If firstword only is true, then the example return is "12312" (first found numeric word)
334 0 : static String numericOnly(String s, {bool firstWordOnly = false}) {
335 : String numericOnlyStr = '';
336 0 : for (var i = 0; i < s.length; i++) {
337 0 : if (isNumericOnly(s[i])) numericOnlyStr += s[i];
338 0 : if (firstWordOnly && numericOnlyStr.isNotEmpty && s[i] == " ") break;
339 : }
340 : return numericOnlyStr;
341 : }
342 :
343 0 : static Regex regex = Regex();
344 : }
345 :
346 : class RegexValidation {
347 : /// Returns whether the pattern has a match in the string [input].
348 0 : static bool hasMatch(String s, Pattern p) =>
349 0 : (s == null) ? false : RegExp(p).hasMatch(s);
350 : }
|