1
0
Fork 0
mirror of https://github.com/ethauvin/kotlin-pluralizer.git synced 2025-04-27 01:28:12 -07:00

Compare commits

..

No commits in common. "master" and "0.2.4" have entirely different histories.

7 changed files with 41 additions and 61 deletions

View file

@ -4,9 +4,9 @@ android:
components: components:
- tools - tools
- platform-tools - platform-tools
- build-tools-24.0.3 - build-tools-24.0.2
- extra-android-m2repository - extra-android-m2repository
- android-25 - android-24
jdk: jdk:
- oraclejdk8 - oraclejdk8

View file

@ -3,21 +3,16 @@
**kotlin extension** to **pluralize** and **singularize** strings **kotlin extension** to **pluralize** and **singularize** strings
[![Build Status](https://travis-ci.org/cesarferreira/kotlin-pluralizer.svg?branch=master)](https://travis-ci.org/cesarferreira/kotlin-pluralizer) [![Release](https://jitpack.io/v/cesarferreira/kotlin-pluralizer.svg)](https://jitpack.io/#cesarferreira/kotlin-pluralizer) [![Build Status](https://travis-ci.org/cesarferreira/kotlin-pluralizer.svg?branch=master)](https://travis-ci.org/cesarferreira/kotlin-pluralizer) [![Release](https://jitpack.io/v/cesarferreira/kotlin-pluralizer.svg)](https://jitpack.io/#cesarferreira/kkotlin-pluralizer) [ ![bintray](https://api.bintray.com/packages/cesarferreira/maven/kotlin-pluralizer/images/download.svg) ](https://bintray.com/cesarferreira/maven/kotlin-pluralizer/_latestVersion)
### Show some love
[![GitHub followers](https://img.shields.io/github/followers/cesarferreira.svg?style=social&label=Follow)](https://github.com/cesarferreira/kotlin-pluralizer) [![Twitter Follow](https://img.shields.io/twitter/follow/cesarmcferreira.svg?style=social)](https://twitter.com/cesarmcferreira)
## Usage ## Usage
**Pluralization:** **Pluralization:**
```kotlin ```kotlin
"person".pluralize() # => "people" "person".pluralize() # => "people"
"post".pluralize() # => "posts" "post".pluralize() # => "posts"
"sheep".pluralize() # => "sheep" "sheep".pluralize() # => "sheep"
"foot".pluralize() # => "feet"
``` ```
**Singuralization:** **Singuralization:**
@ -25,14 +20,7 @@
```kotlin ```kotlin
"words".singularize() # => "word" "words".singularize() # => "word"
"octopi".singularize() # => "octopus" "octopi".singularize() # => "octopus"
"people".singularize() # => "person"
"feet".singularize() # => "foot"
```
**Quantities:**
```kotlin
"person".pluralize(1) # => "person"
"person".pluralize(2) # => "people"
``` ```
## Install ## Install
@ -43,10 +31,13 @@ repositories {
maven { url "https://jitpack.io" } maven { url "https://jitpack.io" }
} }
dependencies { dependencies {
compile 'com.github.cesarferreira:kotlin-pluralizer:0.2.9' compile 'com.github.cesarferreira:kotlin-pluralizer:0.2.4'
} }
``` ```
## Show some love
[![GitHub followers](https://img.shields.io/github/followers/cesarferreira.svg?style=social&label=Follow)](https://github.com/cesarferreira/kotlin-pluralizer) [![Twitter Follow](https://img.shields.io/twitter/follow/cesarmcferreira.svg?style=social)](https://twitter.com/cesarmcferreira)
## Contributing ## 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: 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:
@ -54,11 +45,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. 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 :) 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. 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/kotlin-pluralizer/issues) first, before filing an issue. 5. Check for existing [issues](https://github.com/cesarferreira/kotkotlin-pluralizer/issues) first, before filing an issue.
6. Have fun! 6. Have fun!
## Credits ## Credits
The pluralize and singularize methods are based on the code found in the following places. The pluralize and singularize methods are based on the code found in the following places.

View file

@ -12,11 +12,12 @@ apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions' apply plugin: 'kotlin-android-extensions'
android { android {
compileSdkVersion 25 compileSdkVersion 24
buildToolsVersion "24.0.3" buildToolsVersion "24.0.2"
defaultConfig { defaultConfig {
applicationId "com.cesarferreira.supposedlibrary"
minSdkVersion 16 minSdkVersion 16
targetSdkVersion 25 targetSdkVersion 24
versionCode 1 versionCode 1
versionName "1.0" versionName "1.0"
} }
@ -38,8 +39,8 @@ android {
dependencies { dependencies {
compile project(':library') compile project(':library')
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
compile 'com.android.support:appcompat-v7:25.1.0' compile 'com.android.support:appcompat-v7:24.2.0'
compile 'com.android.support:design:25.1.0' compile 'com.android.support:design:24.2.0'
} }
repositories { repositories {
mavenCentral() mavenCentral()

View file

@ -1,12 +1,12 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules. // Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript { buildscript {
ext.kotlin_version = '1.0.6' ext.kotlin_version = '1.0.3'
ext { ext {
ext_groupId = 'com.cesarferreira' ext_groupId = 'com.cesarferreira'
ext_artifactId = 'kotlin-pluralizer' ext_artifactId = 'kotlin-pluralizer'
ext_version = '0.2.9' ext_version = '0.2.4'
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'
@ -16,7 +16,7 @@ buildscript {
jcenter() jcenter()
} }
dependencies { dependencies {
classpath 'com.android.tools.build:gradle:2.2.3' classpath 'com.android.tools.build:gradle:2.1.3'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
} }
} }

View file

@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip

View file

@ -25,48 +25,34 @@ 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.toLowerCase())) 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 exception = exceptions().firstOrNull() { this.equals(it.component1()) } val excep = exceptions().firstOrNull() { this.equals(it.component1()) }
if (exception != null) found = exception.component2() if (excep != null) found = excep.component2()
return found return found
} }
private fun String.singularizer(): String { private fun String.singularizer(): String {
if (unCountable().contains(this.toLowerCase())) { if (unCountable().contains(this)) {
return this return this
} }
val exceptions = exceptions().firstOrNull() { this.equals(it.component2()) } val excepions = exceptions().firstOrNull() { this.equals(it.component2()) }
if (exceptions != null) { if (excepions != null) {
return exceptions.component1() return excepions.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 {
@ -214,3 +200,5 @@ fun singularizeRules(): List<Pair<String, String>> {
"i$" to "us", "i$" to "us",
"ae$" to "a") "ae$" to "a")
} }