Skip to content

(@fluent/bundle) Forbid setting NUMBER's currency #464

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 44 additions & 5 deletions fluent-bundle/src/builtins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,30 @@ import {
FluentDateTime
} from "./types.js";

function values(opts: Record<string, FluentValue>): Record<string, unknown> {
const unwrapped: Record<string, unknown> = {};
function values(
opts: Record<string, FluentValue>,
allowed: Array<string>
): Record<string, unknown> {
const unwrapped: Record<string, unknown> = Object.create(null);
for (const [name, opt] of Object.entries(opts)) {
unwrapped[name] = opt.valueOf();
if (allowed.includes(name)) {
unwrapped[name] = opt.valueOf();
}
}
return unwrapped;
}

const NUMBER_ALLOWED = [
"unitDisplay",
"currencyDisplay",
"useGrouping",
"minimumIntegerDigits",
"minimumFractionDigits",
"maximumFractionDigits",
"minimumSignificantDigits",
"maximumSignificantDigits",
];

export function NUMBER(
args: Array<FluentValue>,
opts: Record<string, FluentValue>
Expand All @@ -37,12 +53,32 @@ export function NUMBER(
}

if (arg instanceof FluentNumber || arg instanceof FluentDateTime) {
return new FluentNumber(arg.valueOf(), { ...arg.opts, ...values(opts) });
return new FluentNumber(arg.valueOf(), {
...arg.opts,
...values(opts, NUMBER_ALLOWED)
});
}

throw new TypeError("Invalid argument to NUMBER");
}

const DATETIME_ALLOWED = [
"dateStyle",
"timeStyle",
"fractionalSecondDigits",
"dayPeriod",
"hour12",
"weekday",
"era",
"year",
"month",
"day",
"hour",
"minute",
"second",
"timeZoneName",
];

export function DATETIME(
args: Array<FluentValue>,
opts: Record<string, FluentValue>
Expand All @@ -54,7 +90,10 @@ export function DATETIME(
}

if (arg instanceof FluentNumber || arg instanceof FluentDateTime) {
return new FluentDateTime(arg.valueOf(), { ...arg.opts, ...values(opts) });
return new FluentDateTime(arg.valueOf(), {
...arg.opts,
...values(opts, DATETIME_ALLOWED)
});
}

throw new TypeError("Invalid argument to DATETIME");
Expand Down
35 changes: 22 additions & 13 deletions fluent-bundle/test/arguments_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import ftl from "@fluent/dedent";

import {FluentBundle} from '../esm/bundle';
import {FluentResource} from '../esm/resource';
import {FluentType} from '../esm/types';
import {FluentType, FluentNumber, FluentDateTime} from '../esm/types';

suite('Variables', function() {
let bundle, errs;
Expand Down Expand Up @@ -171,45 +171,54 @@ suite('Variables', function() {
});

suite('and numbers', function(){
let args;

suiteSetup(function() {
bundle = new FluentBundle('en-US', { useIsolating: false });
bundle.addResource(new FluentResource(ftl`
foo = { $arg }
`));
args = {
arg: 1
};
});

test('can be a number', function(){
const msg = bundle.getMessage('foo');
const val = bundle.formatPattern(msg.value, args, errs);
const val = bundle.formatPattern(msg.value, {arg: 1}, errs);
assert.strictEqual(val, '1');
assert.strictEqual(errs.length, 0);
});

test('can be a FluentNumber', function(){
const arg = new FluentNumber(1, {minimumFractionDigits: 2});
const msg = bundle.getMessage('foo');
const val = bundle.formatPattern(msg.value, {arg}, errs);
assert.strictEqual(val, '1.00');
assert.strictEqual(errs.length, 0);
});
});

suite('and dates', function(){
let args, dtf;
let dtf;

suiteSetup(function() {
dtf = new Intl.DateTimeFormat('en-US');
bundle = new FluentBundle('en-US', { useIsolating: false });
bundle.addResource(new FluentResource(ftl`
foo = { $arg }
`));
args = {
arg: new Date('2016-09-29')
};
});

test('can be a date', function(){
const arg = new Date('2016-09-29');
const msg = bundle.getMessage('foo');
const val = bundle.formatPattern(msg.value, args, errs);
const val = bundle.formatPattern(msg.value, {arg}, errs);
// format the date argument to account for the testrunner's timezone
assert.strictEqual(val, dtf.format(args.arg));
assert.strictEqual(val, dtf.format(arg));
assert.strictEqual(errs.length, 0);
});

test('can be a FluentDateTime', function(){
const arg = new FluentDateTime(new Date('2016-09-29'), {weekday: "long"});
const msg = bundle.getMessage('foo');
const val = bundle.formatPattern(msg.value, {arg}, errs);
assert.strictEqual(val, 'Thursday');
assert.strictEqual(errs.length, 0);
});
});
Expand Down
Loading