1
0
Fork 0
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:
Cesar Ferreira 2016-09-10 12:48:02 +01:00
commit 9043139d30
5 changed files with 27 additions and 16 deletions

View file

@ -43,7 +43,7 @@ repositories {
maven { url "https://jitpack.io" } maven { url "https://jitpack.io" }
} }
dependencies { dependencies {
compile 'com.github.cesarferreira:kotlin-pluralizer:0.2.6' compile 'com.github.cesarferreira:kotlin-pluralizer:0.2.7'
} }
``` ```

View file

@ -15,7 +15,6 @@ android {
compileSdkVersion 24 compileSdkVersion 24
buildToolsVersion "24.0.2" buildToolsVersion "24.0.2"
defaultConfig { defaultConfig {
applicationId "com.cesarferreira.supposedlibrary"
minSdkVersion 16 minSdkVersion 16
targetSdkVersion 24 targetSdkVersion 24
versionCode 1 versionCode 1
@ -24,7 +23,7 @@ android {
lintOptions { lintOptions {
abortOnError false abortOnError false
} }
buildTypes { buildTypes {
release { release {
minifyEnabled false minifyEnabled false

View file

@ -6,7 +6,7 @@ buildscript {
ext { ext {
ext_groupId = 'com.cesarferreira' ext_groupId = 'com.cesarferreira'
ext_artifactId = 'kotlin-pluralizer' ext_artifactId = 'kotlin-pluralizer'
ext_version = '0.2.4' ext_version = '0.2.5'
ext_url = 'https://github.com/cesarferreira/kotlin-pluralizer' ext_url = 'https://github.com/cesarferreira/kotlin-pluralizer'
ext_vcsUrl = 'https://github.com/cesarferreira/kotlin-pluralizer.git' ext_vcsUrl = 'https://github.com/cesarferreira/kotlin-pluralizer.git'
ext_description = 'Kotlin extension to pluralize and singularize strings' ext_description = 'Kotlin extension to pluralize and singularize strings'

View file

@ -25,19 +25,33 @@ fun String.singularize(plurality: Plurality = Plurality.Plural): String {
if (this.pluralizer() != this && this + "s" != this.pluralizer() && if (this.pluralizer() != this && this + "s" != this.pluralizer() &&
this.pluralizer().singularize() == this && this.singularizer() != this) this.pluralizer().singularize() == this && this.singularizer() != this)
return this; return this
return this.singularize() 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 { private fun String.pluralizer(): String {
if (unCountable().contains(this)) return this if (unCountable().contains(this)) return this
val rule = pluralizeRules().last { Pattern.compile(it.component1(), Pattern.CASE_INSENSITIVE).matcher(this).find() } 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()) var found = Pattern.compile(rule.component1(), Pattern.CASE_INSENSITIVE).matcher(this).replaceAll(rule.component2())
val endswith = exceptions().firstOrNull { this.endsWith(it.component1()) } val endsWith = exceptions().firstOrNull { this.endsWith(it.component1()) }
if (endswith != null) found = this.replace(endswith.component1(), endswith.component2()) if (endsWith != null) found = this.replace(endsWith.component1(), endsWith.component2())
val excep = exceptions().firstOrNull() { this.equals(it.component1()) } val exception = exceptions().firstOrNull() { this.equals(it.component1()) }
if (excep != null) found = excep.component2() if (exception != null) found = exception.component2()
return found return found
} }
@ -45,14 +59,14 @@ private fun String.singularizer(): String {
if (unCountable().contains(this)) { if (unCountable().contains(this)) {
return this return this
} }
val excepions = exceptions().firstOrNull() { this.equals(it.component2()) } val exceptions = exceptions().firstOrNull() { this.equals(it.component2()) }
if (excepions != null) { if (exceptions != null) {
return excepions.component1() 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 { try {
if (singularizeRules().count { if (singularizeRules().count {
@ -200,5 +214,3 @@ fun singularizeRules(): List<Pair<String, String>> {
"i$" to "us", "i$" to "us",
"ae$" to "a") "ae$" to "a")
} }