1
0
Fork 0
mirror of https://github.com/ethauvin/kotlin-pluralizer.git synced 2025-04-28 18:08:11 -07:00

Compare commits

..

29 commits

Author SHA1 Message Date
Cesar Ferreira
6b65d56b19 bumped version 2017-01-30 11:52:35 +00:00
Cesar Ferreira
0afabd8a4e updated travis 2017-01-30 11:21:04 +00:00
Cesar Ferreira
4b1cdffa7e bumped versions 2017-01-30 11:04:06 +00:00
Cesar Ferreira
6fc79f6a94 bump version 2017-01-30 10:47:08 +00:00
César Ferreira
5af7c33746 Merge pull request #2 from marwanad/lower-case-check-for-uncountables
check uncountables against the lower-cased string
2017-01-30 10:45:24 +00:00
Marwan
3177a1ff16 check uncountables against the lower-cased string 2017-01-29 12:58:39 -05:00
Cesar Ferreira
322fdd6d0e updated readme 2016-09-10 12:56:41 +01:00
Cesar Ferreira
9043139d30 Merge branch 'master' of github.com:cesarferreira/kotlin-pluralizer
# Conflicts:
#	README.md
2016-09-10 12:48:02 +01:00
Cesar Ferreira
e579f7bfe1 updated readme 2016-09-10 12:43:42 +01:00
César Ferreira
906b9d8da4 Merge pull request #1 from ethauvin/master
Added pluralize/singularize functions based on an int count.
2016-09-09 23:55:07 +01:00
af2a59e3ec Added pluralize/singularize functions based on an int count. 2016-09-05 20:22:42 -07:00
cesarferreira
0cf3c54ae2 update readme 2016-09-05 21:21:59 +01:00
cesarferreira
dd09353592 updated readme 2016-09-05 20:33:38 +01:00
cesarferreira
46ece47676 fixed typo 2016-09-05 16:46:17 +01:00
cesarferreira
091e16a697 few tweaks and package name change 2016-09-05 14:51:55 +01:00
César Ferreira
3a56d6d792 Update build.gradle 2016-09-05 11:38:45 +01:00
Cesar Ferreira
acf66ae704 added more samples 2016-09-05 00:48:38 +01:00
Cesar Ferreira
ca927135f3 fixed typo 2016-09-05 00:44:43 +01:00
Cesar Ferreira
ba938bd865 fixed typo 2016-09-05 00:44:00 +01:00
Cesar Ferreira
a422910c03 updated readme 2016-09-05 00:29:58 +01:00
Cesar Ferreira
3b02359c3f updated readme 2016-09-05 00:29:44 +01:00
Cesar Ferreira
1f0c0f4dbe updated readme 2016-09-05 00:28:54 +01:00
Cesar Ferreira
aa567c97f7 added credit 2016-09-04 23:35:31 +01:00
Cesar Ferreira
ff11b6a6e4 refactoring 2016-09-04 23:33:25 +01:00
Cesar Ferreira
075acc0c94 it's now a kotlin library and not an android library 2016-09-04 23:31:25 +01:00
Cesar Ferreira
d0f22d0f40 updated readme 2016-09-04 21:51:31 +01:00
Cesar Ferreira
5b4cd994ec lowered gradle version 2016-09-04 21:21:44 +01:00
Cesar Ferreira
3be60a76a8 fixed typo 2016-09-04 21:01:06 +01:00
Cesar Ferreira
74d6ae324c bump 2016-09-04 20:45:42 +01:00
27 changed files with 156 additions and 143 deletions

View file

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

View file

@ -1,61 +1,72 @@
<!-- ![Image](extras/logo.png) -->
# 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)
# kotlin-pluralizer
**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)
### 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:**
```kotlin ```kotlin
"words".singularize() # => "word" "words".singularize() # => "word"
"octopi".pluralize() # => "octopus" "octopi".singularize() # => "octopus"
"people".singularize() # => "person"
"feet".singularize() # => "foot"
```
**Quantities:**
```kotlin
"person".pluralize(1) # => "person"
"person".pluralize(2) # => "people"
``` ```
## Install ## Install
```groovy ```groovy
repositories { repositories {
jcenter() jcenter()
maven { url "https://jitpack.io" } maven { url "https://jitpack.io" }
} }
dependencies { dependencies {
compile 'com.github.cesarferreira:kotlin-pluralizer:0.1.0' compile 'com.github.cesarferreira:kotlin-pluralizer:0.2.9'
} }
``` ```
### Show some :heart:
[![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:
1. Match coding style (braces, spacing, etc.) This is best achieved using `CMD`+`Option`+`L` (Reformat code) on Mac (not sure for Windows) with Android Studio defaults. 1. Match coding style (braces, spacing, etc.) This is best achieved using `CMD`+`Option`+`L` (Reformat code) on Mac (not sure for Windows) with Android Studio defaults.
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/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! 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.
- https://github.com/rails/rails/blob/26698fb91d88dca0f860adcb80528d8d3f0f6285/activesupport/lib/active_support/inflector/inflections.rb
- https://github.com/atteo/evo-inflector/blob/master/src/main/java/org/atteo/evo/inflector/English.java - https://github.com/atteo/evo-inflector/blob/master/src/main/java/org/atteo/evo/inflector/English.java
- http://www.java2s.com/Tutorial/Java/0040__Data-Type/Transformswordstosingularpluralhumanizedhumanreadableunderscorecamelcaseorordinalform.htm - http://www.java2s.com/Tutorial/Java/0040__Data-Type/Transformswordstosingularpluralhumanizedhumanreadableunderscorecamelcaseorordinalform.htm
- https://github.com/rails/rails/blob/26698fb91d88dca0f860adcb80528d8d3f0f6285/activesupport/lib/active_support/inflector/inflections.rb - https://github.com/MehdiK/Humanizer.jvm
### Created & Maintained By ### Created & Maintained By

View file

@ -1,20 +1,29 @@
buildscript {
repositories {
jcenter()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version"
}
}
apply plugin: 'com.android.application' apply plugin: 'com.android.application'
apply plugin: 'kotlin-android' apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android { android {
compileSdkVersion 24 compileSdkVersion 25
buildToolsVersion "24.0.2" buildToolsVersion "24.0.3"
defaultConfig { defaultConfig {
applicationId "com.cesarferreira.supposedlibrary"
minSdkVersion 16 minSdkVersion 16
targetSdkVersion 24 targetSdkVersion 25
versionCode 1 versionCode 1
versionName "1.0" versionName "1.0"
} }
lintOptions { lintOptions {
abortOnError false abortOnError false
} }
buildTypes { buildTypes {
release { release {
minifyEnabled false minifyEnabled false
@ -29,8 +38,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:24.2.0' compile 'com.android.support:appcompat-v7:25.1.0'
compile 'com.android.support:design:24.2.0' compile 'com.android.support:design:25.1.0'
} }
repositories { repositories {
mavenCentral() mavenCentral()

View file

@ -1,12 +1,11 @@
package com.cesarferreira.pluralize.sample package com.cesarferreira.pluralize.sample
import android.os.Bundle import android.os.Bundle
import android.support.design.widget.FloatingActionButton
import android.support.v7.app.AppCompatActivity import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.Toolbar import android.support.v7.widget.Toolbar
import android.util.Log
import com.cesarferreira.pluralize.pluralize import com.cesarferreira.pluralize.pluralize
import com.cesarferreira.pluralize.singularize import com.cesarferreira.pluralize.singularize
import kotlinx.android.synthetic.main.content_hello.*
class HelloActivity : AppCompatActivity() { class HelloActivity : AppCompatActivity() {
@ -16,16 +15,24 @@ class HelloActivity : AppCompatActivity() {
val toolbar = findViewById(R.id.toolbar) as Toolbar val toolbar = findViewById(R.id.toolbar) as Toolbar
setSupportActionBar(toolbar) setSupportActionBar(toolbar)
val fab = findViewById(R.id.fab) as FloatingActionButton val singulars = arrayOf("person", "banana", "woman")
fab.setOnClickListener({
view ->
Log.d("TAG", "person".pluralize())
Log.d("TAG", "banana".pluralize())
Log.d("TAG", "woman".pluralize())
Log.d("TAG", "women".singularize()) for (item in singulars) {
concat("$item -> pluralize -> ${item.pluralize()}")
}
}) concat("")
concat("")
val plurals = arrayOf("words", "octopi", "sheep")
for (item in plurals) {
concat("$item -> singularize -> ${item.singularize()}")
}
}
fun concat(str: String) {
centerTextView.append(str + "\n")
} }
} }

View file

@ -5,8 +5,7 @@
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:fitsSystemWindows="true" android:fitsSystemWindows="true">
tools:context="com.cesarferreira.supposedlibrary.com.cesarferreira.pluralize.sample.HelloActivity">
<android.support.design.widget.AppBarLayout <android.support.design.widget.AppBarLayout
android:layout_height="wrap_content" android:layout_height="wrap_content"
@ -24,12 +23,4 @@
<include layout="@layout/content_hello"/> <include layout="@layout/content_hello"/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@android:drawable/ic_dialog_email"/>
</android.support.design.widget.CoordinatorLayout> </android.support.design.widget.CoordinatorLayout>

View file

@ -11,7 +11,11 @@
android:paddingTop="@dimen/activity_vertical_margin" android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior" app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_hello" tools:showIn="@layout/activity_hello">
tools:context="com.cesarferreira.supposedlibrary.com.cesarferreira.pluralize.sample.HelloActivity">
<TextView
android:id="@+id/centerTextView"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout> </RelativeLayout>

View file

Before

Width:  |  Height:  |  Size: 3.3 KiB

After

Width:  |  Height:  |  Size: 3.3 KiB

Before After
Before After

View file

Before

Width:  |  Height:  |  Size: 2.2 KiB

After

Width:  |  Height:  |  Size: 2.2 KiB

Before After
Before After

View file

Before

Width:  |  Height:  |  Size: 4.7 KiB

After

Width:  |  Height:  |  Size: 4.7 KiB

Before After
Before After

View file

Before

Width:  |  Height:  |  Size: 7.5 KiB

After

Width:  |  Height:  |  Size: 7.5 KiB

Before After
Before After

View file

Before

Width:  |  Height:  |  Size: 10 KiB

After

Width:  |  Height:  |  Size: 10 KiB

Before After
Before After

View file

@ -1,4 +1,4 @@
<resources> <resources>
<string name="app_name">Pluralize</string> <string name="app_name">Pluralize</string>
<string name="title_activity_hello">HelloActivity</string> <string name="title_activity_hello">Pluralize</string>
</resources> </resources>

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.3' ext.kotlin_version = '1.0.6'
ext { ext {
ext_groupId = 'com.cesarferreira' ext_groupId = 'com.cesarferreira'
ext_artifactId = 'kotlin-pluralizer' ext_artifactId = 'kotlin-pluralizer'
ext_version = '0.1.0' ext_version = '0.2.9'
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.0-rc1' classpath 'com.android.tools.build:gradle:2.2.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.0-all.zip distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip

View file

@ -1,47 +1,19 @@
buildscript { buildscript {
repositories { repositories {
mavenCentral() jcenter()
} mavenLocal()
dependencies {
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
} }
} }
apply plugin: 'com.android.library' plugins {
apply plugin: 'kotlin-android' id 'com.jfrog.bintray' version '1.6'
apply plugin: 'com.github.dcendents.android-maven'
android {
compileSdkVersion 24
buildToolsVersion "24.0.2"
defaultConfig {
minSdkVersion 16
targetSdkVersion 24
versionCode 1
versionName "1.0"
}
lintOptions {
abortOnError false
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
}
} }
apply plugin: 'java'
apply plugin: 'kotlin'
apply plugin: 'maven-publish'
dependencies { dependencies {
compile gradleApi()
compile 'com.android.support:appcompat-v7:24.2.0'
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
} }
repositories { repositories {

View file

@ -1,52 +1,59 @@
// build a jar with source files
group "$ext_groupId"
version "$ext_version"
publishing {
publications {
MyPublication(MavenPublication) {
from components.java
artifact sourcesJar
groupId "$ext_groupId"
artifactId "$ext_artifactId"
version "$ext_version"
}
}
}
task sourcesJar(type: Jar) { task sourcesJar(type: Jar) {
from android.sourceSets.main.java.srcDirs from sourceSets.main.java.srcDirs
classifier = 'sources' classifier = 'sources'
} }
task javadoc(type: Javadoc) {
failOnError false
source = android.sourceSets.main.java.sourceFiles
classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
classpath += configurations.compile
}
// build a jar with javadoc
task javadocJar(type: Jar, dependsOn: javadoc) { task javadocJar(type: Jar, dependsOn: javadoc) {
classifier = 'javadoc' classifier = 'javadoc'
from javadoc.destinationDir from javadoc.destinationDir
} }
artifacts { artifacts {
archives sourcesJar
archives javadocJar archives javadocJar
archives sourcesJar
} }
// bintray {
//bintray { user = System.getenv('BINTRAY_USER')
// user = System.getenv('BINTRAY_USER') key = System.getenv('BINTRAY_API_KEY')
// key = System.getenv('BINTRAY_API_KEY')
// dryRun = false
// dryRun = false publish = true
// publish = true
// publications = ['MyPublication']
// publications = ['MyPublication'] pkg {
// pkg { repo = 'maven'
// repo = 'maven' name = "$ext_artifactId"
// name = "$ext_artifactId" licenses = ['Apache-2.0']
// licenses = ['Apache-2.0'] labels = ['android', 'gradle-plugin']
// labels = ['android', 'gradle-plugin']
// publicDownloadNumbers = true
// publicDownloadNumbers = true vcsUrl = "$ext_vcsUrl"
// vcsUrl = "$ext_vcsUrl"
// version {
// version { name = "$ext_version"
// name = "$ext_version" desc = "$ext_description"
// desc = "$ext_description" released = new Date()
// released = new Date() gpg {
// gpg { sign = true // Determines whether to GPG sign the files.
// sign = true // Determines whether to GPG sign the files. }
// } }
// } }
// } }
//}

View file

@ -25,34 +25,48 @@ 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.toLowerCase())) 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
} }
private fun String.singularizer(): String { private fun String.singularizer(): String {
if (unCountable().contains(this)) { if (unCountable().contains(this.toLowerCase())) {
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")
} }

View file

@ -1 +1 @@
include ':sample', ':library' include ':android-sample', ':library'