Skip to content

Commit 24355cf

Browse files
committed
Handle arguments of all types in built-ins
1 parent d9dfd07 commit 24355cf

File tree

3 files changed

+104
-18
lines changed

3 files changed

+104
-18
lines changed

fluent/src/builtins.js

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,7 @@
1111
* `FluentType`. Functions must return `FluentType` objects as well.
1212
*/
1313

14-
import { FluentNumber, FluentDateTime } from "./types.js";
15-
16-
export default {
17-
"NUMBER": ([arg], opts) =>
18-
new FluentNumber(arg.valueOf(), merge(arg.opts, opts)),
19-
"DATETIME": ([arg], opts) =>
20-
new FluentDateTime(arg.valueOf(), merge(arg.opts, opts)),
21-
};
14+
import { FluentNone, FluentNumber, FluentDateTime } from "./types.js";
2215

2316
function merge(argopts, opts) {
2417
return Object.assign({}, argopts, values(opts));
@@ -31,3 +24,25 @@ function values(opts) {
3124
}
3225
return unwrapped;
3326
}
27+
28+
export
29+
function NUMBER([arg], opts) {
30+
if (arg instanceof FluentNone) {
31+
return arg;
32+
}
33+
if (arg instanceof FluentNumber) {
34+
return new FluentNumber(arg.valueOf(), merge(arg.opts, opts));
35+
}
36+
return new FluentNone("NUMBER()");
37+
}
38+
39+
export
40+
function DATETIME([arg], opts) {
41+
if (arg instanceof FluentNone) {
42+
return arg;
43+
}
44+
if (arg instanceof FluentDateTime) {
45+
return new FluentDateTime(arg.valueOf(), merge(arg.opts, opts));
46+
}
47+
return new FluentNone("DATETIME()");
48+
}

fluent/src/resolver.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737

3838
import { FluentType, FluentNone, FluentNumber, FluentDateTime }
3939
from "./types.js";
40-
import builtins from "./builtins.js";
40+
import * as builtins from "./builtins.js";
4141

4242
// Prevent expansion of too long placeables.
4343
const MAX_PLACEABLE_LENGTH = 2500;

fluent/test/functions_builtin_test.js

Lines changed: 80 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ suite('Built-in functions', function() {
2222
let msg;
2323

2424
msg = bundle.getMessage('num-decimal');
25-
assert.equal(bundle.format(msg), 'NaN');
25+
assert.equal(bundle.format(msg), '{$arg}');
2626

2727
msg = bundle.getMessage('num-percent');
28-
assert.equal(bundle.format(msg), 'NaN');
28+
assert.equal(bundle.format(msg), '{$arg}');
2929

3030
msg = bundle.getMessage('num-bad-opt');
31-
assert.equal(bundle.format(msg), 'NaN');
31+
assert.equal(bundle.format(msg), '{$arg}');
3232
});
3333

3434
test('number argument', function() {
@@ -50,13 +50,42 @@ suite('Built-in functions', function() {
5050
let msg;
5151

5252
msg = bundle.getMessage('num-decimal');
53-
assert.equal(bundle.format(msg, args), 'NaN');
53+
assert.equal(bundle.format(msg, args), '{NUMBER()}');
5454

5555
msg = bundle.getMessage('num-percent');
56-
assert.equal(bundle.format(msg, args), 'NaN');
56+
assert.equal(bundle.format(msg, args), '{NUMBER()}');
5757

5858
msg = bundle.getMessage('num-bad-opt');
59-
assert.equal(bundle.format(msg, args), 'NaN');
59+
assert.equal(bundle.format(msg, args), '{NUMBER()}');
60+
});
61+
62+
test('date argument', function() {
63+
const date = new Date('2016-09-29');
64+
const args = {arg: date};
65+
let msg;
66+
67+
msg = bundle.getMessage('num-decimal');
68+
assert.equal(bundle.format(msg, args), '{NUMBER()}');
69+
70+
msg = bundle.getMessage('num-percent');
71+
assert.equal(bundle.format(msg, args), '{NUMBER()}');
72+
73+
msg = bundle.getMessage('num-bad-opt');
74+
assert.equal(bundle.format(msg, args), '{NUMBER()}');
75+
});
76+
77+
test('invalid argument', function() {
78+
const args = {arg: []};
79+
let msg;
80+
81+
msg = bundle.getMessage('num-decimal');
82+
assert.equal(bundle.format(msg, args), '{$arg}');
83+
84+
msg = bundle.getMessage('num-percent');
85+
assert.equal(bundle.format(msg, args), '{$arg}');
86+
87+
msg = bundle.getMessage('num-bad-opt');
88+
assert.equal(bundle.format(msg, args), '{$arg}');
6089
});
6190
});
6291

@@ -74,13 +103,13 @@ suite('Built-in functions', function() {
74103
let msg;
75104

76105
msg = bundle.getMessage('dt-default');
77-
assert.equal(bundle.format(msg), 'Invalid Date');
106+
assert.equal(bundle.format(msg), '{$arg}');
78107

79108
msg = bundle.getMessage('dt-month');
80-
assert.equal(bundle.format(msg), 'Invalid Date');
109+
assert.equal(bundle.format(msg), '{$arg}');
81110

82111
msg = bundle.getMessage('dt-bad-opt');
83-
assert.equal(bundle.format(msg), 'Invalid Date');
112+
assert.equal(bundle.format(msg), '{$arg}');
84113
});
85114

86115
test('Date argument', function () {
@@ -107,5 +136,47 @@ suite('Built-in functions', function() {
107136
// Thu Sep 29 2016 02:00:00 GMT+0200 (CEST)
108137
assert.equal(bundle.format(msg, args), date.toString());
109138
});
139+
140+
test('number argument', function() {
141+
let args = {arg: 1};
142+
let msg;
143+
144+
msg = bundle.getMessage('dt-default');
145+
assert.equal(bundle.format(msg, args), '{DATETIME()}');
146+
147+
msg = bundle.getMessage('dt-month');
148+
assert.equal(bundle.format(msg, args), '{DATETIME()}');
149+
150+
msg = bundle.getMessage('dt-bad-opt');
151+
assert.equal(bundle.format(msg, args), '{DATETIME()}');
152+
});
153+
154+
test('string argument', function() {
155+
let args = {arg: 'Foo'};
156+
let msg;
157+
158+
msg = bundle.getMessage('dt-default');
159+
assert.equal(bundle.format(msg, args), '{DATETIME()}');
160+
161+
msg = bundle.getMessage('dt-month');
162+
assert.equal(bundle.format(msg, args), '{DATETIME()}');
163+
164+
msg = bundle.getMessage('dt-bad-opt');
165+
assert.equal(bundle.format(msg, args), '{DATETIME()}');
166+
});
167+
168+
test('invalid argument', function() {
169+
let args = {arg: []};
170+
let msg;
171+
172+
msg = bundle.getMessage('dt-default');
173+
assert.equal(bundle.format(msg, args), '{$arg}');
174+
175+
msg = bundle.getMessage('dt-month');
176+
assert.equal(bundle.format(msg, args), '{$arg}');
177+
178+
msg = bundle.getMessage('dt-bad-opt');
179+
assert.equal(bundle.format(msg, args), '{$arg}');
180+
});
110181
});
111182
});

0 commit comments

Comments
 (0)