Skip to content

fix: update format-tokenize to correctly handle escaped percent signs #6695

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Apr 19, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
30 changes: 30 additions & 0 deletions lib/node_modules/@stdlib/console/log-each-map/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -639,3 +639,33 @@ tape( 'the function supports providing a callback execution context', function t
actual.push( str );
}
});

tape( 'the function handles escaped percent signs (%%)', function test( t ) {
var logEachMap;
var expected;
var actual;
var x;
var y;

logEachMap = proxyquire( './../lib/main.js', {
'@stdlib/console/log': logger
});

x = [ 4, 5, 6 ];
y = [ 1, 2, 3 ];
expected = [ '4%1 = 0', '5%2 = 1', '6%3 = 0' ];
actual = [];

logEachMap( '%d%%%d = %d', x, y, mod );

t.deepEqual( actual, expected, 'returns expected value' );
t.end();

function mod( v1, v2 ) {
return v1 % v2;
}

function logger( str ) {
actual.push( str );
}
});
32 changes: 32 additions & 0 deletions lib/node_modules/@stdlib/console/log-each/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -520,3 +520,35 @@ tape( 'the function prints a formatted message when only provided two scalar arg
j += 1;
}
});

tape( 'the function handles escaped percent signs (%%)', function test( t ) {
var expected;
var logEach;
var j;

logEach = proxyquire( './../lib/main.js', {
'@stdlib/console/log': logger
});

expected = [
'Progress: 100% complete',
'Rate: 75% success',
'50.0% + 25.0% = 75.0%',
'5%2 = 1'
];

j = 0;

logEach( 'Progress: 100%% complete' );
logEach( 'Rate: %d%% success', 75 );
logEach( '%0.1f%% + %0.1f%% = %.1f%%', 50.00, 25.00, 75.00 );
logEach( '%d%%%d = %d', 5, 2, 1 );

t.strictEqual( j, expected.length, 'returns expected value' );
t.end();

function logger( str ) {
t.equal( str, expected[ j ], 'returns expected value' );
j += 1;
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,7 @@ console.log( out );
out = formatTokenize( 'Multiple flags: %#+s' );
console.log( out );
// => [ 'Multiple flags: ', {...} ]

out = formatTokenize( 'Percent: %d%%' );
console.log( out );
// => [ 'Percent: ', {...}, '%' ]
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,11 @@ function formatTokenize( str ) {
if ( content.length ) {
tokens.push( content );
}
tokens.push( parse( match ) );
if ( match[ 6 ] === '%' ) {
tokens.push( '%' );
} else {
tokens.push( parse( match ) );
}
prev = RE.lastIndex;
match = RE.exec( str );
}
Expand Down
33 changes: 33 additions & 0 deletions lib/node_modules/@stdlib/string/base/format-tokenize/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,3 +271,36 @@ tape( 'the function tokenizes a string (extracting precision)', function test( t

t.end();
});

tape( 'the function tokenizes a string (extracting escaped percent sign)', function test( t ) {
var expected;
var actual;
var str;

str = 'Progress: 100%% complete';
expected = [
'Progress: 100',
'%',
' complete'
];
actual = formatTokenize( str );
t.deepEqual( actual, expected, 'deep equal' );

str = 'Rate: %d%% success';
expected = [
'Rate: ',
{
'flags': '',
'mapping': void 0,
'width': void 0,
'precision': void 0,
'specifier': 'd'
},
'%',
' success'
];
actual = formatTokenize( str );
t.deepEqual( actual, expected, 'deep equal' );

t.end();
});