Skip to content

Commit

Permalink
Format %t and %T: fix rounding error making it possible to display 60…
Browse files Browse the repository at this point in the history
…s instead of the incremented minute, fix sign
  • Loading branch information
cronvel committed May 29, 2022
1 parent 52e718c commit ace8f48
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 16 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@

v0.16.6
-------

Format %t and %T: fix rounding error making it possible to display 60s instead of the incremented minute, fix sign


v0.16.5
-------

Expand Down
51 changes: 44 additions & 7 deletions browser/string-kit.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ StringNumber.prototype.set = function( number ) {

number = + number ;

// Reset anything, if it was already used...
this.sign = 1 ;
this.digits.length = 0 ;
this.exposant = 0 ;
this.special = null ;

if ( ! Number.isFinite( number ) ) {
this.special = number ;
return null ;
Expand Down Expand Up @@ -301,6 +307,8 @@ StringNumber.prototype.precision = function( n , type = 0 ) {

if ( n < 0 ) { this.digits.length = 0 ; return this ; }

type *= this.sign ;

if ( type < 0 ) {
roundUp =
this.digits.length > n + 4
Expand Down Expand Up @@ -1664,32 +1672,61 @@ modes.t = ( arg , modeArg , useLetters = true ) => {
if ( typeof arg !== 'number' ) { return '(NaN)' ; }

var h , min , s , sn , sStr ,
sign = '' ,
subModes = timeModeArg( modeArg ) ,
roundingType = subModes.roundingType ,
hSeparator = useLetters ? 'h' : ':' ,
minSeparator = useLetters ? 'min' : ':' ,
sSeparator = useLetters ? 's' : '.' ,
forceDecimalSeparator = useLetters ;

s = arg / 1000 ;

if ( s < 0 ) {
s = -s ;
roundingType *= -1 ;
sign = '-' ;
}

if ( s < 60 && ! subModes.forceMinutes ) {
sn = new StringNumber( s , sSeparator , undefined , forceDecimalSeparator ) ;
sn.round( subModes.rounding , subModes.roundingType ) ;
sStr = sn.toString( 1 , subModes.rightPadding , subModes.rightPaddingOnlyIfDecimal ) ;
return sStr ;
sn.round( subModes.rounding , roundingType ) ;

// Check if rounding has made it reach 60
if ( sn.toNumber() < 60 ) {
sStr = sn.toString( 1 , subModes.rightPadding , subModes.rightPaddingOnlyIfDecimal ) ;
return sign + sStr ;
}

s = 60 ;

}

min = Math.floor( s / 60 ) ;
s = s % 60 ;

sn = new StringNumber( s , sSeparator , undefined , forceDecimalSeparator ) ;
sn.round( subModes.rounding , subModes.roundingType ) ;
sStr = sn.toString( 2 , subModes.rightPadding , subModes.rightPaddingOnlyIfDecimal ) ;
sn.round( subModes.rounding , roundingType ) ;

// Check if rounding has made it reach 60
if ( sn.toNumber() < 60 ) {
sStr = sn.toString( 2 , subModes.rightPadding , subModes.rightPaddingOnlyIfDecimal ) ;
}
else {
min ++ ;
s = 0 ;
sn.set( s ) ;
sStr = sn.toString( 2 , subModes.rightPadding , subModes.rightPaddingOnlyIfDecimal ) ;
}

if ( min < 60 && ! subModes.forceHours ) { return min + minSeparator + sStr ; }
if ( min < 60 && ! subModes.forceHours ) {
return sign + min + minSeparator + sStr ;
}

h = Math.floor( min / 60 ) ;
min = min % 60 ;
return h + hSeparator + ( '' + min ).padStart( 2 , '0' ) + minSeparator + sStr ;

return sign + h + hSeparator + ( '' + min ).padStart( 2 , '0' ) + minSeparator + sStr ;
} ;

modes.t.noSanitize = true ;
Expand Down
2 changes: 1 addition & 1 deletion browser/string-kit.min.js

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions lib/StringNumber.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ StringNumber.prototype.set = function( number ) {

number = + number ;

// Reset anything, if it was already used...
this.sign = 1 ;
this.digits.length = 0 ;
this.exposant = 0 ;
this.special = null ;

if ( ! Number.isFinite( number ) ) {
this.special = number ;
return null ;
Expand Down Expand Up @@ -300,6 +306,8 @@ StringNumber.prototype.precision = function( n , type = 0 ) {

if ( n < 0 ) { this.digits.length = 0 ; return this ; }

type *= this.sign ;

if ( type < 0 ) {
roundUp =
this.digits.length > n + 4
Expand Down
43 changes: 36 additions & 7 deletions lib/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -808,32 +808,61 @@ modes.t = ( arg , modeArg , useLetters = true ) => {
if ( typeof arg !== 'number' ) { return '(NaN)' ; }

var h , min , s , sn , sStr ,
sign = '' ,
subModes = timeModeArg( modeArg ) ,
roundingType = subModes.roundingType ,
hSeparator = useLetters ? 'h' : ':' ,
minSeparator = useLetters ? 'min' : ':' ,
sSeparator = useLetters ? 's' : '.' ,
forceDecimalSeparator = useLetters ;

s = arg / 1000 ;

if ( s < 0 ) {
s = -s ;
roundingType *= -1 ;
sign = '-' ;
}

if ( s < 60 && ! subModes.forceMinutes ) {
sn = new StringNumber( s , sSeparator , undefined , forceDecimalSeparator ) ;
sn.round( subModes.rounding , subModes.roundingType ) ;
sStr = sn.toString( 1 , subModes.rightPadding , subModes.rightPaddingOnlyIfDecimal ) ;
return sStr ;
sn.round( subModes.rounding , roundingType ) ;

// Check if rounding has made it reach 60
if ( sn.toNumber() < 60 ) {
sStr = sn.toString( 1 , subModes.rightPadding , subModes.rightPaddingOnlyIfDecimal ) ;
return sign + sStr ;
}

s = 60 ;

}

min = Math.floor( s / 60 ) ;
s = s % 60 ;

sn = new StringNumber( s , sSeparator , undefined , forceDecimalSeparator ) ;
sn.round( subModes.rounding , subModes.roundingType ) ;
sStr = sn.toString( 2 , subModes.rightPadding , subModes.rightPaddingOnlyIfDecimal ) ;
sn.round( subModes.rounding , roundingType ) ;

if ( min < 60 && ! subModes.forceHours ) { return min + minSeparator + sStr ; }
// Check if rounding has made it reach 60
if ( sn.toNumber() < 60 ) {
sStr = sn.toString( 2 , subModes.rightPadding , subModes.rightPaddingOnlyIfDecimal ) ;
}
else {
min ++ ;
s = 0 ;
sn.set( s ) ;
sStr = sn.toString( 2 , subModes.rightPadding , subModes.rightPaddingOnlyIfDecimal ) ;
}

if ( min < 60 && ! subModes.forceHours ) {
return sign + min + minSeparator + sStr ;
}

h = Math.floor( min / 60 ) ;
min = min % 60 ;
return h + hSeparator + ( '' + min ).padStart( 2 , '0' ) + minSeparator + sStr ;

return sign + h + hSeparator + ( '' + min ).padStart( 2 , '0' ) + minSeparator + sStr ;
} ;

modes.t.noSanitize = true ;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "string-kit",
"version": "0.16.5",
"version": "0.16.6",
"engines": {
"node": ">=14.15.0"
},
Expand Down
6 changes: 6 additions & 0 deletions test/string-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,12 @@ describe( "format()" , () => {
expect( format( '%T' , 3600000 ) ).to.be( '1:00:00' ) ;
expect( format( '%T' , 3599999 ) ).to.be( '59:59' ) ;
expect( format( '%T' , 7890000 ) ).to.be( '2:11:30' ) ;

expect( format( '%[c]T' , 120000 ) ).to.be( '2:00' ) ;
expect( format( '%[c]T' , 119900 ) ).to.be( '2:00' ) ;

expect( format( '%T' , -120000 ) ).to.be( '-2:00' ) ;
expect( format( '%T' , -119900 ) ).to.be( '-2:00' ) ;
} ) ;

it( "%z should format as base64" , () => {
Expand Down

0 comments on commit ace8f48

Please sign in to comment.