Skip to content

Commit bc766de

Browse files
authored
Merge pull request #1 from Eldhopj/dev/eldhopj/new_extensions_and_improvements
New extensions and improvements
2 parents 56b4d98 + e6bcbf6 commit bc766de

File tree

7 files changed

+147
-72
lines changed

7 files changed

+147
-72
lines changed

Kotlin_extensions/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ afterEvaluate {
1515
release(MavenPublication) {
1616
groupId = 'com.github.Eldhopj'
1717
artifactId = 'kotlin-extension'
18-
version = '1.0'
18+
version = '1.1'
1919

2020
from components.java
2121
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.eldhopj.kotlin_extensions
2+
3+
import java.io.File
4+
5+
private const val KILO_BYTE_SIZE = 1024F
6+
7+
/**
8+
* Returns the file size in MB.
9+
*
10+
* @return
11+
*/
12+
fun File?.getSizeInMB(): Float =
13+
if (this == null) 0.00f else "%.2f".format(File(this.absolutePath).length() / (KILO_BYTE_SIZE * KILO_BYTE_SIZE))
14+
.toFloat()
15+

Kotlin_extensions/src/main/java/com/eldhopj/kotlin_extensions/NumberExtension.kt

+27-22
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import java.text.DecimalFormat
66

77
/**
88
* Returns the string representation of the value using [DecimalFormat]
9+
* eg: 10.02542 -> 10.03
910
*
1011
* @param pattern pattern conversion pattern for [DecimalFormat]
1112
* @param roundingMode Specifies the rounding behavior for operations whose results cannot be
@@ -25,6 +26,7 @@ fun Double?.format(
2526

2627
/**
2728
* Returns the string representation of the value using [DecimalFormat]
29+
* eg: 10.02542 -> 10.03
2830
*
2931
* @param pattern pattern conversion pattern for [DecimalFormat]
3032
* @param roundingMode Specifies the rounding behavior for operations whose results cannot be
@@ -50,18 +52,17 @@ fun Float?.format(
5052
*
5153
* @return
5254
*/
53-
val Long.shortenString: String
54-
get() {
55-
var value = this
56-
val arr = arrayOf("", "K", "M", "B", "T", "P", "E")
57-
var index = 0
58-
while (value / THOUSAND >= 1) {
59-
value /= THOUSAND
60-
index++
61-
}
62-
val decimalFormat = DecimalFormat("#.##")
63-
return String.format("%s %s", decimalFormat.format(value), arr[index])
55+
fun Long.getShortenString(): String {
56+
var value = this
57+
val arr = arrayOf("", "K", "M", "B", "T", "P", "E")
58+
var index = 0
59+
while (value / THOUSAND >= 1) {
60+
value /= THOUSAND
61+
index++
6462
}
63+
val decimalFormat = DecimalFormat("#.##")
64+
return String.format("%s %s", decimalFormat.format(value), arr[index])
65+
}
6566

6667
/**
6768
* Format number into Short values
@@ -71,15 +72,19 @@ val Long.shortenString: String
7172
*
7273
* @return
7374
*/
74-
val Int.shortenString: String
75-
get() {
76-
var value = this
77-
val arr = arrayOf("", "K", "M", "B", "T", "P", "E")
78-
var index = 0
79-
while (value / THOUSAND >= 1) {
80-
value /= THOUSAND
81-
index++
82-
}
83-
val decimalFormat = DecimalFormat("#.##")
84-
return String.format("%s %s", decimalFormat.format(value), arr[index])
75+
fun Int.getShortenString(): String {
76+
var value = this
77+
val arr = arrayOf("", "K", "M", "B", "T", "P", "E")
78+
var index = 0
79+
while (value / THOUSAND >= 1) {
80+
value /= THOUSAND
81+
index++
8582
}
83+
val decimalFormat = DecimalFormat("#.##")
84+
return String.format("%s %s", decimalFormat.format(value), arr[index])
85+
}
86+
87+
/**
88+
* Returns true if this number is null or zero (0)
89+
*/
90+
fun Number?.isNullOrZero(): Boolean = this == null || this == 0

Kotlin_extensions/src/main/java/com/eldhopj/kotlin_extensions/StringExtensions.kt

+30-20
Original file line numberDiff line numberDiff line change
@@ -2,67 +2,77 @@ package com.eldhopj.kotlin_extensions
22

33
import com.eldhopj.kotlin_extensions.utils.Utils.EMAIL_ADDRESS
44
import com.eldhopj.kotlin_extensions.utils.Utils.NAME_PATTERN_REGEX
5+
import com.eldhopj.kotlin_extensions.utils.Utils.PHONE_REGEX
56
import com.eldhopj.kotlin_extensions.utils.Utils.convertToHourMinute
67
import com.eldhopj.kotlin_extensions.utils.Utils.convertToHourMinuteSeconds
78
import java.util.Locale
9+
import java.util.regex.Pattern
810

911
/**
1012
* 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
1215
*/
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)
1517

1618
/**
1719
* Checks whether the user name is valid name or not.
1820
*
1921
* @return true-> if valid, else false
2022
*/
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)
2629

2730
/**
2831
* Capitalize each word
2932
*
3033
* eg: hello world -> Hello World
3134
*
3235
*/
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 ->
3538
if (word.length <= 1) word // if the word is a single character then no need to capitalize.
3639
else
3740
word.replaceFirstChar {
3841
if (it.isLowerCase()) it.titlecase(Locale.getDefault())
3942
else it.toString()
4043
}
41-
}?.trimEnd()?.trim()
44+
}?.trim()
4245

4346
/**
4447
* Converts the seconds to Hours, Minutes & Seconds pattern.
4548
*
4649
* E.g., 1h 2m 30s
4750
*
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.
4952
*
5053
* @return String in Hours, Minutes & Seconds pattern
5154
*/
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)
5657

5758
/**
5859
* Converts the seconds to Hours, Minutes pattern.
5960
*
6061
* E.g., 1h 2m.
6162
*
62-
* @param pattern Default Pattern is %dhr %dmin.
63+
* @param pattern in which pattern we have to convert. Default Pattern is %dhr %dmin.
6364
* @return String in Hours, Minutes pattern
6465
*/
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)
6878
}

Kotlin_extensions/src/main/java/com/eldhopj/kotlin_extensions/utils/Utils.kt

+40-14
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,7 @@ import java.util.regex.Pattern
99
*
1010
* @constructor Create empty Utils
1111
*/
12-
object Utils {
13-
/**
14-
* Name Pattern Regex
15-
*/
16-
internal const val NAME_PATTERN_REGEX = "^[\\p{L} .'-]+$"
12+
internal object Utils {
1713

1814
/**
1915
* Thousand
@@ -33,15 +29,45 @@ object Utils {
3329
/**
3430
* Email Address
3531
*/
36-
internal val EMAIL_ADDRESS = Pattern.compile(
37-
"[a-zA-Z0-9\\+\\.\\_\\%\\-\\+]{1,256}" +
38-
"\\@" +
39-
"[a-zA-Z0-9][a-zA-Z0-9\\-]{0,64}" +
40-
"(" +
41-
"\\." +
42-
"[a-zA-Z0-9][a-zA-Z0-9\\-]{0,25}" +
43-
")+"
44-
)
32+
internal val EMAIL_ADDRESS: Pattern by lazy {
33+
Pattern.compile(
34+
"[a-zA-Z0-9+._%\\-]{1,256}" +
35+
"@" +
36+
"[a-zA-Z0-9][a-zA-Z0-9\\-]{0,64}" +
37+
"(" +
38+
"\\." +
39+
"[a-zA-Z0-9][a-zA-Z0-9\\-]{0,25}" +
40+
")+"
41+
)
42+
}
43+
44+
/**
45+
* Name Pattern Regex
46+
*/
47+
internal val NAME_PATTERN_REGEX by lazy { Pattern.compile("^[\\p{L} .']+$") }
48+
49+
/**
50+
* This pattern is intended for searching for things that look like they
51+
* might be phone numbers in arbitrary text, not for validating whether
52+
* something is in fact a phone number. It will miss many things that
53+
* are legitimate phone numbers.
54+
*
55+
*
56+
* The pattern matches the following:
57+
*
58+
* * Optionally, a + sign followed immediately by one or more digits. Spaces, dots, or dashes may follow.
59+
* * Optionally, sets of digits in parentheses, separated by spaces, dots, or dashes.
60+
* * A string starting and ending with a digit, containing digits, spaces, dots, and/or dashes.
61+
*
62+
*/
63+
internal val PHONE_REGEX: Pattern by lazy {
64+
Pattern.compile( // sdd = space, dot, or dash
65+
"(\\+[0-9]+[\\- .]*)?" // +<digits><sdd>*
66+
+ "(\\([0-9]+\\)[\\- .]*)?" // (<digits>)<sdd>*
67+
+ "([0-9][0-9\\- .]+[0-9])" // <digit><digit|sdd>+<digit>
68+
)
69+
}
70+
4571

4672
/**
4773
* Converts duration in seconds to hours, minutes & seconds.

README.md

+31-15
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,16 @@ How To
1313
Add it in your `root build.gradle` at the end of repositories:
1414
```gradle
1515
allprojects {
16-
repositories {
17-
...
18-
maven { url 'https://jitpack.io' }
19-
}
16+
repositories {
17+
...
18+
maven { url 'https://jitpack.io' }
19+
}
2020
}
2121
```
2222
dependency to your module `build.gradle` file
2323
```gradle
24-
dependencies {
25-
implementation 'com.github.Eldhopj:kotlin-extensions:1.0'
24+
dependencies {
25+
implementation 'com.github.Eldhopj:kotlin-extensions:1.1'
2626
}
2727
```
2828

@@ -35,32 +35,48 @@ Usage
3535

3636
- .**isValidEmail** -> Checks whether the email is valid, returns true if valid else false
3737
- .**isValidName** -> Checks whether the name is valid, returns true if valid else false
38+
- .**isValidPhoneNumber** -> Checks whether the phone number is valid, returns true if valid else false
3839
- .**capitalizeEachWord** -> Capitalize each word
3940
* eg: hello world -> Hello World
40-
- .**toHourMinuteSeconds** -> * Converts the seconds to Hours, Minutes & Seconds as per your pattern.
4141

42-
Default pattern is %dh %dm %ds.
42+
- .**checkRegex** -> Checks the regex pattern is valid, returns true if valid else false
43+
44+
Parameters
45+
1.pattern -> regex pattern.
46+
- .**toHourMinuteSeconds** -> Converts the seconds to Hours, Minutes & Seconds as per your pattern.
47+
48+
Parameters
49+
1.pattern -> in which pattern we have to convert. Default Pattern is %dh %dm %ds.
4350
* eg: 1h 2m 30s
44-
- .**toHourMinuteSeconds** -> Converts the seconds to Minutes & Seconds as per your pattern.
51+
- .**toHourMinute** -> Converts the seconds to Minutes & Seconds as per your pattern.
4552

46-
Default pattern is %dm %ds.
53+
Parameters
54+
1.pattern -> in which pattern we have to convert. Default Pattern is %dm %ds.
4755
* eg: 1hr 2m
4856

57+
Please go thorugh the [String Extenstions][3] code documentation for more information
58+
4959
## **Number Extenstions**
5060

5161
- .**shortenString** -> Format number into short values.
5262
* eg: 1000 -> 1k
53-
* 1000000 -> 1M
63+
* 1000000 -> 1M
5464
- .**format** -> Returns the string representation of the decimal values
5565

56-
Pattern : defaut its 0.##, u can pass any desired pattern
57-
RoundingMode: default its RoundingMode.HALF_EVEN
66+
Parameters
67+
1.pattern -> in which pattern we have to convert. Default Pattern is 0.##.
68+
2.roundingMode -> set the rounding behavour, default behavour is RoundingMode.HALF_EVEN
5869
* eg: 10.02542 -> 10.03
59-
70+
- .**isNullOrZero** -> Returns true if this number is null or zero (0)
71+
72+
Please go thorugh the [Number Extenstions][4] code documentation for more information
73+
6074
## **Date Extenstions**
61-
75+
6276
Please go thorugh the [Date Extension][2] code documentation
6377

6478

6579
[1]: https://jitpack.io/#Eldhopj/kotlin-extensions/Tag
6680
[2]: https://github.com/Eldhopj/kotlin-extensions/blob/master/Kotlin_extensions/src/main/java/com/eldhopj/kotlin_extensions/DateExtension.kt "Date Extension"
81+
[3]: https://github.com/Eldhopj/kotlin-extensions/blob/master/Kotlin_extensions/src/main/java/com/eldhopj/kotlin_extensions/StringExtensions.kt "String Extenstions"
82+
[4]: https://github.com/Eldhopj/kotlin-extensions/blob/master/Kotlin_extensions/src/main/java/com/eldhopj/kotlin_extensions/NumberExtension.kt "Number Extenstions"

app/src/main/java/com/eldhopj/extensions/MainActivity.kt

+3
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@ package com.eldhopj.extensions
22

33
import android.os.Bundle
44
import androidx.appcompat.app.AppCompatActivity
5+
import com.eldhopj.kotlin_extensions.isNullOrZero
56

67
class MainActivity : AppCompatActivity() {
78
override fun onCreate(savedInstanceState: Bundle?) {
89
super.onCreate(savedInstanceState)
910
setContentView(R.layout.activity_main)
1011

12+
val num = 10.99
13+
num.isNullOrZero()
1114
}
1215
}

0 commit comments

Comments
 (0)