This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Foundation | |
| extension String { | |
| mutating func insert(separator: String, every n: Int) { | |
| self = inserting(separator: separator, every: n) | |
| } | |
| func inserting(separator: String, every n: Int) -> String { | |
| var result: String = "" | |
| let characters = Array(self) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.security.MessageDigest | |
| val String.md5: String | |
| get() { | |
| val bytes = MessageDigest.getInstance("MD5").digest(this.toByteArray()) | |
| return bytes.joinToString("") { | |
| "%02x".format(it) | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.security.MessageDigest | |
| val String.sha1: String | |
| get() { | |
| val bytes = MessageDigest.getInstance("SHA-1").digest(this.toByteArray()) | |
| return bytes.joinToString("") { | |
| "%02x".format(it) | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.regex.Pattern | |
| fun String.isEmailValid(): Boolean { | |
| val expression = "^[\\w.-]+@([\\w\\-]+\\.)+[A-Z]{2,8}$" | |
| val pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE) | |
| val matcher = pattern.matcher(this) | |
| return matcher.matches() | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import org.json.JSONArray | |
| import org.json.JSONException | |
| import org.json.JSONObject | |
| fun JSONObject.getIntOrNull(name: String): Int? = | |
| try { | |
| getInt(name) | |
| } | |
| catch (e: JSONException) { | |
| val strValue = getStringOrNull(name) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import android.content.Context | |
| import io.michaelrocks.libphonenumber.android.PhoneNumberUtil | |
| fun String.formatPhoneNumber(context: Context, region: String): String? { | |
| val phoneNumberKit = PhoneNumberUtil.createInstance(context) | |
| val number = phoneNumberKit.parse(this, region) | |
| if (!phoneNumberKit.isValidNumber(number)) | |
| return null | |
| return phoneNumberKit.format(number, PhoneNumberUtil.PhoneNumberFormat.INTERNATIONAL) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import com.google.i18n.phonenumbers.PhoneNumberUtil | |
| fun String.formatPhoneNumber(region: String): String? { | |
| val phoneNumberKit = PhoneNumberUtil.getInstance() | |
| val number = phoneNumberKit.parse(this, region) | |
| if (!phoneNumberKit.isValidNumber(number)) | |
| return null | |
| return phoneNumberKit.format(number, PhoneNumberUtil.PhoneNumberFormat.INTERNATIONAL) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| val String.lastPathComponent: String | |
| get() { | |
| var path = this | |
| if (path.endsWith("/")) | |
| path = path.substring(0, path.length - 1) | |
| var index = path.lastIndexOf('/') | |
| if (index < 0) { | |
| if (path.endsWith("\\")) | |
| path = path.substring(0, path.length - 1) | |
| index = path.lastIndexOf('\\') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| extension String { | |
| func matches(_ expression: String) -> Bool { | |
| if let range = range(of: expression, options: .regularExpression, range: nil, locale: nil) { | |
| return range.lowerBound == startIndex && range.upperBound == endIndex | |
| } else { | |
| return false | |
| } | |
| } | |
| var isValidEmail: Bool { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| val String.containsLatinLetter: Boolean | |
| get() = matches(Regex(".*[A-Za-z].*")) | |
| val String.containsDigit: Boolean | |
| get() = matches(Regex(".*[0-9].*")) | |
| val String.isAlphanumeric: Boolean | |
| get() = matches(Regex("[A-Za-z0-9]*")) | |
| val String.hasLettersAndDigits: Boolean |