Skip to content

Commit 7159fce

Browse files
committed
fix: update format-tokenize to handle escaped percent signs
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent da4436d commit 7159fce

File tree

5 files changed

+104
-1
lines changed

5 files changed

+104
-1
lines changed

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

Lines changed: 30 additions & 0 deletions
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

Lines changed: 32 additions & 0 deletions
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

Lines changed: 4 additions & 0 deletions
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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,11 @@ function formatTokenize( str ) {
7373
if ( content.length ) {
7474
tokens.push( content );
7575
}
76-
tokens.push( parse( match ) );
76+
if ( match[ 6 ] === '%' ) {
77+
tokens.push( '%' );
78+
} else {
79+
tokens.push( parse( match ) );
80+
}
7781
prev = RE.lastIndex;
7882
match = RE.exec( str );
7983
}

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,3 +271,36 @@ 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 = 'Rate: %d%% success';
290+
expected = [
291+
'Rate: ',
292+
{
293+
'flags': '',
294+
'mapping': void 0,
295+
'width': void 0,
296+
'precision': void 0,
297+
'specifier': 'd'
298+
},
299+
'%',
300+
' success'
301+
];
302+
actual = formatTokenize( str );
303+
t.deepEqual( actual, expected, 'deep equal' );
304+
305+
t.end();
306+
});

0 commit comments

Comments
 (0)