Skip to content
This repository was archived by the owner on Apr 14, 2021. It is now read-only.

Commit 20f322d

Browse files
committed
fixes #190: add support for single configuration option by hardwiring some of the configuration missing from CLDR availableFormats collection.
1 parent 0aa4f48 commit 20f322d

File tree

2 files changed

+76
-1
lines changed

2 files changed

+76
-1
lines changed

src/12.datetimeformat.js

+25
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
} from "./11.numberformat.js";
2222

2323
import {
24+
generateSyntheticFormat,
2425
createDateTimeFormats
2526
} from "./cldr";
2627

@@ -607,6 +608,30 @@ function BasicFormatMatcher (options, formats) {
607608
* not expect to see the returned format containing narrow, short or long part names
608609
*/
609610
function BestFitFormatMatcher (options, formats) {
611+
/** Diverging: this block implements the hack for single property configuration, eg.:
612+
*
613+
* `new Intl.DateTimeFormat('en', {day: 'numeric'})`
614+
*
615+
* should produce a single digit with the day of the month. This is needed because
616+
* CLDR `availableFormats` data structure doesn't cover these cases.
617+
*/
618+
{
619+
const optionsPropNames = [];
620+
for (let property in dateTimeComponents) {
621+
if (!hop.call(dateTimeComponents, property))
622+
continue;
623+
624+
if (options['[['+ property +']]'] !== undefined) {
625+
optionsPropNames.push(property);
626+
}
627+
}
628+
if (optionsPropNames.length === 1) {
629+
const bestFormat = generateSyntheticFormat(optionsPropNames[0], options['[['+ optionsPropNames[0] +']]']);
630+
if (bestFormat) {
631+
return bestFormat;
632+
}
633+
}
634+
}
610635

611636
// 1. Let removalPenalty be 120.
612637
let removalPenalty = 120;

src/cldr.js

+51-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ let expPatternTrimmer = /^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g;
99
// timezone, weekday, amoung others
1010
let unwantedDTCs = /[rqQASjJgwWIQq]/; // xXVO were removed from this list in favor of computing matches with timeZoneName values but printing as empty string
1111

12-
let dtKeys = ["weekday", "era", "year", "month", "day", "weekday", "quarter"];
12+
let dtKeys = ["era", "year", "month", "day", "weekday", "quarter"];
1313
let tmKeys = ["hour", "minute", "second", "hour12", "timeZoneName"];
1414

1515
function isDateFormatOnly(obj) {
@@ -306,3 +306,53 @@ export function createDateTimeFormats(formats) {
306306

307307
return result;
308308
}
309+
310+
// this represents the exceptions of the rule that are not covered by CLDR availableFormats
311+
// for single property configurations, they play no role when using multiple properties, and
312+
// those that are not in this table, are not exceptions or are not covered by the data we
313+
// provide.
314+
const validSyntheticProps = {
315+
second: {
316+
numeric: 's',
317+
'2-digit': 'ss'
318+
},
319+
minute: {
320+
numeric: 'm',
321+
'2-digit': 'mm'
322+
},
323+
year: {
324+
numeric: 'y',
325+
'2-digit': 'yy'
326+
},
327+
day: {
328+
numeric: 'd',
329+
'2-digit': 'dd'
330+
},
331+
month: {
332+
numeric: 'L',
333+
'2-digit': 'LL',
334+
narrow: 'LLLLL',
335+
short: 'LLL',
336+
long: 'LLLL'
337+
},
338+
weekday: {
339+
narrow: 'ccccc',
340+
short: 'ccc',
341+
long: 'cccc'
342+
}
343+
};
344+
345+
export function generateSyntheticFormat(propName, propValue) {
346+
if (validSyntheticProps[propName] && validSyntheticProps[propName][propValue]) {
347+
return {
348+
originalPattern: validSyntheticProps[propName][propValue],
349+
_: {
350+
[propName]: propValue
351+
},
352+
extendedPattern: `{${propName}}`,
353+
[propName]: propValue,
354+
pattern12: `{${propName}}`,
355+
pattern: `{${propName}}`
356+
};
357+
}
358+
}

0 commit comments

Comments
 (0)