Skip to content

Commit

Permalink
Plural: Create initial module
Browse files Browse the repository at this point in the history
Borrow santhoshtr/CLDRPluralRuleParser.

Ref #220
  • Loading branch information
rxaviers committed Apr 29, 2014
1 parent 9551c5d commit 588f80e
Show file tree
Hide file tree
Showing 12 changed files with 1,079 additions and 3 deletions.
25 changes: 24 additions & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ module.exports = function( grunt ) {
baseUrl: ".",
optimize: "none",
paths: {
cldr: "../external/cldrjs/dist/cldr"
cldr: "../external/cldrjs/dist/cldr",
CLDRPluralRuleParser: "../external/CLDRPluralRuleParser/src/CLDRPluralRuleParser"
},
skipSemiColonInsertion: true,
skipModuleInsertion: true,
Expand All @@ -102,6 +103,15 @@ module.exports = function( grunt ) {
onBuildWrite: function ( id, path, contents ) {
var name = camelCase( id.replace(/util\//, "") );

// CLDRPluralRuleParser
if ( (/CLDRPluralRuleParser/).test( id ) ) {
// 1: UMD intro
// 2: UMD outro
return contents
.replace( /\(function\(root, factory\)[\s\S]*?}\(this, function\(\) {/, "var CLDRPluralRuleParser = (function() {" ) /* 1 */
.replace( /}\)\);\s+$/, "}());" ); /* 2 */
}

// 1, and 2: Remove define() wrap.
// 3: Remove empty define()'s.
contents = contents
Expand Down Expand Up @@ -162,6 +172,18 @@ module.exports = function( grunt ) {
}
}
},
{
name: "globalize.plural",
include: [ "plural" ],
exclude: [ "cldr", "./core" ],
create: true,
override: {
wrap: {
startFile: "src/build/intro-plural.js",
endFile: "src/build/outro.js"
}
}
},
{
name: "globalize.message",
include: [ "message" ],
Expand Down Expand Up @@ -222,6 +244,7 @@ module.exports = function( grunt ) {
"dist/globalize.min.js": [ "dist/globalize.js" ],
"dist/globalize/date.min.js": [ "dist/globalize/date.js" ],
"dist/globalize/number.min.js": [ "dist/globalize/number.js" ],
"dist/globalize/plural.min.js": [ "dist/globalize/plural.js" ],
"dist/globalize/message.min.js": [ "dist/globalize/message.js" ]
}
}
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Node.js module.
- [Date module](#date_module)
- [Message module](#message_module)
- [Number module](#number_module)
- [Plural module](#plural_module)
- more to come...
- [Development](#development)
- [File structure](#file_structure)
Expand Down
1 change: 1 addition & 0 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"cldrjs": "0.3.4"
},
"devDependencies": {
"CLDRPluralRuleParser": "rxaviers/CLDRPluralRuleParser#3a9a4f6a32455d82cb6ed9170c4d676a2719d29a",
"jquery": "2.0.3",
"qunit": "1.12.0",
"requirejs": "2.1.9",
Expand Down
32 changes: 32 additions & 0 deletions doc/api/plural/plural.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
## Globalize.plural( value [, locale] )

Return the count group String: `zero`, `one`, `two`, `few`, `many`, or `other`.

### Parameters

**value**

Number to be formatted, eg. `0` (integer), or `3.14` (decimal).

**locale** Optional

Locale string that overrides default.


### Example

```javascript
Globalize.locale( "en" );
Globalize.plural( 0 ); // "other"
Globalize.plural( 1 ); // "one"
Globalize.plural( 2 ); // "other"

Globalize.plural( 0, "ar" ); // "zero"
Globalize.plural( 1, "ar" ); // "one"
Globalize.plural( 2, "ar" ); // "two"
Globalize.plural( 3, "ar" ); // "few"

Globalize.plural( 0, "ru" ); // "many"
Globalize.plural( 1, "ru" ); // "one"
Globalize.plural( 2, "ru" ); // "few"
```
3 changes: 2 additions & 1 deletion src/build/allinone-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ module.exports = require( "./globalize" );

// Extent core with the following modules
require( "./globalize/date" );
require( "./globalize/number" );
require( "./globalize/message" );
require( "./globalize/number" );
require( "./globalize/plural" );
31 changes: 31 additions & 0 deletions src/build/intro-plural.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*!
* Globalize v@VERSION
*
* http://github.com/jquery/globalize
*
* Copyright 2005, 2013 jQuery Foundation, Inc. and other contributors
* Released under the MIT license
* http://jquery.org/license
*
* Date: @DATE
*/
(function( root, factory ) {

// UMD returnExports
if ( typeof define === "function" && define.amd ) {

// AMD
define([
"cldr",
"../globalize"
], factory );
} else if ( typeof exports === "object" ) {

// Node, CommonJS
module.exports = factory( require( "cldrjs" ), require( "globalize" ) );
} else {

// Global
factory( root.Cldr, root.Globalize );
}
}(this, function( Cldr, Globalize ) {
34 changes: 34 additions & 0 deletions src/plural.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
define([
"./common/get-locale",
"./core",
"./plural/form"
], function( commonGetLocale, Globalize, pluralForm ) {

/**
* Globalize.plural( value, locale )
*
* @value [Number]
*
* @locale [String]
*
* Return the count group String: zero | one | two | few | many | other.
*/
Globalize.plural = function( value, locale ) {
var form;

if ( typeof value !== "number" ) {
throw new Error( "Value is not a number" );
}

locale = commonGetLocale( locale );

if ( !( form = pluralForm( value, locale ) ) ) {
throw new Error( "Plural form not found!" );
}

return form;
};

return Globalize;

});
28 changes: 28 additions & 0 deletions src/plural/form.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
define([
"CLDRPluralRuleParser",
"./rules"
], function( CLDRPluralRuleParser, pluralRules ) {

/**
* pluralForm( value, cldr )
*
* @value [Number]
*
* @cldr [Cldr instance].
*
* Return the corresponding form (zero | one | two | few | many | other) of a value given locale @cldr.
*/
return function( value, cldr ) {
var form,
rules = pluralRules( cldr );

for( form in rules ) {
if( CLDRPluralRuleParser( rules[ form ], value ) ) {
return form.replace( /pluralRule-count-/, "" );
}
}

return null;
};

});
14 changes: 14 additions & 0 deletions src/plural/rules.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
define(function() {

/**
* pluralRules( cldr )
*
* @cldr [Cldr instance].
*
* Return all the plural rules of a language.
*/
return function( cldr ) {
return cldr.supplemental( "plurals-type-cardinal/{language}" );
};

});
Loading

0 comments on commit 588f80e

Please sign in to comment.