forked from globalizejs/globalize
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Date: Dynamically augment skeleton (3/3)
Ref globalizejs#271 Closes globalizejs#604 Closes globalizejs#462
- Loading branch information
Showing
10 changed files
with
251 additions
and
219 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,4 +7,3 @@ before_install: | |
install: | ||
- npm install | ||
- bower install | ||
sudo: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
define([ | ||
"./normalize-pattern-type", | ||
"../pattern-re", | ||
"../../util/string/repeat" | ||
], function( dateExpandPatternNormalizePatternType, datePatternRe, stringRepeat ) { | ||
|
||
return function( requestedSkeleton, bestMatchFormat ) { | ||
var i, j, matchedType, matchedLength, requestedType, requestedLength, | ||
|
||
// Using an easier to read variable. | ||
normalizePatternType = dateExpandPatternNormalizePatternType; | ||
|
||
requestedSkeleton = requestedSkeleton.match( datePatternRe ); | ||
bestMatchFormat = bestMatchFormat.match( datePatternRe ); | ||
|
||
for ( i = 0; i < bestMatchFormat.length; i++ ) { | ||
matchedType = bestMatchFormat[i].charAt( 0 ); | ||
matchedLength = bestMatchFormat[i].length; | ||
for ( j = 0; j < requestedSkeleton.length; j++ ) { | ||
requestedType = requestedSkeleton[j].charAt( 0 ); | ||
requestedLength = requestedSkeleton[j].length; | ||
if ( normalizePatternType( matchedType ) === normalizePatternType( requestedType ) && | ||
matchedLength < requestedLength | ||
) { | ||
bestMatchFormat[i] = stringRepeat( matchedType, requestedLength ); | ||
} | ||
} | ||
} | ||
|
||
return bestMatchFormat.join( "" ); | ||
}; | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
define([ | ||
"./normalize-pattern-type", | ||
"../pattern-re" | ||
], function( dateExpandPatternNormalizePatternType, datePatternRe ) { | ||
|
||
return function( formatA, formatB ) { | ||
var distance, typeA, typeB, matchFound, i, j, | ||
|
||
// Using easier to read variables. | ||
normalizePatternType = dateExpandPatternNormalizePatternType; | ||
|
||
if ( formatA === formatB ) { | ||
return 0; | ||
} | ||
|
||
formatA = formatA.match( datePatternRe ); | ||
formatB = formatB.match( datePatternRe ); | ||
if ( formatA.length === formatB.length ) { | ||
distance = 1; | ||
for ( i = 0; i < formatA.length; i++ ) { | ||
typeA = normalizePatternType( formatA[i].charAt( 0 ) ); | ||
typeB = null; | ||
matchFound = false; | ||
for ( j = 0; j < formatB.length; j++ ) { | ||
typeB = normalizePatternType( formatB[j].charAt( 0 ) ); | ||
if ( typeA === typeB ) { | ||
break; | ||
} else { | ||
typeB = null; | ||
} | ||
} | ||
if ( typeB === null ) { | ||
return -1; | ||
} | ||
distance = distance + Math.abs( formatA[i].length - formatB[j].length ); | ||
if ( formatA[i].charAt( 0 ) !== formatB[j].charAt( 0 ) ) { | ||
distance = distance + 1; | ||
} | ||
} | ||
return distance; | ||
} | ||
return -1; | ||
}; | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
define([ | ||
"./augment-format", | ||
"./compare-formats" | ||
], function( dateExpandPatternAugmentFormat, dateExpandPatternCompareFormats ) { | ||
|
||
return function( cldr, path, skeleton ) { | ||
var availableFormats, format, pattern, ratedFormats, | ||
|
||
// Using easier to read variables. | ||
augmentFormat = dateExpandPatternAugmentFormat, | ||
compareFormats = dateExpandPatternCompareFormats; | ||
|
||
pattern = cldr.main([ path, skeleton ]); | ||
|
||
if ( skeleton && !pattern ) { | ||
availableFormats = cldr.main([ path ]); | ||
ratedFormats = []; | ||
|
||
for ( format in availableFormats ) { | ||
ratedFormats.push({ | ||
format: format, | ||
pattern: availableFormats[format], | ||
rate: compareFormats( skeleton, format ) | ||
}); | ||
} | ||
|
||
ratedFormats = ratedFormats | ||
.filter( function( format ) { | ||
return format.rate > -1; | ||
} ) | ||
.sort( function( formatA, formatB ) { | ||
return formatA.rate - formatB.rate; | ||
}); | ||
|
||
if ( ratedFormats.length ) { | ||
pattern = augmentFormat( skeleton, ratedFormats[0].pattern ); | ||
} | ||
} | ||
|
||
return pattern; | ||
}; | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
define([ | ||
"./similar-fields-map" | ||
], function( dateExpandPatternSimilarFieldsMap ) { | ||
|
||
return function( character ) { | ||
return dateExpandPatternSimilarFieldsMap[ character ] || character; | ||
}; | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
define([ | ||
"../../util/object/invert" | ||
], function( objectInvert ) { | ||
|
||
// Invert key and values, e.g., {"e": "eEc"} ==> {"e": "e", "E": "e", "c": "e"}. | ||
return objectInvert({ | ||
"e": "eEc", | ||
"L": "ML" | ||
}, function( object, key, value ) { | ||
value.split( "" ).forEach(function( field ) { | ||
object[ field ] = key; | ||
}); | ||
return object; | ||
}); | ||
|
||
}); |
Oops, something went wrong.