Browse files

Several fixes, 12/541 still failing

  • Loading branch information...
1 parent a09488d commit 562c88c4ba3a4ba4d40f172f64261ca8485d6c1f @therazor committed May 23, 2012
Showing with 29 additions and 25 deletions.
  1. +28 −25 lib/globalize.js
  2. +1 −0 test/customFormat.js
View
53 lib/globalize.js
@@ -701,31 +701,20 @@ formatDate = function( value, format, culture ) {
},
expandInt = function( number, padToLength, formatInfo ) {
- var numberString = truncate( number ).toString(),
+ var numberString = Math.abs(truncate( number )).toString(),
groupSizes = formatInfo.groupSizes,
curSize = groupSizes[ 0 ],
curGroupIndex = 1,
stringIndex,
sep = formatInfo[ "," ],
ret = "";
- if ( padToLength > numberString.length ) {
- var isNegative;
- if ( numberString.indexOf(0) === '-' ) {
- isNegative = true;
- numberString = numberString.slice( 1 );
- }
+ if ( padToLength > numberString.length )
numberString = zeroPad( numberString, padToLength, true );
- if ( isNegative )
- numberString = '-' + numberString;
- }
stringIndex = numberString.length - 1;
- while ( stringIndex >= 0 ) {
- if ( curSize === 0 || curSize > stringIndex ) {
- return numberString.slice( 0, stringIndex + 1 ) + ( ret.length ? (sep + ret) : '' );
- }
+ while ( stringIndex >= 0 && curSize !== 0 && curSize <= stringIndex ) {
ret = numberString.slice( stringIndex - curSize + 1, stringIndex + 1 ) + ( ret.length ? (sep + ret) : "" );
stringIndex -= curSize;
@@ -736,7 +725,7 @@ formatDate = function( value, format, culture ) {
}
}
- return numberString.slice( 0, stringIndex + 1 ) + sep + ret;
+ return (number < 0 ? '-' : '') + numberString.slice( 0, stringIndex + 1 ) + ( ret.length ? (sep + ret) : '' );
},
expandNumber = function( number, precision, formatInfo ) {;
@@ -783,6 +772,8 @@ formatDate = function( value, format, culture ) {
return value.toString();
// FIXME ignore escaped ; first
+ // Now this would be nice and easy with javascript's
+ // negative lookbehind regex... o wait
var patterns = format.split(';', 3),
//discardIfRoundedIsZero = !!value,
zeroPattern,
@@ -880,7 +871,7 @@ formatDate = function( value, format, culture ) {
var fullPattern = subpatterns.join( '' ),
intDecPatterns = fullPattern.replace( ',', '' ).split( '.' ),
- hasThousandsSeparator = fullPattern.split( '.' ).shift().indexOf(',') > 0,
+ hasThousandsSeparator = !!~fullPattern.split( '.' ).shift().search(/[^,]+,/),
intMinLength,
decimalsLengthRange; // [ min, max ]
@@ -908,18 +899,17 @@ formatDate = function( value, format, culture ) {
if ( rounded === 0 && /*discardIfRoundedIsZero &&*/ pattern !== zeroPattern )
return customNumberFormat.call( this, 0, zeroPattern, formatInfo );
- var formattedString = '',
- formattedValue = [],
+ var formattedValue = [],
roundedString = Math.abs( rounded ).toString(),
curSubpatterns = firstSubpatternContainer;
if ( hasThousandsSeparator )
formattedValue.push( expandInt( rounded, intMinLength, formatInfo ) );
else {
formattedValue.push( zeroPad( roundedString.split('.').shift(), intMinLength, true ) );
+ if ( rounded < 0 )
+ formattedValue[0] = '-' + formattedValue[0];
}
- if ( rounded < 0 )
- formattedValue[0] = '-' + formattedValue[0];
if ( ~decimalsSeparatorIndex && intDecPatterns.length > 1 && intDecPatterns[1] !== '' ) {
var splitRounded = roundedString.split('.'),
@@ -938,22 +928,33 @@ formatDate = function( value, format, culture ) {
formattedValue = formattedValue.join( formatInfo[ "." ] );
+ /*
+ for ( i = subpatterns.length - 1; i > 0; i-- ) {
+ if ( i > decimalsSeparatorIndex || )
+ ;
+ else
+
+ subpatterns[i] = currentFragmentValue;
+ }*/
+
var currentFragmentValue,
intSeparators = 0
curDecimalPlaces = decimalPlaces;
for ( i = subpatterns.length - 1; i > 0; i-- ) {
- if ( i < decimalsSeparatorIndex && curDecimalPlaces < decimalsLengthRange[1] && false )
- currentFragmentValue = '';
- else if ( !hasThousandsSeparator || i > decimalsSeparatorIndex )
+ /*if ( i < decimalsSeparatorIndex && curDecimalPlaces < decimalsLengthRange[1] && false )
+ currentFragmentValue = formattedValue.slice(
+ -Math.min( decimalsLengthRange[1] - curDecimalPlaces, subpatterns[i].length ) );
+ curDecimalPlaces -= currentFragmentValue;
+ else */if ( !hasThousandsSeparator || i > decimalsSeparatorIndex )
currentFragmentValue = formattedValue.slice( -subpatterns[i].length );
else {
if ( decimalsSeparatorIndex === i ) {
var separatorPosition = formattedValue.lastIndexOf( formatInfo["."] );
currentFragmentValue = formattedValue.slice( separatorPosition );
formattedValue = formattedValue.slice( 0, separatorPosition );
subpatterns[i] = subpatterns[i].slice( 0, separatorPosition );
- } else
+ } else // FIXME what is this for?
currentFragmentValue = '';
var j = subpatterns[i].length - 1;
@@ -973,7 +974,9 @@ formatDate = function( value, format, culture ) {
}
subpatterns[i] = formattedValue;
-
+
+ var formattedString = '';
+
// assemble the already made pieces together in the correct order
while ( curSubpatterns.length ) {
formattedString += curSubpatterns.shift();
View
1 test/customFormat.js
@@ -50,6 +50,7 @@ test("Number Formatting - custom w/ mixed # and 0", function() {
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" );
+ equal( Globalize.format(-300.35, '#,##0.00##'), "-300.35" );
});
test("Number Formatting - custom w/ mixed # and 0 and integers", function() {

0 comments on commit 562c88c

Please sign in to comment.