mirror of
https://github.com/ethauvin/kotlin-pluralizer.git
synced 2025-04-25 00:37:12 -07:00
Merge branch 'master' of github.com:cesarferreira/kotlin-pluralizer
# Conflicts: # README.md
This commit is contained in:
commit
9043139d30
5 changed files with 27 additions and 16 deletions
|
@ -43,7 +43,7 @@ repositories {
|
|||
maven { url "https://jitpack.io" }
|
||||
}
|
||||
dependencies {
|
||||
compile 'com.github.cesarferreira:kotlin-pluralizer:0.2.6'
|
||||
compile 'com.github.cesarferreira:kotlin-pluralizer:0.2.7'
|
||||
}
|
||||
```
|
||||
|
||||
|
|
|
@ -15,7 +15,6 @@ android {
|
|||
compileSdkVersion 24
|
||||
buildToolsVersion "24.0.2"
|
||||
defaultConfig {
|
||||
applicationId "com.cesarferreira.supposedlibrary"
|
||||
minSdkVersion 16
|
||||
targetSdkVersion 24
|
||||
versionCode 1
|
||||
|
@ -24,7 +23,7 @@ android {
|
|||
lintOptions {
|
||||
abortOnError false
|
||||
}
|
||||
|
||||
|
||||
buildTypes {
|
||||
release {
|
||||
minifyEnabled false
|
||||
|
|
|
@ -6,7 +6,7 @@ buildscript {
|
|||
ext {
|
||||
ext_groupId = 'com.cesarferreira'
|
||||
ext_artifactId = 'kotlin-pluralizer'
|
||||
ext_version = '0.2.4'
|
||||
ext_version = '0.2.5'
|
||||
ext_url = 'https://github.com/cesarferreira/kotlin-pluralizer'
|
||||
ext_vcsUrl = 'https://github.com/cesarferreira/kotlin-pluralizer.git'
|
||||
ext_description = 'Kotlin extension to pluralize and singularize strings'
|
||||
|
|
|
@ -25,19 +25,33 @@ fun String.singularize(plurality: Plurality = Plurality.Plural): String {
|
|||
|
||||
if (this.pluralizer() != this && this + "s" != this.pluralizer() &&
|
||||
this.pluralizer().singularize() == this && this.singularizer() != this)
|
||||
return this;
|
||||
return this
|
||||
|
||||
return this.singularize()
|
||||
}
|
||||
|
||||
fun String.pluralize(count: Int): String {
|
||||
if (count > 1)
|
||||
return this.pluralize(Plurality.Plural)
|
||||
else
|
||||
return this.pluralize(Plurality.Singular)
|
||||
}
|
||||
|
||||
fun String.singularize(count: Int): String {
|
||||
if (count > 1)
|
||||
return this.singularize(Plurality.Plural)
|
||||
else
|
||||
return this.singularize(Plurality.Singular)
|
||||
}
|
||||
|
||||
private fun String.pluralizer(): String {
|
||||
if (unCountable().contains(this)) return this
|
||||
val rule = pluralizeRules().last { Pattern.compile(it.component1(), Pattern.CASE_INSENSITIVE).matcher(this).find() }
|
||||
var found = Pattern.compile(rule.component1(), Pattern.CASE_INSENSITIVE).matcher(this).replaceAll(rule.component2())
|
||||
val endswith = exceptions().firstOrNull { this.endsWith(it.component1()) }
|
||||
if (endswith != null) found = this.replace(endswith.component1(), endswith.component2())
|
||||
val excep = exceptions().firstOrNull() { this.equals(it.component1()) }
|
||||
if (excep != null) found = excep.component2()
|
||||
val endsWith = exceptions().firstOrNull { this.endsWith(it.component1()) }
|
||||
if (endsWith != null) found = this.replace(endsWith.component1(), endsWith.component2())
|
||||
val exception = exceptions().firstOrNull() { this.equals(it.component1()) }
|
||||
if (exception != null) found = exception.component2()
|
||||
return found
|
||||
}
|
||||
|
||||
|
@ -45,14 +59,14 @@ private fun String.singularizer(): String {
|
|||
if (unCountable().contains(this)) {
|
||||
return this
|
||||
}
|
||||
val excepions = exceptions().firstOrNull() { this.equals(it.component2()) }
|
||||
val exceptions = exceptions().firstOrNull() { this.equals(it.component2()) }
|
||||
|
||||
if (excepions != null) {
|
||||
return excepions.component1()
|
||||
if (exceptions != null) {
|
||||
return exceptions.component1()
|
||||
}
|
||||
val endswith = exceptions().firstOrNull { this.endsWith(it.component2()) }
|
||||
val endsWith = exceptions().firstOrNull { this.endsWith(it.component2()) }
|
||||
|
||||
if (endswith != null) return this.replace(endswith.component2(), endswith.component1())
|
||||
if (endsWith != null) return this.replace(endsWith.component2(), endsWith.component1())
|
||||
|
||||
try {
|
||||
if (singularizeRules().count {
|
||||
|
@ -200,5 +214,3 @@ fun singularizeRules(): List<Pair<String, String>> {
|
|||
"i$" to "us",
|
||||
"ae$" to "a")
|
||||
}
|
||||
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue