Permalink
Browse files

Several fixes, now fails 13/540

  • Loading branch information...
1 parent 8cfba09 commit a09488d2afa667bd9316f01abed1afd1fc35d40d @therazor committed May 18, 2012
Showing with 154 additions and 130 deletions.
  1. +46 −30 lib/globalize.js
  2. +107 −0 test/customFormat.js
  3. +0 −100 test/format.js
  4. +1 −0 test/index.html
View
@@ -705,11 +705,22 @@ formatDate = function( value, format, culture ) {
groupSizes = formatInfo.groupSizes,
curSize = groupSizes[ 0 ],
curGroupIndex = 1,
- stringIndex = numberString.length - 1,
+ stringIndex,
sep = formatInfo[ "," ],
ret = "";
- padToLength = Math.max( numberString.length, padToLength );
+ if ( padToLength > numberString.length ) {
+ var isNegative;
+ if ( numberString.indexOf(0) === '-' ) {
+ isNegative = true;
+ numberString = numberString.slice( 1 );
+ }
+ numberString = zeroPad( numberString, padToLength, true );
+ if ( isNegative )
+ numberString = '-' + numberString;
+ }
+
+ stringIndex = numberString.length - 1;
while ( stringIndex >= 0 ) {
if ( curSize === 0 || curSize > stringIndex ) {
@@ -811,13 +822,13 @@ formatDate = function( value, format, culture ) {
textualSubpatterns = [],
subpatternContainer,
subpattern = '',
- decimalsSeparatorIndex = -1;
+ decimalsSeparatorIndex = -1,
+ uniqueCharsFound = [];
for (i = 0; i < len; i++) {
- var isEscaped,
- uniqueCharsFound = [];
+ var isEscaped;
- if ( pattern[i] == '\\' ) {
+ if ( pattern.charAt(i) == '\\' ) {
if ( i + 1 === len )
break;
@@ -827,7 +838,7 @@ formatDate = function( value, format, culture ) {
isEscaped = false;
// TODO /[.,#%‰E0-9]/i
- if ( isEscaped || /[^.,#0]/.test( pattern[i] ) ) {
+ if ( isEscaped || /[^.,#0]/.test( pattern.charAt(i) ) ) {
subpatternContainer = textualSubpatterns;
} else {
subpatternContainer = subpatterns;
@@ -838,25 +849,25 @@ formatDate = function( value, format, culture ) {
// implicit decimal point, the number to be formatted is divided
// by 1000 for each comma. For example, if the string "0,,"
// is used to format the number 100 million, the output is "100".
- if ( /[.,]/.test( pattern[i] ) ) {
- if ( ~arrayIndexOf( uniqueCharsFound, pattern[i] ) ||
- ( pattern[i] == ',' && ~arrayIndexOf( uniqueCharsFound, '.' ) ) )
+ if ( /[.,]/.test( pattern.charAt(i) ) ) {
+ if ( ~arrayIndexOf( uniqueCharsFound, pattern.charAt(i) ) ||
+ ( pattern.charAt(i) == ',' && ~arrayIndexOf( uniqueCharsFound, '.' ) ) )
continue;
- else if ( pattern[i] == '.' )
+ else if ( pattern.charAt(i) == '.' )
decimalsSeparatorIndex = subpatterns.length;
- uniqueCharsFound.push( pattern[i] );
+ uniqueCharsFound.push( pattern.charAt(i) );
}
}
if ( i === 0 )
firstSubpatternContainer = lastSubpatternContainer = subpatternContainer;
if ( subpatternContainer === lastSubpatternContainer )
- subpattern += pattern[i];
+ subpattern += pattern.charAt(i);
else {
lastSubpatternContainer.push( subpattern );
- subpattern = pattern[i];
+ subpattern = pattern.charAt(i);
}
lastSubpatternContainer = subpatternContainer;
@@ -868,12 +879,10 @@ formatDate = function( value, format, culture ) {
return textualSubpatterns[0];
var fullPattern = subpatterns.join( '' ),
- intDecPatterns = fullPattern.split( '.' ),
- hasThousandsSeparator = intDecPatterns[0].indexOf( ',' ) > 0,
- intMinLength, // [ min, max ] - applies to decimalsLengthRange as well
- decimalsLengthRange;
-
- intDecPatterns[0] = intDecPatterns[0].replace( ',', '' );
+ intDecPatterns = fullPattern.replace( ',', '' ).split( '.' ),
+ hasThousandsSeparator = fullPattern.split( '.' ).shift().indexOf(',') > 0,
+ intMinLength,
+ decimalsLengthRange; // [ min, max ]
if ( ~( intMinLength = intDecPatterns[0].indexOf( '0' ) ) )
intMinLength = intDecPatterns[0].length - intMinLength;
@@ -888,7 +897,7 @@ formatDate = function( value, format, culture ) {
decimalsLengthRange = [ 0, 0 ];
var intAndDecimalsValue = value.toString().split('.'),
- precision = intAndDecimalsValue.length && ~decimalsSeparatorIndex ?
+ precision = intAndDecimalsValue.length > 1 && ~decimalsSeparatorIndex ?
intAndDecimalsValue[1].length < decimalsLengthRange[1] ?
intAndDecimalsValue[1].length : decimalsLengthRange[1]
: 0,
@@ -901,15 +910,18 @@ formatDate = function( value, format, culture ) {
var formattedString = '',
formattedValue = [],
- roundedString = rounded.toString(),
+ roundedString = Math.abs( rounded ).toString(),
curSubpatterns = firstSubpatternContainer;
if ( hasThousandsSeparator )
- formattedValue.push( expandInt( truncate( rounded ), intMinLength, formatInfo ) );
- else
+ formattedValue.push( expandInt( rounded, intMinLength, formatInfo ) );
+ else {
formattedValue.push( zeroPad( roundedString.split('.').shift(), intMinLength, true ) );
+ }
+ if ( rounded < 0 )
+ formattedValue[0] = '-' + formattedValue[0];
- if ( ~decimalsSeparatorIndex ) {
+ if ( ~decimalsSeparatorIndex && intDecPatterns.length > 1 && intDecPatterns[1] !== '' ) {
var splitRounded = roundedString.split('.'),
roundedDecimals = splitRounded.length > 1 ? splitRounded[1] : '',
decimalPlaces;
@@ -926,10 +938,14 @@ formatDate = function( value, format, culture ) {
formattedValue = formattedValue.join( formatInfo[ "." ] );
- var currentFragmentValue,
- intSeparators = 0;
+ var currentFragmentValue,
+ intSeparators = 0
+ curDecimalPlaces = decimalPlaces;
+
for ( i = subpatterns.length - 1; i > 0; i-- ) {
- if ( !hasThousandsSeparator || i > decimalsSeparatorIndex )
+ if ( i < decimalsSeparatorIndex && curDecimalPlaces < decimalsLengthRange[1] && false )
+ currentFragmentValue = '';
+ else if ( !hasThousandsSeparator || i > decimalsSeparatorIndex )
currentFragmentValue = formattedValue.slice( -subpatterns[i].length );
else {
if ( decimalsSeparatorIndex === i ) {
@@ -960,7 +976,7 @@ formatDate = function( value, format, culture ) {
// assemble the already made pieces together in the correct order
while ( curSubpatterns.length ) {
- formattedString += curSubpatterns.pop();
+ formattedString += curSubpatterns.shift();
if ( curSubpatterns === textualSubpatterns )
curSubpatterns = subpatterns;
@@ -982,7 +998,7 @@ formatDate = function( value, format, culture ) {
return culture.numberFormat[ "NaN" ];
}
if ( !format || format === "i" ) {
- return culture.name.length ? value.toLocaleString() : value.toString();
+ return value.toString();
}
format = format || "D";
View
@@ -0,0 +1,107 @@
+test("Number Formatting - custom w/ 0 padding", function() {
+ equal( Globalize.format(1234.567, "0"), "1235" );
+ equal( Globalize.format(1234.567, ",0"), "1235" );
+ equal( Globalize.format(1234.567, "00"), "1235" );
+ equal( Globalize.format(1234.567, ".00"), "1234.57" );
+ equal( Globalize.format(1234.567, "00000"), "01235" );
+ equal( Globalize.format(1234.567, "000.00"), "1234.57" );
+ equal( Globalize.format(1234.123, "000.00"), "1234.12" );
+ equal( Globalize.format(-1234.567, "00000"), "-01235" );
+ equal( Globalize.format(-1234.567, "000.00"), "-1234.57" );
+ equal( Globalize.format(2.34e12, "000.00"), "2340000000000.00" );
+ equal( Globalize.format(1234.567, "0.0"), "1234.6" );
+ equal( Globalize.format(1234.567, "0.00"), "1234.57" );
+ equal( Globalize.format(1234.567, "0.000"), "1234.567" );
+ equal( Globalize.format(1234.567, "0.0000"), "1234.5670" );
+ equal( Globalize.format(1234.567, "0,0.00"), "1,234.57" );
+ equal( Globalize.format(1234.567, "0000000.00"), "0001234.57" );
+ equal( Globalize.format(1234.567, "000000,0.00"), "0,001,234.57" );
+ equal( Globalize.format(1234.567, "0,000,000.00"), "0,001,234.57" );
+ equal( Globalize.format(1234.567, "0,0,0,0.00"), "1,234.57" );
+ equal( Globalize.format(1234.567, "0,0,0,0,0.00"), "01,234.57" );
+});
+
+test("Number Formatting - custom w/ #", function() {
+ equal( Globalize.format(1234.567, "#"), "1235" );
+ equal( Globalize.format(1234.567, "#."), "1235" );
+ equal( Globalize.format(1234.567, ",#"), "1235" );
+ equal( Globalize.format(1234.567, "##"), "1235" );
+ equal( Globalize.format(1234.567, ".##"), "1234.57" );
+ equal( Globalize.format(1234.567, "#####"), "1235" );
+ equal( Globalize.format(1234.567, "###.##"), "1234.57" );
+ equal( Globalize.format(-1234.567, "#####"), "-1235" );
+ equal( Globalize.format(-1234.567, "###.##"), "-1234.57" );
+ equal( Globalize.format(1234.567, "#.#"), "1234.6" );
+ equal( Globalize.format(1234.567, "#.##"), "1234.57" );
+ equal( Globalize.format(1234.567, "#.###"), "1234.567" );
+ equal( Globalize.format(1234.567, "#.####"), "1234.567" );
+ equal( Globalize.format(1234.567, "#,#.##"), "1,234.57" );
+ equal( Globalize.format(1234.567, "#,#,#.##"), "1,234.57" );
+});
+
+test("Number Formatting - custom w/ mixed # and 0", function() {
+ equal( Globalize.format(1234.567, "#00000"), "01235" );
+ equal( Globalize.format(1234.567, "#,##0"), "1,235" );
+ equal( Globalize.format(1234.567, "#,##0.00"), "1,234.57" );
+ equal( Globalize.format(1234.567, "###.#0"), "1234.57" );
+ equal( Globalize.format(1234.567, "###.0#"), "1234.57" );
+ equal( Globalize.format(1234.567, "#,###.00"), "1,234.57" );
+ equal( Globalize.format(1234.567, "#,0.0#"), "1,234.57" );
+ equal( Globalize.format(1234.567, "0,#,#,0.0#"), "1,234.57" );
+ equal( Globalize.format(-0.59, '#,##0.00'), "-0.59" );
+ equal( Globalize.format(-2.2, '#,##0.00##'), "-2.20" );
+});
+
+test("Number Formatting - custom w/ mixed # and 0 and integers", function() {
+ equal( Globalize.format(1234, "0.0000##"), "1234.0000" );
+ equal( Globalize.format(1234, "00000#"), "001234" );
+});
+
+test("Number Formatting - custom w/ pattern for +/-", function() {
+ equal( Globalize.format(1234.567, "+#;-#"), "+1235" );
+ equal( Globalize.format(1234.567, "Positive: 0;Negative: 0;zero;ignored"), "Positive: 1235" );
+ equal( Globalize.format(-1234.567, "Positive: 0;Negative: 0;zero;ignored"), "Negative: 1235" );
+ equal( Globalize.format(0, "Positive: 0;Negative: 0;zero;ignored"), "zero" );
+ equal( Globalize.format(0.00001, "Positive: 0;Negative: 0;zero;ignored"), "zero" );
+ equal( Globalize.format(0.00001, "Positive: 0;Negative: 0.0000#;zero;ignored"), "Negative: 0.00001" );
+ equal( Globalize.format(0.00001, "Positive: 0;Negative: 0;;ignored"), "Positive: 0" );
+ equal( Globalize.format(-0.00001, "Positive: 0;Negative: 0;;ignored"), "Positive: 0" );
+ // the test below gives a different result (zero) in C#.
+ // I do not believe that is the expected behaviour, as if I want to add
+ // a positive/negative class to an element, it'd be extremely wrong to receive
+ // zero or positive for a negative number.
+ equal( Globalize.format(-0.00001, "positive;negative;zero;ignored"), "negative" );
+ equal( Globalize.format(-0.00001, "Positive: 0;;zero;ignored"), "zero" );
+});
+
+test("Number Formatting - custom w/ non-formatting text", function() {
+ equal( Globalize.format(1234.567, "# 0 # # 0 0|0.0|0 0 0 # #"), " 0 0 1 2 3|4.5|6 7 0" );
+ equal( Globalize.format(-1234.567, "# 0 # # 0 0|0.0|0 0 0 # #"), "- 0 0 1 2 3|4.5|6 7 0" );
+ equal( Globalize.format(-1234.567, "0 # # 0 0|0.0|0 0 0 # #"), "-0 0 1 2 3|4.5|6 7 0" );
+ equal( Globalize.format(-1234.567, "0 # # 0 \\0|0.0|0 0 0 # #"), "-0 1 2 3 0|4.5|6 7 0" );
+ equal( Globalize.format(1234.567, "<a href=\"\\#go\">#</a>"), "<a href=\"#go\">1235</a>" );
+ equal( Globalize.format(1234.567, '\\\\00000'), "\\01235" );
+});
+
+test("Number Formatting - custom w/ various edge cases", function() {
+ equal( Globalize.format(1234.567, "#.# \\"), "1234.6 " );
+ equal( Globalize.format(1234.567, "#....#"), "1234.6" );
+ equal( Globalize.format(1234.567, "#,,,#.#"), "1,234.6" );
+ equal( Globalize.format(1234.567, "#.#,#"), "1234.57" );
+ equal( Globalize.format(1234.567, ",#.#"), "1234.6" );
+ equal( Globalize.format(1234.567, "#,.#"), "1.2" );
+ equal( Globalize.format(1234.567, ",.#"), "1234.6" );
+ equal( Globalize.format(1234.567, ""), "1234.567" );
+ equal( Globalize.format(1234.567, "0000, ,0000, ,0000.00"), "000,0 00,00 1,234.57" );
+ equal( Globalize.format(1234.567, "000, ,000, ,000.00"), "000, 001, 234.57" );
+ equal( Globalize.format(1234.567, "00 00, ,00, ,00.00"), "00, 00 1,2 34.57" );
+ equal( Globalize.format(1234567890, "##0bill ##0mill ##0thousands and ##0"), "1bill 234mill 567thousands and 890" );
+ equal( Globalize.format(12,23, "##0bill ##0mill ##0thousands and ##0"), "0bill 000mill 000thousands and 012" );
+ equal( Globalize.format(0.1234, ".####"), ".1234" );
+ equal( Globalize.format(0.1234, ".## ##"), ".12 34" );
+ equal( Globalize.format(0.1234, "."), "" );
+ equal( Globalize.format(0.1234, ","), "" );
+ equal( Globalize.format(0.1234, ",."), "" );
+ equal( Globalize.format(0.1234, "0."), "0" );
+ equal( Globalize.format(0, "positive\\;still positive;negative;zero"), "zero" );
+});
View
@@ -27,106 +27,6 @@ test("Number Formatting - p for percentage", function() {
equal( Globalize.format(0.12345, "p4"), "12.3450 %" );
});
-test("Number Formatting - custom w/ 0 padding", function() {
- equal( Globalize.format(1234.567, "0"), "1235" );
- equal( Globalize.format(1234.567, ",0"), "1235" );
- equal( Globalize.format(1234.567, "00"), "1235" );
- equal( Globalize.format(1234.567, ".00"), ".57" );
- equal( Globalize.format(1234.567, "00000"), "01235" );
- equal( Globalize.format(1234.567, "000.00"), "1234.57" );
- equal( Globalize.format(1234.123, "000.00"), "1234.12" );
- equal( Globalize.format(-1234.567, "00000"), "-01235" );
- equal( Globalize.format(-1234.567, "000.00"), "-1234.57" );
- equal( Globalize.format(2.34e12, "000.00"), "2340000000000.00" );
- equal( Globalize.format(1234.567, "0.0"), "1234.6" );
- equal( Globalize.format(1234.567, "0.00"), "1234.57" );
- equal( Globalize.format(1234.567, "0.000"), "1234.567" );
- equal( Globalize.format(1234.567, "0.0000"), "1234.5670" );
- equal( Globalize.format(1234.567, "0,0.00"), "1,234.57" );
- equal( Globalize.format(1234.567, "0000000.00"), "0001234.57" );
- equal( Globalize.format(1234.567, "000000,0.00"), "0,001,234.57" );
- equal( Globalize.format(1234.567, "0,000,000.00"), "0,001,234.57" );
- equal( Globalize.format(1234.567, "0,0,0,0.00"), "1,234.57" );
- equal( Globalize.format(1234.567, "0,0,0,0,0.00"), "01,234.57" );
-});
-
-test("Number Formatting - custom w/ #", function() {
- equal( Globalize.format(1234.567, "#"), "1235" );
- equal( Globalize.format(1234.567, "#."), "1235" );
- equal( Globalize.format(1234.567, ",#"), "1235" );
- equal( Globalize.format(1234.567, "##"), "1235" );
- equal( Globalize.format(1234.567, ".##"), ".57" );
- equal( Globalize.format(1234.567, "#####"), "1235" );
- equal( Globalize.format(1234.567, "###.##"), "1234.57" );
- equal( Globalize.format(-1234.567, "#####"), "-1235" );
- equal( Globalize.format(-1234.567, "###.##"), "-1234.57" );
- equal( Globalize.format(1234.567, "#.#"), "1234.6" );
- equal( Globalize.format(1234.567, "#.##"), "1234.57" );
- equal( Globalize.format(1234.567, "#.###"), "1234.567" );
- equal( Globalize.format(1234.567, "#.####"), "1234.567" );
- equal( Globalize.format(1234.567, "#,#.##"), "1,234.57" );
- equal( Globalize.format(1234.567, "#,#,#.##"), "1,234.57" );
-});
-
-test("Number Formatting - custom w/ mixed # and 0", function() {
- equal( Globalize.format(1234.567, "#00000"), "01235" );
- equal( Globalize.format(1234.567, "#,##0"), "1,235" );
- equal( Globalize.format(1234.567, "#,##0.00"), "1,234.57" );
- equal( Globalize.format(1234.567, "###.#0"), "1234.57" );
- equal( Globalize.format(1234.567, "###.0#"), "1234.57" );
- equal( Globalize.format(1234.567, "#,###.00"), "1,234.57" );
- equal( Globalize.format(1234.567, "#,0.0#"), "1,234.57" );
-});
-
-test("Number Formatting - custom w/ pattern for +/-", function() {
- equal( Globalize.format(1234.567, "+#;-#"), "+1235" );
- equal( Globalize.format(1234.567, "Positive: 0;Negative: 0;zero;ignored"), "Positive: 1235" );
- equal( Globalize.format(-1234.567, "Positive: 0;Negative: 0;zero;ignored"), "Negative: 1235" );
- equal( Globalize.format(0, "Positive: 0;Negative: 0;zero;ignored"), "zero" );
- equal( Globalize.format(0.00001, "Positive: 0;Negative: 0;zero;ignored"), "zero" );
- equal( Globalize.format(0.00001, "Positive: 0;Negative: 0.0000#;zero;ignored"), "Negative: 0.00001" );
- equal( Globalize.format(0.00001, "Positive: 0;Negative: 0;;ignored"), "Positive: 0" );
- equal( Globalize.format(-0.00001, "Positive: 0;Negative: 0;;ignored"), "Positive: 0" );
- // the test below gives a different result (zero) in C#.
- // I do not believe that is the expected behaviour, as if I want to add
- // a positive/negative class to an element, it'd be extremely wrong to receive
- // zero or positive for a negative number.
- equal( Globalize.format(-0.00001, "positive;negative;zero;ignored"), "negative" );
- equal( Globalize.format(-0.00001, "Positive: 0;;zero;ignored"), "zero" );
-});
-
-test("Number Formatting - custom w/ non-formatting text", function() {
- equal( Globalize.format(1234.567, "# 0 # # 0 0|0.0|0 0 0 # #"), " 0 0 1 2 3|4.5|6 7 0" );
- equal( Globalize.format(-1234.567, "# 0 # # 0 0|0.0|0 0 0 # #"), "- 0 0 1 2 3|4.5|6 7 0" );
- equal( Globalize.format(-1234.567, "0 # # 0 0|0.0|0 0 0 # #"), "-0 0 1 2 3|4.5|6 7 0" );
- equal( Globalize.format(-1234.567, "0 # # 0 \\0|0.0|0 0 0 # #"), "-0 1 2 3 0|4.5|6 7 0" );
- equal( Globalize.format(1234.567, "<a href=\"\\#go\">#</a>"), "<a href=\"#go\">1235</a>" );
- equal( Globalize.format(1234.567, '\\\\00000'), "\\01235" );
-});
-
-test("Number Formatting - custom w/ various edge cases", function() {
- equal( Globalize.format(1234.567, "#.# \\"), "1234.6 " );
- equal( Globalize.format(1234.567, "#....#"), "1234.6 " );
- equal( Globalize.format(1234.567, "#,,,#.#"), "1234.6 " );
- equal( Globalize.format(1234.567, "#.#,#"), "1234.6 " );
- equal( Globalize.format(1234.567, ",#.#"), "1234.6" );
- equal( Globalize.format(1234.567, "#,.#"), "1.2" );
- equal( Globalize.format(1234.567, ",.#"), "1234.6" );
- equal( Globalize.format(1234.567, ""), "1234.567" );
- equal( Globalize.format(1234.567, "0000, ,0000, ,0000.00"), "000,0 00,00 1,234.57" );
- equal( Globalize.format(1234.567, "000, ,000, ,000.00"), "000, 001, 234.57" );
- equal( Globalize.format(1234.567, "00 00, ,00, ,00.00"), "00, 00 1,2 34.57" );
- equal( Globalize.format(1234567890, "##0bill ##0mill ##0thousands and ##0"), "1bill 234mill 567thousands and 890" );
- equal( Globalize.format(12,23, "##0bill ##0mill ##0thousands and ##0"), "0bill 000mill 000thousands and 012" );
- equal( Globalize.format(0.1234, ".####"), ".1234" );
- equal( Globalize.format(0.1234, ".## ##"), ".12 34" );
- equal( Globalize.format(0.1234, "."), "" );
- equal( Globalize.format(0.1234, ","), "" );
- equal( Globalize.format(0.1234, ",."), "" );
- equal( Globalize.format(0.1234, "0."), "0" );
- equal( Globalize.format(0, "positive\\;still positive;negative;zero"), "zero" );
-});
-
test("NaN", function() {
equal( Globalize.format(NaN, "n"), "NaN" );
equal( Globalize.format(NaN, "n1"), "NaN" );
View
1 test/index.html 100644 → 100755
@@ -13,6 +13,7 @@
<script src="instance.js"></script>
<script src="findClosestCulture.js"></script>
<script src="format.js"></script>
+ <script src="customFormat.js"></script>
<script src="localize.js"></script>
<script src="parse.js"></script>
</head>

0 comments on commit a09488d

Please sign in to comment.