diff --git a/lib/node_modules/@stdlib/console/log-each-map/test/test.js b/lib/node_modules/@stdlib/console/log-each-map/test/test.js index b758bf72e00e..acf98cc70234 100644 --- a/lib/node_modules/@stdlib/console/log-each-map/test/test.js +++ b/lib/node_modules/@stdlib/console/log-each-map/test/test.js @@ -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 ); + } +}); diff --git a/lib/node_modules/@stdlib/console/log-each/test/test.js b/lib/node_modules/@stdlib/console/log-each/test/test.js index 3def3b72d2ef..9e1ef53faf32 100644 --- a/lib/node_modules/@stdlib/console/log-each/test/test.js +++ b/lib/node_modules/@stdlib/console/log-each/test/test.js @@ -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; + } +}); diff --git a/lib/node_modules/@stdlib/string/base/format-tokenize/examples/index.js b/lib/node_modules/@stdlib/string/base/format-tokenize/examples/index.js index e0aac7731c94..70ca4596b70d 100644 --- a/lib/node_modules/@stdlib/string/base/format-tokenize/examples/index.js +++ b/lib/node_modules/@stdlib/string/base/format-tokenize/examples/index.js @@ -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: ', {...}, '%' ] diff --git a/lib/node_modules/@stdlib/string/base/format-tokenize/lib/main.js b/lib/node_modules/@stdlib/string/base/format-tokenize/lib/main.js index 207c12822174..f7366ec046ba 100644 --- a/lib/node_modules/@stdlib/string/base/format-tokenize/lib/main.js +++ b/lib/node_modules/@stdlib/string/base/format-tokenize/lib/main.js @@ -73,7 +73,12 @@ function formatTokenize( str ) { if ( content.length ) { tokens.push( content ); } - tokens.push( parse( match ) ); + // Check for an escaped percent sign `%%`... + if ( match[ 6 ] === '%' ) { + tokens.push( '%' ); + } else { + tokens.push( parse( match ) ); + } prev = RE.lastIndex; match = RE.exec( str ); } diff --git a/lib/node_modules/@stdlib/string/base/format-tokenize/test/test.js b/lib/node_modules/@stdlib/string/base/format-tokenize/test/test.js index 002e684a327b..aec6be29862b 100644 --- a/lib/node_modules/@stdlib/string/base/format-tokenize/test/test.js +++ b/lib/node_modules/@stdlib/string/base/format-tokenize/test/test.js @@ -271,3 +271,63 @@ 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 = 'Testing %%%% complete'; + expected = [ + 'Testing ', + '%', + '%', + ' 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' ); + + str = 'Testing %%%d%% complete'; + expected = [ + 'Testing ', + '%', + { + 'flags': '', + 'mapping': void 0, + 'width': void 0, + 'precision': void 0, + 'specifier': 'd' + }, + '%', + ' complete' + ]; + actual = formatTokenize( str ); + t.deepEqual( actual, expected, 'deep equal' ); + + t.end(); +});