Skip to content

Commit

Permalink
adding a view helper to pluralize based on a count
Browse files Browse the repository at this point in the history
  • Loading branch information
danschultz committed Mar 4, 2011
1 parent f13166f commit ef217de
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -42,7 +42,7 @@
<dependency>
<groupId>com.github.danschultz</groupId>
<artifactId>as3-collections</artifactId>
<version>1.1</version>
<version>1.2.0-SNAPSHOT</version>
<type>swc</type>
</dependency>
<dependency>
Expand Down
29 changes: 29 additions & 0 deletions src/mesh/view/helpers/text/pluralizeByCount.as
@@ -0,0 +1,29 @@
package mesh.view.helpers.text
{
import mesh.core.inflection.pluralize;

/**
* Pluralizes a <code>singular</code> word based on its <code>count</code>. The word
* is pluralized if and only if <code>count < 1 < count</code>. If <code>plural</code>
* is given, then that word will be used. Otherwise an inflection will be used.
*
* <p>
* <strong>Examples:</strong>
* <listing version="3.0">
* trace( pluralize(2, "person") ); // 2 people
* trace( pluralize(-2, "person") ); // -2 people
* trace( pluralize(1, "fish") ); // 1 fish
* trace( pluralize(0, "fish") ); // 0 fishes
* trace( pluralize(2, "cake", "desserts") ); // 2 desserts
* trace( pluralize(2, "cake", "desserts") ); // 2 desserts
* </listing>
* </p>
*
* @return Either a singular or pluralized word when <code>count</code> is not 1.
* @see mesh.core.inflection.pluralize()
*/
public function pluralizeByCount(count:Number, singular:String, plural:String = null):String
{
return count.toString() + " " + (count == 1 ? singular : (plural != null) ? plural : pluralize(singular));
}
}
31 changes: 31 additions & 0 deletions tests/mesh/view/helpers/text/PluralizeByCountTests.as
@@ -0,0 +1,31 @@
package mesh.view.helpers.text
{
import org.flexunit.assertThat;
import org.hamcrest.object.equalTo;

public class PluralizeByCountTests
{
private var _tests:Array;

[Before]
public function setup():void
{
_tests = [
{args:[-1, "person"], expected:"-1 people"},
{args:[0, "person"], expected:"0 people"},
{args:[1, "person"], expected:"1 person"},
{args:[2, "person"], expected:"2 people"},
{args:[1, "cake", "desserts"], expected:"1 cake"},
{args:[2, "cake", "desserts"], expected:"2 desserts"}
];
}

[Test]
public function testPluralize():void
{
for each (var test:Object in _tests) {
assertThat("test failed for args '" + test.args + "'", pluralizeByCount.apply(null, test.args), equalTo(test.expected));
}
}
}
}

0 comments on commit ef217de

Please sign in to comment.