Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding a view helper to pluralize based on a count
- Loading branch information
danschultz
committed
Mar 4, 2011
1 parent
f13166f
commit ef217de
Showing
3 changed files
with
61 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} | ||
} | ||
} | ||
} |