Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,15 @@ function formatInteger( token ) {
if ( i < 0 ) {
out = ( -i ).toString( base );
if ( token.precision ) {
out = zeroPad( out, token.precision, token.padRight );
out = zeroPad( out, token.precision, false );
}
out = '-' + out;
} else {
out = i.toString( base );
if ( !i && !token.precision ) {
out = '';
} else if ( token.precision ) {
out = zeroPad( out, token.precision, token.padRight );
out = zeroPad( out, token.precision, false );
}
if ( token.sign ) {
out = token.sign + out;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,13 @@

'use strict';

// FUNCTIONS //
// VARIABLES //

/**
* Tests if a string starts with a minus sign (`-`).
*
* @private
* @param {string} str - input string
* @returns {boolean} boolean indicating if a string starts with a minus sign (`-`)
*/
function startsWithMinus( str ) {
return str[ 0 ] === '-';
}
// Sign (`-`, `+`, or ` `) and/or hexadecimal prefix (`0x` or `0X`), which must precede any zeros:
var RE_PREFIX = /^[-+ ]?(?:0[xX])?/;


// FUNCTIONS //

/**
* Returns a string of `n` zeros.
Expand Down Expand Up @@ -60,22 +55,17 @@ function zeros( n ) {
* @returns {string} padded token argument
*/
function zeroPad( str, width, right ) {
var negative = false;
var prefix;
var pad = width - str.length;
if ( pad < 0 ) {
return str;
}
if ( startsWithMinus( str ) ) {
negative = true;
str = str.substr( 1 );
}
prefix = RE_PREFIX.exec( str )[ 0 ];
str = str.substring( prefix.length );
str = ( right ) ?
str + zeros( pad ) :
zeros( pad ) + str;
if ( negative ) {
str = '-' + str;
}
return str;
return prefix + str;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ tape( 'the function returns an integer-formatted token argument (precision)', fu
'precision': 5,
'padRight': true
};
expected = '10000';
expected = '00010';
actual = formatInteger( token );
t.strictEqual( actual, expected, 'returns expected value' );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,83 @@ tape( 'the function returns a formatted string (`d` specifier, minimum width, ze
t.end();
});

tape( 'the function returns a formatted string (`d` specifier, minimum width, zero-padded, sign)', function test( t ) {
var expected;
var tokens;
var actual;

tokens = formatTokenize( '%+05d' );
actual = formatInterpolate( tokens, 42 );
expected = '+0042';
t.strictEqual( actual, expected, 'returns expected output' );

tokens = formatTokenize( '% 05d' );
actual = formatInterpolate( tokens, 42 );
expected = ' 0042';
t.strictEqual( actual, expected, 'returns expected output' );

tokens = formatTokenize( '%+05d' );
actual = formatInterpolate( tokens, -42 );
expected = '-0042';
t.strictEqual( actual, expected, 'returns expected output' );

t.end();
});

tape( 'the function returns a formatted string (`d` specifier, precision, left-justified)', function test( t ) {
var expected;
var tokens;
var actual;

tokens = formatTokenize( '%-8.3d|' );
actual = formatInterpolate( tokens, -7 );
expected = '-007 |';
t.strictEqual( actual, expected, 'returns expected output' );

tokens = formatTokenize( '%-+6.2d|' );
actual = formatInterpolate( tokens, 5 );
expected = '+05 |';
t.strictEqual( actual, expected, 'returns expected output' );

t.end();
});

tape( 'the function returns a formatted string (`x` and `X` specifiers, alternate form, minimum width, zero-padded)', function test( t ) {
var expected;
var tokens;
var actual;

tokens = formatTokenize( '%#08x' );
actual = formatInterpolate( tokens, 255 );
expected = '0x0000ff';
t.strictEqual( actual, expected, 'returns expected output' );

tokens = formatTokenize( '%#08X' );
actual = formatInterpolate( tokens, 255 );
expected = '0X0000FF';
t.strictEqual( actual, expected, 'returns expected output' );

t.end();
});

tape( 'the function returns a formatted string (`f` specifier, minimum width, zero-padded, sign)', function test( t ) {
var expected;
var tokens;
var actual;

tokens = formatTokenize( '%+08.3f' );
actual = formatInterpolate( tokens, 3.14159 );
expected = '+003.142';
t.strictEqual( actual, expected, 'returns expected output' );

tokens = formatTokenize( '% 08.3f' );
actual = formatInterpolate( tokens, 3.14159 );
expected = ' 003.142';
t.strictEqual( actual, expected, 'returns expected output' );

t.end();
});

tape( 'the function returns a formatted string (`f` specifier)', function test( t ) {
var expected;
var tokens;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,36 @@ tape( 'the function pads an argument with zeros to a minimum width on the right'

t.end();
});

tape( 'the function inserts zeros after a sign and a hexadecimal prefix', function test( t ) {
var expected;
var actual;
var str;

str = '+42';
expected = '+0042';
actual = zeroPad( str, 5 );
t.strictEqual( actual, expected, 'pads' );

str = ' 42';
expected = ' 0042';
actual = zeroPad( str, 5 );
t.strictEqual( actual, expected, 'pads' );

str = '0xff';
expected = '0x0000ff';
actual = zeroPad( str, 8 );
t.strictEqual( actual, expected, 'pads' );

str = '0XFF';
expected = '0X0000FF';
actual = zeroPad( str, 8 );
t.strictEqual( actual, expected, 'pads' );

str = '+3.142';
expected = '+003.142';
actual = zeroPad( str, 8 );
t.strictEqual( actual, expected, 'pads' );

t.end();
});
Loading