Created
September 28, 2015 23:24
-
-
Save alunny/c9777c4c171b986fad9f to your computer and use it in GitHub Desktop.
This file contains hidden or 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
diff --git a/src/unit.js b/src/unit.js | |
index 5ad4499..3f35708 100644 | |
--- a/src/unit.js | |
+++ b/src/unit.js | |
@@ -2,17 +2,21 @@ define([ | |
"./core", | |
"./common/validate/parameter-presence", | |
"./common/validate/parameter-type/number", | |
+ "./common/validate/parameter-type/plain-object", | |
"./unit/format", | |
+ "./unit/formatter-fn", | |
+ "./unit/get", | |
"./plural" | |
-], function( Globalize, validateParameterPresence, validateParameterTypeNumber, unitFormat ) { | |
+], function( Globalize, validateParameterPresence, validateParameterTypeNumber, | |
+ validateParameterTypePlainObject, unitFormat, unitFormatterFn, unitGet ) { | |
/** | |
* Globalize.formatUnit( value, unit, options ) | |
* | |
* @value [Number] | |
* | |
- * @unit [String]: | |
+ * @unit [String]: The unit (e.g "second", "day", "year") | |
* | |
* @options [Object] | |
* - form: [String] "long", "short" (default), or "narrow". | |
@@ -21,16 +25,33 @@ define([ | |
*/ | |
Globalize.formatUnit = | |
Globalize.prototype.formatUnit = function( value, unit, options ) { | |
- var pluralGenerator; | |
+ validateParameterTypeNumber( value, "value" ); | |
- validateParameterTypeNumber( value ); | |
- validateParameterPresence( unit ); | |
+ return this.unitFormatter( unit, options )( value ); | |
+}; | |
+ | |
+/** | |
+ * Globalize.unitFormatter( unit, options ) | |
+ * | |
+ * @unit [String]: The unit (e.g "second", "day", "year") | |
+ * | |
+ * @options [Object] | |
+ * - form: [String] "long", "short" (default), or "narrow". | |
+ */ | |
+Globalize.unitFormatter = | |
+Globalize.prototype.unitFormatter = function( unit, options ) { | |
+ var unitProperties, form; | |
- pluralGenerator = this.pluralGenerator(); | |
+ validateParameterPresence( unit, "unit" ); | |
+ validateParameterTypePlainObject( options, "options" ); | |
- return unitFormat( value, unit, options, pluralGenerator, this.cldr, this ); | |
+ form = options.form || "long"; | |
+ unitProperties = unitGet( unit, form, this.cldr ); | |
+ | |
+ return unitFormatterFn( unitProperties, this.pluralGenerator() ); | |
}; | |
return Globalize; | |
}); | |
+ | |
diff --git a/src/unit/format.js b/src/unit/format.js | |
index 8962035..f044349 100644 | |
--- a/src/unit/format.js | |
+++ b/src/unit/format.js | |
@@ -1,17 +1,15 @@ | |
define([ | |
- "./get", | |
"../common/format-message" | |
-], function( unitGet, formatMessage ) { | |
+], function( formatMessage ) { | |
/** | |
- * format( value, unit, options, cldr ) | |
+ * format( value, unit, pluralGenerator ) | |
* | |
* @value [Number] | |
* | |
- * @unit [String]: | |
+ * @unitProperies [Object]: localized unit data from cldr | |
* | |
- * @options [Object] | |
- * - form: [String] "long", "short" (default), or "narrow". | |
+ * @pluralGenerator [Object]: A pluralGenerator from Globalize.pluralGenerator | |
* | |
* TODO pass along numberFormatter | |
* | |
@@ -25,18 +23,10 @@ define([ | |
* Duration Unit (for composed time unit durations) is not implemented. | |
* http://www.unicode.org/reports/tr35/tr35-35/tr35-general.html#durationUnit | |
*/ | |
-return function( value, unit, options, pluralGenerator, cldr ) { | |
- var form, message, ret; | |
- options = options || {}; | |
- form = options.form || "long"; | |
+return function( value, unitProperties, pluralGenerator ) { | |
+ var message; | |
- ret = unitGet( unit, form, cldr ); | |
- | |
- if ( !ret ) { | |
- return; | |
- } | |
- | |
- message = ret[ pluralGenerator( value ) ]; | |
+ message = unitProperties[ pluralGenerator( value ) ]; | |
return formatMessage( message, [ value ] ); | |
}; | |
diff --git a/src/unit/formatter-fn.js b/src/unit/formatter-fn.js | |
new file mode 100644 | |
index 0000000..013078b | |
--- /dev/null | |
+++ b/src/unit/formatter-fn.js | |
@@ -0,0 +1,17 @@ | |
+define([ | |
+ "../common/validate/parameter-presence", | |
+ "../common/validate/parameter-type/number", | |
+ "./format" | |
+], function( validateParameterPresence, validateParameterTypeNumber, unitFormat ) { | |
+ | |
+return function( unitProperties, pluralGenerator ) { | |
+ return function unitFormatter( value ) { | |
+ validateParameterPresence( value, "value" ); | |
+ validateParameterTypeNumber( value, "value" ); | |
+ | |
+ return unitFormat( value, unitProperties, pluralGenerator ); | |
+ }; | |
+ | |
+}; | |
+ | |
+}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment