Skip to content

Commit 5d5742b

Browse files
fix: update format-tokenize to correctly handle escaped percent signs
PR-URL: #6695 Reviewed-by: Athan Reines <[email protected]> Reviewed-by: Philipp Burckhardt <[email protected]>
1 parent 1905832 commit 5d5742b

File tree

5 files changed

+132
-1
lines changed

5 files changed

+132
-1
lines changed

lib/node_modules/@stdlib/console/log-each-map/test/test.js

+30
Original file line numberDiff line numberDiff line change
@@ -639,3 +639,33 @@ tape( 'the function supports providing a callback execution context', function t
639639
actual.push( str );
640640
}
641641
});
642+
643+
tape( 'the function handles escaped percent signs (%%)', function test( t ) {
644+
var logEachMap;
645+
var expected;
646+
var actual;
647+
var x;
648+
var y;
649+
650+
logEachMap = proxyquire( './../lib/main.js', {
651+
'@stdlib/console/log': logger
652+
});
653+
654+
x = [ 4, 5, 6 ];
655+
y = [ 1, 2, 3 ];
656+
expected = [ '4%1 = 0', '5%2 = 1', '6%3 = 0' ];
657+
actual = [];
658+
659+
logEachMap( '%d%%%d = %d', x, y, mod );
660+
661+
t.deepEqual( actual, expected, 'returns expected value' );
662+
t.end();
663+
664+
function mod( v1, v2 ) {
665+
return v1 % v2;
666+
}
667+
668+
function logger( str ) {
669+
actual.push( str );
670+
}
671+
});

lib/node_modules/@stdlib/console/log-each/test/test.js

+32
Original file line numberDiff line numberDiff line change
@@ -520,3 +520,35 @@ tape( 'the function prints a formatted message when only provided two scalar arg
520520
j += 1;
521521
}
522522
});
523+
524+
tape( 'the function handles escaped percent signs (%%)', function test( t ) {
525+
var expected;
526+
var logEach;
527+
var j;
528+
529+
logEach = proxyquire( './../lib/main.js', {
530+
'@stdlib/console/log': logger
531+
});
532+
533+
expected = [
534+
'Progress: 100% complete',
535+
'Rate: 75% success',
536+
'50.0% + 25.0% = 75.0%',
537+
'5%2 = 1'
538+
];
539+
540+
j = 0;
541+
542+
logEach( 'Progress: 100%% complete' );
543+
logEach( 'Rate: %d%% success', 75 );
544+
logEach( '%0.1f%% + %0.1f%% = %.1f%%', 50.00, 25.00, 75.00 );
545+
logEach( '%d%%%d = %d', 5, 2, 1 );
546+
547+
t.strictEqual( j, expected.length, 'returns expected value' );
548+
t.end();
549+
550+
function logger( str ) {
551+
t.equal( str, expected[ j ], 'returns expected value' );
552+
j += 1;
553+
}
554+
});

lib/node_modules/@stdlib/string/base/format-tokenize/examples/index.js

+4
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,7 @@ console.log( out );
3131
out = formatTokenize( 'Multiple flags: %#+s' );
3232
console.log( out );
3333
// => [ 'Multiple flags: ', {...} ]
34+
35+
out = formatTokenize( 'Percent: %d%%' );
36+
console.log( out );
37+
// => [ 'Percent: ', {...}, '%' ]

lib/node_modules/@stdlib/string/base/format-tokenize/lib/main.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,12 @@ function formatTokenize( str ) {
7373
if ( content.length ) {
7474
tokens.push( content );
7575
}
76-
tokens.push( parse( match ) );
76+
// Check for an escaped percent sign `%%`...
77+
if ( match[ 6 ] === '%' ) {
78+
tokens.push( '%' );
79+
} else {
80+
tokens.push( parse( match ) );
81+
}
7782
prev = RE.lastIndex;
7883
match = RE.exec( str );
7984
}

lib/node_modules/@stdlib/string/base/format-tokenize/test/test.js

+60
Original file line numberDiff line numberDiff line change
@@ -271,3 +271,63 @@ tape( 'the function tokenizes a string (extracting precision)', function test( t
271271

272272
t.end();
273273
});
274+
275+
tape( 'the function tokenizes a string (extracting escaped percent sign)', function test( t ) {
276+
var expected;
277+
var actual;
278+
var str;
279+
280+
str = 'Progress: 100%% complete';
281+
expected = [
282+
'Progress: 100',
283+
'%',
284+
' complete'
285+
];
286+
actual = formatTokenize( str );
287+
t.deepEqual( actual, expected, 'deep equal' );
288+
289+
str = 'Testing %%%% complete';
290+
expected = [
291+
'Testing ',
292+
'%',
293+
'%',
294+
' complete'
295+
];
296+
actual = formatTokenize( str );
297+
t.deepEqual( actual, expected, 'deep equal' );
298+
299+
str = 'Rate: %d%% success';
300+
expected = [
301+
'Rate: ',
302+
{
303+
'flags': '',
304+
'mapping': void 0,
305+
'width': void 0,
306+
'precision': void 0,
307+
'specifier': 'd'
308+
},
309+
'%',
310+
' success'
311+
];
312+
actual = formatTokenize( str );
313+
t.deepEqual( actual, expected, 'deep equal' );
314+
315+
str = 'Testing %%%d%% complete';
316+
expected = [
317+
'Testing ',
318+
'%',
319+
{
320+
'flags': '',
321+
'mapping': void 0,
322+
'width': void 0,
323+
'precision': void 0,
324+
'specifier': 'd'
325+
},
326+
'%',
327+
' complete'
328+
];
329+
actual = formatTokenize( str );
330+
t.deepEqual( actual, expected, 'deep equal' );
331+
332+
t.end();
333+
});

0 commit comments

Comments
 (0)