Skip to content

Commit 7400ed1

Browse files
committed
Use runtimeArgs for compiled messageFormatters
messageformatterFn now uses the runtimeArgs generated by globalize-compiler instead of having them duplicated by messageFormatterRuntimeBind.
1 parent b55ca27 commit 7400ed1

File tree

4 files changed

+27
-34
lines changed

4 files changed

+27
-34
lines changed

src/message.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,16 +109,19 @@ Globalize.prototype.messageFormatter = function( path ) {
109109

110110
/* jshint evil:true */
111111
formatter = new Function(
112-
"number, plural, select, fmt", messageCompiler.funcname( cldr.locale ),
113-
"return " + formatterSrc )(
114-
runtime.number, runtime.plural, runtime.select, compiler.formatters, pluralGenerator
115-
);
112+
"number, plural, select", messageCompiler.funcname( cldr.locale ),
113+
" var fmt = [].slice.call( arguments, 4 );\n" +
114+
" return " + formatterSrc + "\n"
115+
);
116116

117-
returnFn = messageFormatterFn( formatter );
117+
returnFn = messageFormatterFn.apply( this, [
118+
formatter,
119+
runtime.number, runtime.plural, runtime.select, pluralGenerator
120+
].concat( compiler.formatters ) );
118121

119122
var runtimeArgs = [
120123
messageFormatterRuntimeBind(
121-
cldr, formatter, compiler.runtime, pluralType, cldr.locale, compiler.formatters
124+
formatter, formatterSrc, compiler.runtime, pluralType, cldr.locale, compiler.formatters
122125
)
123126
];
124127

src/message/formatter-fn.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ define([
33
], function( validateParameterTypeMessageVariables ) {
44

55
return function( formatter ) {
6+
formatter = formatter.apply( null, [].slice.call( arguments, 1 ) );
67
return function messageFormatter( variables ) {
78
if ( typeof variables === "number" || typeof variables === "string" ) {
89
variables = [].slice.call( arguments, 0 );

src/message/formatter-runtime-bind.js

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,35 @@
11
define(function() {
22

3-
return function( cldr, messageformatter, runtime, pluralType, locale, embeddedFormatters ) {
4-
var origToString = messageformatter.toString;
3+
return function( messageformatter, formatterSrc, runtime, pluralType, locale, formatters ) {
4+
var hasFormatters = formatters.length > 0;
55

66
messageformatter.toString = function() {
7-
var argNames, argValues, output,
8-
args = {};
9-
10-
// Properly adjust SlexAxton/messageformat.js compiled variables with Globalize variables:
11-
output = origToString.call( messageformatter );
7+
var locals = [];
8+
var argCount = 0,
9+
args = [];
1210

1311
if ( runtime.number ) {
14-
args.number = "messageFormat.number";
12+
locals.push( "var number = messageFormat.number;" );
1513
}
1614

1715
if ( runtime.plural ) {
18-
args.plural = "messageFormat.plural";
16+
locals.push( "var plural = messageFormat.plural;" );
1917
}
2018

2119
if ( runtime.select ) {
22-
args.select = "messageFormat.select";
23-
}
24-
25-
if ( embeddedFormatters.length ) {
26-
args.fmt = "[" + embeddedFormatters.map( function( fn ) {
27-
return fn.generatorString();
28-
} ).join( ", " ) + "]";
20+
locals.push( "var select = messageFormat.select;" );
2921
}
3022

3123
if ( pluralType !== false ) {
32-
args[locale] = "Globalize(\"" + locale + "\")." +
33-
"pluralGenerator( { type: \"" + pluralType + "\" } )";
24+
args.push( locale );
25+
argCount++;
3426
}
3527

36-
argNames = Object.keys( args ).join( ", " );
37-
argValues = Object.keys( args ).map(function( key ) {
38-
return args[ key ];
39-
}).join( ", " );
40-
41-
return "(function( " + argNames + " ) {\n" +
42-
" return " + output + "\n" +
43-
"})(" + argValues + ")";
28+
return "(function( " + args.join( ", " ) + " ) {\n" +
29+
( locals.length ? ( locals.join( "\n" ) + "\n" ) : "" ) +
30+
( hasFormatters ? " var fmt = [].slice.call( arguments, " + argCount + " );\n" : "" ) +
31+
" return " + formatterSrc + "\n" +
32+
"})";
4433
};
4534

4635
return messageformatter;

test/functional/message/message-formatter.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ QUnit.test( "should allow for runtime compilation", function( assert ) {
223223
function( runtimeArgs ) {
224224
assert.equal(
225225
runtimeArgs[ 0 ].toString(),
226-
"(function( ) {\n return function (d) { return \"Amen\"; }\n})()"
226+
"(function( ) {\n return function(d) { return \"Amen\"; }\n})"
227227
);
228228
}
229229
);
@@ -236,7 +236,7 @@ QUnit.test( "should allow for runtime compilation", function( assert ) {
236236
function( runtimeArgs ) {
237237
assert.equal(
238238
runtimeArgs[ 0 ].toString(),
239-
"(function( number, plural, en ) {\n return function (d) { return plural(d.count, 1, en, { \"0\": \"Be the first to like this\", \"1\": \"You liked this\", one: \"You and \" + d.someone + \" liked this\", other: \"You and \" + number(d.count, \"count\", 1) + \" others liked this\" }); }\n})(messageFormat.number, messageFormat.plural, Globalize(\"en\").pluralGenerator( { type: \"cardinal\" } ))"
239+
"(function( en ) {\nvar number = messageFormat.number;\nvar plural = messageFormat.plural;\n return function(d) { return plural(d.count, 1, en, { \"0\": \"Be the first to like this\", \"1\": \"You liked this\", one: \"You and \" + d.someone + \" liked this\", other: \"You and \" + number(d.count, \"count\", 1) + \" others liked this\" }); }\n})"
240240
);
241241
}
242242
);

0 commit comments

Comments
 (0)