diff --git a/README.md b/README.md index 51eb799..efae377 100644 --- a/README.md +++ b/README.md @@ -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' } ``` diff --git a/android-sample/build.gradle b/android-sample/build.gradle index fa4a664..18bffd7 100644 --- a/android-sample/build.gradle +++ b/android-sample/build.gradle @@ -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 diff --git a/build.gradle b/build.gradle index f750bae..8413d7a 100644 --- a/build.gradle +++ b/build.gradle @@ -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' diff --git a/library/src/main/java/com/cesarferreira/pluralize/Pluralize.kt b/library/src/main/kotlin/com/cesarferreira/pluralize/Pluralize.kt similarity index 87% rename from library/src/main/java/com/cesarferreira/pluralize/Pluralize.kt rename to library/src/main/kotlin/com/cesarferreira/pluralize/Pluralize.kt index db1d4cc..b9cbc59 100644 --- a/library/src/main/java/com/cesarferreira/pluralize/Pluralize.kt +++ b/library/src/main/kotlin/com/cesarferreira/pluralize/Pluralize.kt @@ -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> { "i$" to "us", "ae$" to "a") } - - diff --git a/library/src/main/java/com/cesarferreira/pluralize/utils/Plurality.kt b/library/src/main/kotlin/com/cesarferreira/pluralize/utils/Plurality.kt similarity index 100% rename from library/src/main/java/com/cesarferreira/pluralize/utils/Plurality.kt rename to library/src/main/kotlin/com/cesarferreira/pluralize/utils/Plurality.kt