Skip to content

Commit

Permalink
Currency: Implement currency formatting (symbol mode) (1/2)
Browse files Browse the repository at this point in the history
Ref #238
Ref #351
  • Loading branch information
rxaviers committed Dec 9, 2014
1 parent 0453b62 commit 4c050c6
Show file tree
Hide file tree
Showing 15 changed files with 379 additions and 6 deletions.
13 changes: 13 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,18 @@ module.exports = function( grunt ) {
}
}
},
{
name: "globalize.currency",
include: [ "currency" ],
exclude: [ "cldr", "cldr/event", "./core", "./number" ],
create: true,
override: {
wrap: {
startFile: "src/build/intro-currency.js",
endFile: "src/build/outro.js"
}
}
},
{
name: "globalize.date",
include: [ "date" ],
Expand Down Expand Up @@ -283,6 +295,7 @@ module.exports = function( grunt ) {
dist: {
files: {
"tmp/globalize.min.js": [ "dist/globalize.js" ],
"tmp/globalize/currency.min.js": [ "dist/globalize/currency.js" ],
"tmp/globalize/date.min.js": [ "dist/globalize/date.js" ],
"tmp/globalize/number.min.js": [ "dist/globalize/number.js" ],
"tmp/globalize/plural.min.js": [ "dist/globalize/plural.js" ],
Expand Down
42 changes: 42 additions & 0 deletions src/build/intro-currency.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*!
* Globalize v@VERSION
*
* http://github.com/jquery/globalize
*
* Copyright 2010, 2014 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",
"./number",
"cldr/event"
], 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 ) {

var alwaysArray = Globalize._alwaysArray,
formatMessage = Globalize._formatMessage,
numberNumberingSystem = Globalize._numberNumberingSystem,
validateCldr = Globalize._validateCldr,
validateDefaultLocale = Globalize._validateDefaultLocale,
validateParameterPresence = Globalize._validateParameterPresence,
validateParameterType = Globalize._validateParameterType,
validateParameterTypePlainObject = Globalize._validateParameterTypePlainObject;
1 change: 1 addition & 0 deletions src/build/node-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ require( "./globalize/number" );
require( "./globalize/plural" );

// Load after globalize/number
require( "./globalize/currency" );
require( "./globalize/date" );
13 changes: 13 additions & 0 deletions src/common/regexp-not-s.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 53 additions & 4 deletions src/currency.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,71 @@
define([
"./core",
"./common/validate/cldr",
"./common/validate/default-locale",
"./common/validate/parameter-presence",
"./common/validate/parameter-type/currency",
"./common/validate/parameter-type/plain-object",
"./currency/code-pattern",
"./currency/name-format",
"./currency/name-pattern",
"./currency/symbol-pattern",
"./util/object/omit",
"./number",
"cldr/event"
], function( Globalize ) {
], function( Globalize, validateCldr, validateDefaultLocale, validateParameterPresence,
validateParameterTypeCurrency, validateParameterTypePlainObject, currencyCodePattern,
currencyNameFormat, currencyNamePattern, currencySymbolPattern, objectOmit ) {

/**
* .currencyFormatter( currency [, options] )
*
* @currency [String] 3-letter currency code as defined by ISO 4217.
*
* @options [Object]:
* - form: [String] "symbol" (default), "code" or "name".
* - style: [String] "symbol" (default), "code" or "name".
* - see also number/format options.
*
* Return a function that formats a currency according to the given options and default/instance
* locale.
*/
Globalize.currencyFormatter =
Globalize.prototype.currencyFormatter = function( currency, options ) {
var cldr, numberFormatter, pattern, patternFn;

validateParameterPresence( currency, "currency" );
validateParameterTypeCurrency( currency, "currency" );

validateParameterTypePlainObject( options, "options" );

options = options || {};
cldr = this.cldr;

validateDefaultLocale( cldr );

cldr.on( "get", validateCldr );

// Get pattern given style "symbol" (default), "code" or "name".
patternFn = { code: currencyCodePattern, name: currencyNamePattern };
pattern = ( patternFn[ options.style ] || currencySymbolPattern )( currency, cldr );

cldr.off( "get", validateCldr );

// Return formatter when style is "symbol" or "code".
if ( typeof pattern === "string" ) {

// options = options minus style, plus pattern.
options = objectOmit( options, "style" );
options.pattern = pattern;
return this.numberFormatter( options );
}

// Return formatter when style is "name".
numberFormatter = this.numberFormatter( pattern.pattern );
// FIXME validate plural presence or throw "load plural module" error.
//plural = this.plural(); // FIXME generator
return function( value ) {
return currencyNameFormat( numberFormatter( value ), /* plural( value ), */ pattern );
};
};

/**
Expand Down Expand Up @@ -47,7 +96,7 @@ Globalize.prototype.currencyParser = function( /* currency, options */ ) {
* Format a currency according to the given options and the default/instance locale.
*/
Globalize.formatCurrency =
Globalize.prototype.formatCurrency = function( value, currency, options ) {
Globalize.prototype.formatCurrency = function( /* value, currency, options */ ) {
};

/**
Expand All @@ -62,7 +111,7 @@ Globalize.prototype.formatCurrency = function( value, currency, options ) {
* Return the parsed currency or NaN when value is invalid.
*/
Globalize.parseCurrency =
Globalize.prototype.parseCurrency = function( value, currency, options ) {
Globalize.prototype.parseCurrency = function( /* value, currency, options */ ) {
};

return Globalize;
Expand Down
9 changes: 9 additions & 0 deletions src/currency/code-pattern.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
define([], function() {

/**
*/
return function( /* currency, cldr, options */ ) {
// FIXME
};

});
17 changes: 17 additions & 0 deletions src/currency/name-format.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
define([
"../common/format-message"
], function( formatMessage ) {

/**
*/
return function( value, pluralForm, properties ) {
var displayNames = properties.displayNames,
unitPatterns = properties.unitPatterns;

return formatMessage( unitPatterns[ pluralForm ] || unitPatterns.other, [
value,
displayNames[ pluralForm ] || displayNames.other
]);
};

});
9 changes: 9 additions & 0 deletions src/currency/name-pattern.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
define([], function() {

/**
*/
return function( /*currency, cldr, options */ ) {
// FIXME
};

});
59 changes: 59 additions & 0 deletions src/currency/symbol-pattern.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
define([
"../common/regexp-not-s",
"../number/numbering-system"
], function( regexpNotS, numberNumberingSystem ) {

/**
* symbolPattern( currency, cldr )
*
* Return pattern replacing `¤` with the appropriate currency symbol literal.
*/
return function( currency, cldr ) {
var currencySpacing, pattern,
regexp = {
"[:digit:]": /\d/,
"[:^S:]": regexpNotS
},
symbol = cldr.main([
"numbers/currencies",
currency,
"symbol"
]);

currencySpacing = [ "beforeCurrency", "afterCurrency" ].map(function( position ) {
return cldr.main([
"numbers",
"currencyFormats-numberSystem-" + numberNumberingSystem( cldr ),
"currencySpacing",
position
]);
});

// TODO: Allow other `u-cu`s [1].
pattern = cldr.main([
"numbers",
"currencyFormats-numberSystem-" + numberNumberingSystem( cldr ),
"standard" /* 1 */
]);

return pattern.split( ";" ).map(function( pattern ) {

// "\u00A4" = "¤"
return pattern.split( "\u00A4" ).map(function( part, i ) {
var currencyMatch = regexp[ currencySpacing[ i ].currencyMatch ],
surroundingMatch = regexp[ currencySpacing[ i ].surroundingMatch ],
insertBetween = "";

if ( currencyMatch.test( symbol.charAt( i ? symbol.length - 1 : 0 ) ) && part &&
surroundingMatch.test(
part.charAt( i ? 0 : part.length - 1 ).replace( /[#@,.]/g, "0" )
) ) {
insertBetween = currencySpacing[ i ].insertBetween;
}

return ( i ? insertBetween : "" ) + part + ( i ? "" : insertBetween );
}).join( "'" + symbol + "'" );
}).join( ";" );
};

});
6 changes: 4 additions & 2 deletions src/number.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ define([
"./common/validate/parameter-type/string",
"./number/format",
"./number/format-properties",
"./number/numbering-system",
"./number/parse",
"./number/parse-properties",
"./number/pattern",
Expand All @@ -19,8 +20,8 @@ define([
], function( Globalize, createErrorUnsupportedFeature, validateCldr, validateDefaultLocale,
validateParameterPresence, validateParameterRange, validateParameterTypeNumber,
validateParameterTypePlainObject, validateParameterTypeString, numberFormat,
numberFormatProperties, numberParse, numberParseProperties, numberPattern, numberSymbol,
stringPad ) {
numberFormatProperties, numberNumberingSystem, numberParse, numberParseProperties, numberPattern,
numberSymbol, stringPad ) {

/**
* .numberFormatter( [options] )
Expand Down Expand Up @@ -164,6 +165,7 @@ Globalize.prototype.parseNumber = function( value, options ) {
* Optimization to avoid duplicating some internal functions across modules.
*/
Globalize._createErrorUnsupportedFeature = createErrorUnsupportedFeature;
Globalize._numberNumberingSystem = numberNumberingSystem;
Globalize._numberSymbol = numberSymbol;
Globalize._stringPad = stringPad;
Globalize._validateParameterTypeString = validateParameterTypeString;
Expand Down
20 changes: 20 additions & 0 deletions src/util/object/omit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
define([
"../always-array"
], function( alwaysArray ) {

return function( object, keys ) {
var key,
copy = {};

keys = alwaysArray( keys );

for ( key in object ) {
if ( keys.indexOf( key ) === -1 ) {
copy[ key ] = object[ key ];
}
}

return copy;
};

});
3 changes: 3 additions & 0 deletions test/functional.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ require([
// core
"./functional/core",

// currency
"./functional/currency/currency-formatter",

// date
"./functional/date/date-formatter",
"./functional/date/date-parser",
Expand Down
Loading

0 comments on commit 4c050c6

Please sign in to comment.