@@ -2,67 +2,77 @@ package com.eldhopj.kotlin_extensions
2
2
3
3
import com.eldhopj.kotlin_extensions.utils.Utils.EMAIL_ADDRESS
4
4
import com.eldhopj.kotlin_extensions.utils.Utils.NAME_PATTERN_REGEX
5
+ import com.eldhopj.kotlin_extensions.utils.Utils.PHONE_REGEX
5
6
import com.eldhopj.kotlin_extensions.utils.Utils.convertToHourMinute
6
7
import com.eldhopj.kotlin_extensions.utils.Utils.convertToHourMinuteSeconds
7
8
import java.util.Locale
9
+ import java.util.regex.Pattern
8
10
9
11
/* *
10
12
* Checks whether the email is valid or not
11
- * @param regEx Regular expression value to match the text for the email format.
13
+ *
14
+ * @return true-> if valid, else false
12
15
*/
13
- fun String?.isValidEmail (regEx : Regex = EMAIL_ADDRESS .toRegex()): Boolean =
14
- this != null && this .matches(regEx)
16
+ fun String.isValidEmail (): Boolean = this .checkRegex(EMAIL_ADDRESS )
15
17
16
18
/* *
17
19
* Checks whether the user name is valid name or not.
18
20
*
19
21
* @return true-> if valid, else false
20
22
*/
21
- fun String?.isValidName (pattern : Regex = Regex (NAME_PATTERN_REGEX )): Boolean {
22
- return this != null &&
23
- this .trim().isNotEmpty() &&
24
- matches(pattern)
25
- }
23
+ fun String.isValidName (): Boolean = this .checkRegex(NAME_PATTERN_REGEX )
24
+
25
+ /* *
26
+ * Extension method to check if String is Phone Number.
27
+ */
28
+ fun String.isValidPhoneNumber (): Boolean = this .checkRegex(PHONE_REGEX )
26
29
27
30
/* *
28
31
* Capitalize each word
29
32
*
30
33
* eg: hello world -> Hello World
31
34
*
32
35
*/
33
- val String? .capitalizeEachWord
34
- get() = this ?.lowercase(Locale .getDefault())?.split(" " )?.joinToString(" " ) { word ->
36
+ fun String?.getCapitalizeEachWord () =
37
+ this ?.lowercase(Locale .getDefault())?.split(" " )?.joinToString(" " ) { word ->
35
38
if (word.length <= 1 ) word // if the word is a single character then no need to capitalize.
36
39
else
37
40
word.replaceFirstChar {
38
41
if (it.isLowerCase()) it.titlecase(Locale .getDefault())
39
42
else it.toString()
40
43
}
41
- }?.trimEnd()?. trim()
44
+ }?.trim()
42
45
43
46
/* *
44
47
* Converts the seconds to Hours, Minutes & Seconds pattern.
45
48
*
46
49
* E.g., 1h 2m 30s
47
50
*
48
- * @param pattern Default Pattern is %dh %dm %ds.
51
+ * @param pattern in which pattern we have to convert. Default Pattern is %dh %dm %ds.
49
52
*
50
53
* @return String in Hours, Minutes & Seconds pattern
51
54
*/
52
- fun String?.toHourMinuteSeconds (pattern : String = "%dh %dm %ds"): String {
53
- if (this == null ) return " "
54
- return convertToHourMinuteSeconds(this , pattern)
55
- }
55
+ fun String?.toHourMinuteSeconds (pattern : String = "%dh %dm %ds"): String =
56
+ if (this == null ) " " else convertToHourMinuteSeconds(this , pattern)
56
57
57
58
/* *
58
59
* Converts the seconds to Hours, Minutes pattern.
59
60
*
60
61
* E.g., 1h 2m.
61
62
*
62
- * @param pattern Default Pattern is %dhr %dmin.
63
+ * @param pattern in which pattern we have to convert. Default Pattern is %dhr %dmin.
63
64
* @return String in Hours, Minutes pattern
64
65
*/
65
- fun String?.toHourMinute (pattern : String = "%dh %dm"): String {
66
- if (this == null ) return " "
67
- return convertToHourMinute(this , pattern)
66
+ fun String?.toHourMinute (pattern : String = "%dh %dm"): String =
67
+ if (this == null ) " " else convertToHourMinute(this , pattern)
68
+
69
+ /* *
70
+ * Check regex
71
+ *
72
+ * @param pattern Regex pattern
73
+ * @return @return true-> if valid, else false
74
+ */
75
+ fun String.checkRegex (pattern : Pattern ): Boolean {
76
+ val regex = pattern.toRegex()
77
+ return this .isNotBlank() && this .matches(regex)
68
78
}
0 commit comments