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

Commit ee2dec6

Browse files
committed
Allow expanding of components in selected availableFormats
e.g. If a requested pattern is yMMMMd, but only yMMMd exists in the availableFormats array, yMMMd is used, but the number of M symbols in the pattern is expanded to match the requested format. This is in accordance with the LDML specification: http://www.unicode.org/reports/tr35/tr35-dates.html#availableFormats_appendItems
1 parent 878c801 commit ee2dec6

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

tools/Ldml2Json.js

+24-3
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,10 @@ function processObj(data) {
379379
avail = defCa.dateTimeFormats.availableFormats,
380380
order = defCa.dateTimeFormats.medium,
381381
verify = function (frmt) {
382-
return (!frmt[0] || avail[frmt[0]]) && (!frmt[1] || avail[frmt[1]]);
382+
// Unicode LDML spec allows us to expand some pattern components to suit
383+
var dFrmt = frmt[1] && frmt[1].replace(/M{4,5}/, 'MMM').replace(/E{4,6}/, 'E');
384+
385+
return (!frmt[0] || avail[frmt[0]]) && (!dFrmt || avail[dFrmt]);
383386
};
384387

385388
// Make sure every local supports these minimum required formats
@@ -388,10 +391,28 @@ function processObj(data) {
388391

389392
// Map the formats into a pattern for createDateTimeFormats
390393
ret.date.formats = formats.map(function (frmt) {
394+
var M, E, dFrmt;
395+
396+
// Expand component lengths if necessary, as allowed in the LDML spec
397+
if (frmt[1]) {
398+
// Get the lengths of 'M' and 'E' substrings in the date pattern
399+
// as arrays that can be joined to create a new substring
400+
M = new Array((frmt[1].match(/M/g)||[]).length + 1);
401+
E = new Array((frmt[1].match(/E/g)||[]).length + 1);
402+
403+
dFrmt = avail[frmt[1].replace(/M{4,5}/, 'MMM').replace(/E{4,6}/, 'E')];
404+
405+
if (M.length > 2)
406+
dFrmt = dFrmt.replace(/(M|L)+/, M.join('$1'));
407+
408+
if (E.length > 2)
409+
dFrmt = dFrmt.replace(/([Eec])+/, E.join('$1'));
410+
}
411+
391412
return createDateTimeFormat(
392413
order
393-
.replace('{0}', frmt[0] ? avail[frmt[0]] : '')
394-
.replace('{1}', frmt[1] ? avail[frmt[1]] : '')
414+
.replace('{0}', avail[frmt[0]] || '')
415+
.replace('{1}', dFrmt || '')
395416
.replace(/^[^a-z0-9]+|[^a-z0-9]+$/gi, '')
396417
);
397418
});

0 commit comments

Comments
 (0)