Skip to content

Commit

Permalink
Dynamically augment availableFormats
Browse files Browse the repository at this point in the history
  • Loading branch information
vectart committed Jul 15, 2015
1 parent fe14f5b commit 049db10
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 12 deletions.
112 changes: 100 additions & 12 deletions src/date/expand-pattern.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
define([
"../common/format-message",
"../common/create-error/invalid-parameter-value"
], function( formatMessage, createErrorInvalidParameterValue ) {
"../common/create-error/invalid-parameter-value",
"./pattern-re"
], function( formatMessage, createErrorInvalidParameterValue, datePatternRe ) {

/**
* expandPattern( options, cldr )
Expand All @@ -26,7 +27,7 @@ define([
*/

return function( options, cldr ) {
var dateSkeleton, result, skeleton, timeSkeleton, type;
var dateSkeleton, result, skeleton, timeSkeleton, type, pattern;

function combineDateTime( type, datePattern, timePattern ) {
return formatMessage(
Expand All @@ -38,6 +39,89 @@ return function( options, cldr ) {
);
}

function getBestMatchPattern( path, skeleton ) {
var availableFormats, ratedFormats, format;

pattern = cldr.main([ path, skeleton ]);

if (skeleton && pattern === undefined) {
availableFormats = cldr.main([ path ]);
ratedFormats = [];

for (format in availableFormats) {
ratedFormats.push({
format: format,
pattern: availableFormats[format],
rate: compareFormats(skeleton, format)
});
}

ratedFormats.sort(function(a, b) {
return a.rate - b.rate;
});

if (ratedFormats.length) {
pattern = augmentFormat(skeleton, ratedFormats[0].pattern);
}
}

return pattern;
}

function compareFormats( a, b ) {
var distance, minLength, i;

distance = 1;
a = a.match(datePatternRe);
b = b.match(datePatternRe);
minLength = Math.min(a.length, b.length);

for (i = 0; i < minLength; i++) {
if (a[i].charAt(0) === b[i].charAt(0)) {
if (a[i].length === b[i].length) {
distance *= 0.25;
} else {
distance *= 0.75;
}
} else {
distance *= 1.25;
}
}

if (a.length === b.length) {
distance *= 0.5;
}

return distance;
}

function augmentFormat( requestedSkeleton, bestMatchFormat ) {
var originalBestMatchFormat, i, t, j, k, l;

originalBestMatchFormat = bestMatchFormat;
requestedSkeleton = requestedSkeleton.match(datePatternRe);
bestMatchFormat = bestMatchFormat.match(datePatternRe);

for (i = 0, l = bestMatchFormat.length; i < l; i++) {
t = bestMatchFormat[i].charAt(0);

for (j = 0, k = requestedSkeleton.length; j < k; j++) {
if (t === requestedSkeleton[j].charAt(0)) {
bestMatchFormat[i] = requestedSkeleton[j];
break;
}
}
}

bestMatchFormat = bestMatchFormat.join("");

if (bestMatchFormat === originalBestMatchFormat) {
bestMatchFormat = requestedSkeleton.join("");
}

return bestMatchFormat;
}

switch ( true ) {
case "skeleton" in options:
skeleton = options.skeleton;
Expand All @@ -57,16 +141,20 @@ return function( options, cldr ) {
} else {
type = "short";
}
result = combineDateTime( type,
cldr.main([
"dates/calendars/gregorian/dateTimeFormats/availableFormats",
dateSkeleton
]),
cldr.main([
"dates/calendars/gregorian/dateTimeFormats/availableFormats",
timeSkeleton
])
dateSkeleton = getBestMatchPattern(
"dates/calendars/gregorian/dateTimeFormats/availableFormats",
dateSkeleton
);
timeSkeleton = getBestMatchPattern(
"dates/calendars/gregorian/dateTimeFormats/availableFormats",
timeSkeleton
);

if (dateSkeleton && timeSkeleton) {
result = combineDateTime( type, dateSkeleton, timeSkeleton);
} else {
result = dateSkeleton || timeSkeleton;
}
}
break;

Expand Down
9 changes: 9 additions & 0 deletions test/functional/date/date-formatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,13 @@ QUnit.test( "should return a formatter", function( assert ) {
assert.equal( Globalize.dateFormatter({ skeleton: "yQQQhm" })( date ), "Q3 2010, 5:35 PM" );
});

QUnit.test( "should augment a skeleton", function( assert ) {
extraSetup();

assert.equal( Globalize.dateFormatter({ skeleton: "yMMMMd" })( date ), "September 15, 2010" );
assert.equal( Globalize.dateFormatter({ skeleton: "MMMMd" })( date ), "September 15" );
assert.equal( Globalize.dateFormatter({ skeleton: "MMMM" })( date ), "September" );
assert.equal( Globalize.dateFormatter({ skeleton: "EEEE" })( date ), "Wednesday" );
});

});
7 changes: 7 additions & 0 deletions test/unit/date/expand-pattern.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,11 @@ QUnit.test( "should expand {raw: \"<pattern>\"}", function( assert ) {
assert.equal( expandPattern( { raw: "MMM d" }, en ), "MMM d" );
});

QUnit.test( "should augment {skeleton: \"<skeleton>\"}", function( assert ) {
assert.equal( expandPattern( { skeleton: "yMMMMd" }, de ), "d. MMMM y" );
assert.equal( expandPattern( { skeleton: "MMMMd" }, de ), "d. MMMM" );
assert.equal( expandPattern( { skeleton: "MMMM" }, de ), "MMMM" );
assert.equal( expandPattern( { skeleton: "EEEE" }, de ), "EEEE" );
});

});

0 comments on commit 049db10

Please sign in to comment.