mirror of
https://github.com/ethauvin/kotlin-pluralizer.git
synced 2025-04-25 00:37:12 -07:00
Compare commits
19 commits
Author | SHA1 | Date | |
---|---|---|---|
|
6b65d56b19 | ||
|
0afabd8a4e | ||
|
4b1cdffa7e | ||
|
6fc79f6a94 | ||
|
5af7c33746 | ||
|
3177a1ff16 | ||
|
322fdd6d0e | ||
|
9043139d30 | ||
|
e579f7bfe1 | ||
|
906b9d8da4 | ||
af2a59e3ec | |||
|
0cf3c54ae2 | ||
|
dd09353592 | ||
|
46ece47676 | ||
|
091e16a697 | ||
|
3a56d6d792 | ||
|
acf66ae704 | ||
|
ca927135f3 | ||
|
ba938bd865 |
7 changed files with 61 additions and 41 deletions
|
@ -4,9 +4,9 @@ android:
|
|||
components:
|
||||
- tools
|
||||
- platform-tools
|
||||
- build-tools-24.0.2
|
||||
- build-tools-24.0.3
|
||||
- extra-android-m2repository
|
||||
- android-24
|
||||
- android-25
|
||||
|
||||
jdk:
|
||||
- oraclejdk8
|
||||
|
|
37
README.md
37
README.md
|
@ -3,41 +3,50 @@
|
|||
|
||||
**kotlin extension** to **pluralize** and **singularize** strings
|
||||
|
||||
[](https://travis-ci.org/cesarferreira/kotlin-pluralizer) [](https://jitpack.io/#cesarferreira/kkotlin-pluralizer) [  ](https://bintray.com/cesarferreira/maven/kotlin-pluralizer/_latestVersion)
|
||||
[](https://travis-ci.org/cesarferreira/kotlin-pluralizer) [](https://jitpack.io/#cesarferreira/kotlin-pluralizer)
|
||||
|
||||
### Show some love
|
||||
[](https://github.com/cesarferreira/kotlin-pluralizer) [](https://twitter.com/cesarmcferreira)
|
||||
|
||||
## Usage
|
||||
|
||||
|
||||
**Pluralization:**
|
||||
|
||||
```kotlin
|
||||
"person".pluralize() # => "people"
|
||||
"post".pluralize() # => "posts"
|
||||
"sheep".pluralize() # => "sheep"
|
||||
"person".pluralize() # => "people"
|
||||
"post".pluralize() # => "posts"
|
||||
"sheep".pluralize() # => "sheep"
|
||||
"foot".pluralize() # => "feet"
|
||||
```
|
||||
|
||||
**Singuralization:**
|
||||
|
||||
```kotlin
|
||||
"words".singularize() # => "word"
|
||||
"octopi".singularize() # => "octopus"
|
||||
"words".singularize() # => "word"
|
||||
"octopi".singularize() # => "octopus"
|
||||
"people".singularize() # => "person"
|
||||
"feet".singularize() # => "foot"
|
||||
```
|
||||
|
||||
**Quantities:**
|
||||
```kotlin
|
||||
"person".pluralize(1) # => "person"
|
||||
"person".pluralize(2) # => "people"
|
||||
```
|
||||
|
||||
## Install
|
||||
|
||||
```groovy
|
||||
repositories {
|
||||
jcenter()
|
||||
maven { url "https://jitpack.io" }
|
||||
jcenter()
|
||||
maven { url "https://jitpack.io" }
|
||||
}
|
||||
dependencies {
|
||||
compile 'com.github.cesarferreira:kotlin-pluralizer:0.2.4'
|
||||
compile 'com.github.cesarferreira:kotlin-pluralizer:0.2.9'
|
||||
}
|
||||
```
|
||||
|
||||
## Show some love
|
||||
[](https://github.com/cesarferreira/kotlin-pluralizer) [](https://twitter.com/cesarmcferreira)
|
||||
|
||||
## Contributing
|
||||
|
||||
I welcome and encourage all pull requests. It usually will take me within 24-48 hours to respond to any issue or request. Here are some basic rules to follow to ensure timely addition of your request:
|
||||
|
@ -45,11 +54,11 @@ I welcome and encourage all pull requests. It usually will take me within 24-48
|
|||
2. If its a feature, bugfix, or anything please only change code to what you specify.
|
||||
3. Please keep PR titles easy to read and descriptive of changes, this will make them easier to merge :)
|
||||
4. Pull requests _must_ be made against `develop` branch. Any other branch (unless specified by the maintainers) will get rejected.
|
||||
5. Check for existing [issues](https://github.com/cesarferreira/kotkotlin-pluralizer/issues) first, before filing an issue.
|
||||
5. Check for existing [issues](https://github.com/cesarferreira/kotlin-pluralizer/issues) first, before filing an issue.
|
||||
6. Have fun!
|
||||
|
||||
|
||||
## Credits
|
||||
## Credits
|
||||
|
||||
The pluralize and singularize methods are based on the code found in the following places.
|
||||
|
||||
|
|
|
@ -12,19 +12,18 @@ apply plugin: 'kotlin-android'
|
|||
apply plugin: 'kotlin-android-extensions'
|
||||
|
||||
android {
|
||||
compileSdkVersion 24
|
||||
buildToolsVersion "24.0.2"
|
||||
compileSdkVersion 25
|
||||
buildToolsVersion "24.0.3"
|
||||
defaultConfig {
|
||||
applicationId "com.cesarferreira.supposedlibrary"
|
||||
minSdkVersion 16
|
||||
targetSdkVersion 24
|
||||
targetSdkVersion 25
|
||||
versionCode 1
|
||||
versionName "1.0"
|
||||
}
|
||||
lintOptions {
|
||||
abortOnError false
|
||||
}
|
||||
|
||||
|
||||
buildTypes {
|
||||
release {
|
||||
minifyEnabled false
|
||||
|
@ -39,8 +38,8 @@ android {
|
|||
dependencies {
|
||||
compile project(':library')
|
||||
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
|
||||
compile 'com.android.support:appcompat-v7:24.2.0'
|
||||
compile 'com.android.support:design:24.2.0'
|
||||
compile 'com.android.support:appcompat-v7:25.1.0'
|
||||
compile 'com.android.support:design:25.1.0'
|
||||
}
|
||||
repositories {
|
||||
mavenCentral()
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
// Top-level build file where you can add configuration options common to all sub-projects/modules.
|
||||
|
||||
buildscript {
|
||||
ext.kotlin_version = '1.0.3'
|
||||
ext.kotlin_version = '1.0.6'
|
||||
|
||||
ext {
|
||||
ext_groupId = 'com.cesarferreira'
|
||||
ext_artifactId = 'kotlin-pluralizer'
|
||||
ext_version = '0.2.4'
|
||||
ext_version = '0.2.9'
|
||||
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'
|
||||
|
@ -16,7 +16,7 @@ buildscript {
|
|||
jcenter()
|
||||
}
|
||||
dependencies {
|
||||
classpath 'com.android.tools.build:gradle:2.1.3'
|
||||
classpath 'com.android.tools.build:gradle:2.2.3'
|
||||
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
|
||||
}
|
||||
}
|
||||
|
|
2
gradle/wrapper/gradle-wrapper.properties
vendored
2
gradle/wrapper/gradle-wrapper.properties
vendored
|
@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
|
|||
distributionPath=wrapper/dists
|
||||
zipStoreBase=GRADLE_USER_HOME
|
||||
zipStorePath=wrapper/dists
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip
|
||||
|
|
|
@ -25,34 +25,48 @@ 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
|
||||
if (unCountable().contains(this.toLowerCase())) 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
|
||||
}
|
||||
|
||||
private fun String.singularizer(): String {
|
||||
if (unCountable().contains(this)) {
|
||||
if (unCountable().contains(this.toLowerCase())) {
|
||||
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