Skip to content

Commit

Permalink
Merge pull request #1 from Eldhopj/dev/eldhopj/new_extensions_and_imp…
Browse files Browse the repository at this point in the history
…rovements

New extensions and improvements
  • Loading branch information
Eldhopj authored Apr 26, 2022
2 parents 56b4d98 + e6bcbf6 commit bc766de
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 72 deletions.
2 changes: 1 addition & 1 deletion Kotlin_extensions/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ afterEvaluate {
release(MavenPublication) {
groupId = 'com.github.Eldhopj'
artifactId = 'kotlin-extension'
version = '1.0'
version = '1.1'

from components.java
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.eldhopj.kotlin_extensions

import java.io.File

private const val KILO_BYTE_SIZE = 1024F

/**
* Returns the file size in MB.
*
* @return
*/
fun File?.getSizeInMB(): Float =
if (this == null) 0.00f else "%.2f".format(File(this.absolutePath).length() / (KILO_BYTE_SIZE * KILO_BYTE_SIZE))
.toFloat()

Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import java.text.DecimalFormat

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

/**
* Returns the string representation of the value using [DecimalFormat]
* eg: 10.02542 -> 10.03
*
* @param pattern pattern conversion pattern for [DecimalFormat]
* @param roundingMode Specifies the rounding behavior for operations whose results cannot be
Expand All @@ -50,18 +52,17 @@ fun Float?.format(
*
* @return
*/
val Long.shortenString: String
get() {
var value = this
val arr = arrayOf("", "K", "M", "B", "T", "P", "E")
var index = 0
while (value / THOUSAND >= 1) {
value /= THOUSAND
index++
}
val decimalFormat = DecimalFormat("#.##")
return String.format("%s %s", decimalFormat.format(value), arr[index])
fun Long.getShortenString(): String {
var value = this
val arr = arrayOf("", "K", "M", "B", "T", "P", "E")
var index = 0
while (value / THOUSAND >= 1) {
value /= THOUSAND
index++
}
val decimalFormat = DecimalFormat("#.##")
return String.format("%s %s", decimalFormat.format(value), arr[index])
}

/**
* Format number into Short values
Expand All @@ -71,15 +72,19 @@ val Long.shortenString: String
*
* @return
*/
val Int.shortenString: String
get() {
var value = this
val arr = arrayOf("", "K", "M", "B", "T", "P", "E")
var index = 0
while (value / THOUSAND >= 1) {
value /= THOUSAND
index++
}
val decimalFormat = DecimalFormat("#.##")
return String.format("%s %s", decimalFormat.format(value), arr[index])
fun Int.getShortenString(): String {
var value = this
val arr = arrayOf("", "K", "M", "B", "T", "P", "E")
var index = 0
while (value / THOUSAND >= 1) {
value /= THOUSAND
index++
}
val decimalFormat = DecimalFormat("#.##")
return String.format("%s %s", decimalFormat.format(value), arr[index])
}

/**
* Returns true if this number is null or zero (0)
*/
fun Number?.isNullOrZero(): Boolean = this == null || this == 0
Original file line number Diff line number Diff line change
Expand Up @@ -2,67 +2,77 @@ package com.eldhopj.kotlin_extensions

import com.eldhopj.kotlin_extensions.utils.Utils.EMAIL_ADDRESS
import com.eldhopj.kotlin_extensions.utils.Utils.NAME_PATTERN_REGEX
import com.eldhopj.kotlin_extensions.utils.Utils.PHONE_REGEX
import com.eldhopj.kotlin_extensions.utils.Utils.convertToHourMinute
import com.eldhopj.kotlin_extensions.utils.Utils.convertToHourMinuteSeconds
import java.util.Locale
import java.util.regex.Pattern

/**
* Checks whether the email is valid or not
* @param regEx Regular expression value to match the text for the email format.
*
* @return true-> if valid, else false
*/
fun String?.isValidEmail(regEx: Regex = EMAIL_ADDRESS.toRegex()): Boolean =
this != null && this.matches(regEx)
fun String.isValidEmail(): Boolean = this.checkRegex(EMAIL_ADDRESS)

/**
* Checks whether the user name is valid name or not.
*
* @return true-> if valid, else false
*/
fun String?.isValidName(pattern: Regex = Regex(NAME_PATTERN_REGEX)): Boolean {
return this != null &&
this.trim().isNotEmpty() &&
matches(pattern)
}
fun String.isValidName(): Boolean = this.checkRegex(NAME_PATTERN_REGEX)

/**
* Extension method to check if String is Phone Number.
*/
fun String.isValidPhoneNumber(): Boolean = this.checkRegex(PHONE_REGEX)

/**
* Capitalize each word
*
* eg: hello world -> Hello World
*
*/
val String?.capitalizeEachWord
get() = this?.lowercase(Locale.getDefault())?.split(" ")?.joinToString(" ") { word ->
fun String?.getCapitalizeEachWord() =
this?.lowercase(Locale.getDefault())?.split(" ")?.joinToString(" ") { word ->
if (word.length <= 1) word // if the word is a single character then no need to capitalize.
else
word.replaceFirstChar {
if (it.isLowerCase()) it.titlecase(Locale.getDefault())
else it.toString()
}
}?.trimEnd()?.trim()
}?.trim()

/**
* Converts the seconds to Hours, Minutes & Seconds pattern.
*
* E.g., 1h 2m 30s
*
* @param pattern Default Pattern is %dh %dm %ds.
* @param pattern in which pattern we have to convert. Default Pattern is %dh %dm %ds.
*
* @return String in Hours, Minutes & Seconds pattern
*/
fun String?.toHourMinuteSeconds(pattern: String = "%dh %dm %ds"): String {
if (this == null) return ""
return convertToHourMinuteSeconds(this, pattern)
}
fun String?.toHourMinuteSeconds(pattern: String = "%dh %dm %ds"): String =
if (this == null) "" else convertToHourMinuteSeconds(this, pattern)

/**
* Converts the seconds to Hours, Minutes pattern.
*
* E.g., 1h 2m.
*
* @param pattern Default Pattern is %dhr %dmin.
* @param pattern in which pattern we have to convert. Default Pattern is %dhr %dmin.
* @return String in Hours, Minutes pattern
*/
fun String?.toHourMinute(pattern: String = "%dh %dm"): String {
if (this == null) return ""
return convertToHourMinute(this, pattern)
fun String?.toHourMinute(pattern: String = "%dh %dm"): String =
if (this == null) "" else convertToHourMinute(this, pattern)

/**
* Check regex
*
* @param pattern Regex pattern
* @return @return true-> if valid, else false
*/
fun String.checkRegex(pattern: Pattern): Boolean {
val regex = pattern.toRegex()
return this.isNotBlank() && this.matches(regex)
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@ import java.util.regex.Pattern
*
* @constructor Create empty Utils
*/
object Utils {
/**
* Name Pattern Regex
*/
internal const val NAME_PATTERN_REGEX = "^[\\p{L} .'-]+$"
internal object Utils {

/**
* Thousand
Expand All @@ -33,15 +29,45 @@ object Utils {
/**
* Email Address
*/
internal val EMAIL_ADDRESS = Pattern.compile(
"[a-zA-Z0-9\\+\\.\\_\\%\\-\\+]{1,256}" +
"\\@" +
"[a-zA-Z0-9][a-zA-Z0-9\\-]{0,64}" +
"(" +
"\\." +
"[a-zA-Z0-9][a-zA-Z0-9\\-]{0,25}" +
")+"
)
internal val EMAIL_ADDRESS: Pattern by lazy {
Pattern.compile(
"[a-zA-Z0-9+._%\\-]{1,256}" +
"@" +
"[a-zA-Z0-9][a-zA-Z0-9\\-]{0,64}" +
"(" +
"\\." +
"[a-zA-Z0-9][a-zA-Z0-9\\-]{0,25}" +
")+"
)
}

/**
* Name Pattern Regex
*/
internal val NAME_PATTERN_REGEX by lazy { Pattern.compile("^[\\p{L} .']+$") }

/**
* This pattern is intended for searching for things that look like they
* might be phone numbers in arbitrary text, not for validating whether
* something is in fact a phone number. It will miss many things that
* are legitimate phone numbers.
*
*
* The pattern matches the following:
*
* * Optionally, a + sign followed immediately by one or more digits. Spaces, dots, or dashes may follow.
* * Optionally, sets of digits in parentheses, separated by spaces, dots, or dashes.
* * A string starting and ending with a digit, containing digits, spaces, dots, and/or dashes.
*
*/
internal val PHONE_REGEX: Pattern by lazy {
Pattern.compile( // sdd = space, dot, or dash
"(\\+[0-9]+[\\- .]*)?" // +<digits><sdd>*
+ "(\\([0-9]+\\)[\\- .]*)?" // (<digits>)<sdd>*
+ "([0-9][0-9\\- .]+[0-9])" // <digit><digit|sdd>+<digit>
)
}


/**
* Converts duration in seconds to hours, minutes & seconds.
Expand Down
46 changes: 31 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ How To
Add it in your `root build.gradle` at the end of repositories:
```gradle
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
repositories {
...
maven { url 'https://jitpack.io' }
}
}
```
dependency to your module `build.gradle` file
```gradle
dependencies {
implementation 'com.github.Eldhopj:kotlin-extensions:1.0'
dependencies {
implementation 'com.github.Eldhopj:kotlin-extensions:1.1'
}
```

Expand All @@ -35,32 +35,48 @@ Usage

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

Default pattern is %dh %dm %ds.
- .**checkRegex** -> Checks the regex pattern is valid, returns true if valid else false

Parameters
1.pattern -> regex pattern.
- .**toHourMinuteSeconds** -> Converts the seconds to Hours, Minutes & Seconds as per your pattern.

Parameters
1.pattern -> in which pattern we have to convert. Default Pattern is %dh %dm %ds.
* eg: 1h 2m 30s
- .**toHourMinuteSeconds** -> Converts the seconds to Minutes & Seconds as per your pattern.
- .**toHourMinute** -> Converts the seconds to Minutes & Seconds as per your pattern.

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

Please go thorugh the [String Extenstions][3] code documentation for more information

## **Number Extenstions**

- .**shortenString** -> Format number into short values.
* eg: 1000 -> 1k
* 1000000 -> 1M
* 1000000 -> 1M
- .**format** -> Returns the string representation of the decimal values

Pattern : defaut its 0.##, u can pass any desired pattern
RoundingMode: default its RoundingMode.HALF_EVEN
Parameters
1.pattern -> in which pattern we have to convert. Default Pattern is 0.##.
2.roundingMode -> set the rounding behavour, default behavour is RoundingMode.HALF_EVEN
* eg: 10.02542 -> 10.03

- .**isNullOrZero** -> Returns true if this number is null or zero (0)

Please go thorugh the [Number Extenstions][4] code documentation for more information

## **Date Extenstions**

Please go thorugh the [Date Extension][2] code documentation


[1]: https://jitpack.io/#Eldhopj/kotlin-extensions/Tag
[2]: https://github.com/Eldhopj/kotlin-extensions/blob/master/Kotlin_extensions/src/main/java/com/eldhopj/kotlin_extensions/DateExtension.kt "Date Extension"
[3]: https://github.com/Eldhopj/kotlin-extensions/blob/master/Kotlin_extensions/src/main/java/com/eldhopj/kotlin_extensions/StringExtensions.kt "String Extenstions"
[4]: https://github.com/Eldhopj/kotlin-extensions/blob/master/Kotlin_extensions/src/main/java/com/eldhopj/kotlin_extensions/NumberExtension.kt "Number Extenstions"
3 changes: 3 additions & 0 deletions app/src/main/java/com/eldhopj/extensions/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ package com.eldhopj.extensions

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.eldhopj.kotlin_extensions.isNullOrZero

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

val num = 10.99
num.isNullOrZero()
}
}

0 comments on commit bc766de

Please sign in to comment.