From d100a235a4b646ebf9dc57d3d44bee7de821fcfd Mon Sep 17 00:00:00 2001 From: Snehil Shah Date: Sat, 20 Jul 2024 01:26:23 +0000 Subject: [PATCH] fix: incorrect behavior of `truncate` Signed-off-by: Snehil Shah --- lib/node_modules/@stdlib/string/truncate/lib/main.js | 4 ++-- .../@stdlib/string/truncate/test/test.js | 12 ++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/node_modules/@stdlib/string/truncate/lib/main.js b/lib/node_modules/@stdlib/string/truncate/lib/main.js index 5aadc30d4da4..353dff40685d 100644 --- a/lib/node_modules/@stdlib/string/truncate/lib/main.js +++ b/lib/node_modules/@stdlib/string/truncate/lib/main.js @@ -89,10 +89,10 @@ function truncate( str, len, ending ) { ending = ending || '...'; endingLength = numGraphemeClusters( ending ); fromIndex = 0; - if ( len > numGraphemeClusters( str ) ) { + if ( len >= numGraphemeClusters( str ) ) { return str; } - if ( len - endingLength < 0 ) { + if ( len - endingLength <= 0 ) { return ending.slice( 0, len ); } nVisual = 0; diff --git a/lib/node_modules/@stdlib/string/truncate/test/test.js b/lib/node_modules/@stdlib/string/truncate/test/test.js index 897a7e844f29..02a6531ffec4 100644 --- a/lib/node_modules/@stdlib/string/truncate/test/test.js +++ b/lib/node_modules/@stdlib/string/truncate/test/test.js @@ -145,6 +145,18 @@ tape( 'the function truncates a string to the specified length', function test( actual = truncate( str, len ); t.strictEqual( actual, expected, 'returns expected value' ); + str = 'beep boop'; + len = 3; + expected = '...'; + actual = truncate( str, len ); + t.strictEqual( actual, expected, 'returns expected value' ); + + str = 'beep boop'; + len = 9; + expected = 'beep boop'; + actual = truncate( str, len ); + t.strictEqual( actual, expected, 'returns expected value' ); + str = '🐺 Wolf Brothers 🐺'; len = 6; expected = '🐺 W...';