Skip to content

Commit

Permalink
Update String#isPhone() returns whether the string is exact phone num…
Browse files Browse the repository at this point in the history
…ber.
  • Loading branch information
DylanCaiCoding committed Dec 28, 2021
1 parent 58b7362 commit 8f9d163
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 4 deletions.
81 changes: 81 additions & 0 deletions longan/src/androidTest/java/com/dylanc/longan/StringTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,97 @@ class StringTest {

@Test
fun isPhone() {
// China mobile:
Assert.assertTrue("13488888888".isPhone())
Assert.assertTrue("13588888888".isPhone())
Assert.assertTrue("13688888888".isPhone())
Assert.assertTrue("13788888888".isPhone())
Assert.assertTrue("13888888888".isPhone())
Assert.assertTrue("13988888888".isPhone())
Assert.assertTrue("14788888888".isPhone())
Assert.assertTrue("14888888888".isPhone())
Assert.assertTrue("15088888888".isPhone())
Assert.assertTrue("15188888888".isPhone())
Assert.assertTrue("15288888888".isPhone())
Assert.assertTrue("15788888888".isPhone())
Assert.assertTrue("15888888888".isPhone())
Assert.assertTrue("15988888888".isPhone())
Assert.assertTrue("17288888888".isPhone())
Assert.assertTrue("17888888888".isPhone())
Assert.assertTrue("18288888888".isPhone())
Assert.assertTrue("18388888888".isPhone())
Assert.assertTrue("18488888888".isPhone())
Assert.assertTrue("18788888888".isPhone())
Assert.assertTrue("18888888888".isPhone())
Assert.assertTrue("19588888888".isPhone())
Assert.assertTrue("19888888888".isPhone())

// China unicom:
Assert.assertTrue("13088888888".isPhone())
Assert.assertTrue("13188888888".isPhone())
Assert.assertTrue("13288888888".isPhone())
Assert.assertTrue("14588888888".isPhone())
Assert.assertTrue("14688888888".isPhone())
Assert.assertTrue("15588888888".isPhone())
Assert.assertTrue("15688888888".isPhone())
Assert.assertTrue("16688888888".isPhone())
Assert.assertTrue("17588888888".isPhone())
Assert.assertTrue("18588888888".isPhone())
Assert.assertTrue("18688888888".isPhone())
Assert.assertTrue("19688888888".isPhone())

// China telecom:
Assert.assertTrue("13388888888".isPhone())
Assert.assertTrue("14988888888".isPhone())
Assert.assertTrue("15388888888".isPhone())
Assert.assertTrue("17388888888".isPhone())
Assert.assertTrue("17488888888".isPhone())
Assert.assertTrue("17788888888".isPhone())
Assert.assertTrue("18088888888".isPhone())
Assert.assertTrue("18188888888".isPhone())
Assert.assertTrue("18988888888".isPhone())
Assert.assertTrue("19188888888".isPhone())
Assert.assertTrue("19388888888".isPhone())
Assert.assertTrue("19988888888".isPhone())

// China nrta:
Assert.assertTrue("19088888888".isPhone())
Assert.assertTrue("19288888888".isPhone())
Assert.assertTrue("19788888888".isPhone())

// China telecom virtual
Assert.assertTrue("16288888888".isPhone())
Assert.assertTrue("17008888888".isPhone())
Assert.assertTrue("17018888888".isPhone())
Assert.assertTrue("17028888888".isPhone())

// China unicom virtual
Assert.assertTrue("16788888888".isPhone())
Assert.assertTrue("17048888888".isPhone())
Assert.assertTrue("17078888888".isPhone())
Assert.assertTrue("17088888888".isPhone())
Assert.assertTrue("17098888888".isPhone())
Assert.assertTrue("17188888888".isPhone())

// China mobile virtual
Assert.assertTrue("16588888888".isPhone())
Assert.assertTrue("17038888888".isPhone())
Assert.assertTrue("17058888888".isPhone())
Assert.assertTrue("17068888888".isPhone())

Assert.assertFalse("138888".isPhone())
Assert.assertFalse("28888888888".isPhone())
}

@Test
fun isEmail() {
Assert.assertTrue("dylancai@qq.com".isEmail())
Assert.assertFalse("dylancai.com".isEmail())
}

@Test
fun isIDCard18() {
Assert.assertTrue("440881199901014554".isIDCard18())
Assert.assertFalse("44088119990101".isIDCard18())
}
}
1 change: 1 addition & 0 deletions longan/src/main/java/com/dylanc/longan/MetaData.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import android.content.BroadcastReceiver
import android.content.ComponentName
import android.content.pm.PackageManager


fun applicationMetaDataOf(name: String): String? =
try {
application.packageManager.getApplicationInfo(packageName, PackageManager.GET_META_DATA)
Expand Down
16 changes: 14 additions & 2 deletions longan/src/main/java/com/dylanc/longan/String.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
package com.dylanc.longan

import android.text.format.Formatter
import android.util.Patterns
import androidx.core.util.PatternsCompat
import org.json.JSONObject
import java.util.*
Expand All @@ -31,6 +30,19 @@ const val REGEX_ID_CARD_15: String =
const val REGEX_ID_CARD_18: String =
"^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([0-9Xx])$"

/**
* Regex of exact phone number. Update at 2021.05.13.
* - China mobile: 134,135,136,137,138,139,147,148,150,151,152,157,158,159,172,178,182,183,184,187,188,195,198
* - China unicom: 130,131,132,145,146,155,156,166,175,176,185,186,196
* - China telecom: 133,149,153,173,174,177,180,181,189,191,193,199
* - China nrta: 190,192,197
* - China mobile virtual: 165,1703,1705,1706
* - China unicom virtual: 167,1704,1707,1708,1709,171
* - China telecom virtual: 162,1700,1701,1702
*/
const val REGEX_PHONE_EXACT :String =
"^1(3\\d|4[5-9]|5[0-35-9]|6[2567]|7[0-8]|8\\d|9[0-35-9])\\d{8}$"

inline val randomUUIDString: String
get() = UUID.randomUUID().toString()

Expand All @@ -44,7 +56,7 @@ fun String.limitLength(length: Int): String =
if (this.length <= length) this else substring(0, length)

fun String.isPhone(): Boolean =
Patterns.PHONE.matcher(this).matches()
REGEX_PHONE_EXACT.toRegex().matches(this)

fun String.isDomainName(): Boolean =
PatternsCompat.DOMAIN_NAME.matcher(this).matches()
Expand Down
7 changes: 5 additions & 2 deletions longan/src/main/java/com/dylanc/longan/View.kt
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,10 @@ inline val View.locationOnScreen: Rect
Rect(it[0], it[1], it[0] + width, it[1] + height)
}

@Deprecated("", replaceWith = ReplaceWith("withStyledAttributes(set, attrs, defStyleAttr, defStyleRes, block)"))
@Deprecated(
"Replace with new api",
replaceWith = ReplaceWith("withStyledAttributes(set, attrs, defStyleAttr, defStyleRes, block)")
)
inline fun View.withStyledAttrs(
set: AttributeSet?,
@StyleableRes attrs: IntArray,
Expand Down Expand Up @@ -166,4 +169,4 @@ fun <T> viewTags(key: Int) = object : ReadWriteProperty<View, T?> {

override fun setValue(thisRef: View, property: KProperty<*>, value: T?) =
thisRef.setTag(key, value)
}
}

0 comments on commit 8f9d163

Please sign in to comment.