Skip to content

Commit

Permalink
fix: resolve bug in string/truncate
Browse files Browse the repository at this point in the history
PR-URL: stdlib-js#2635
Closes: stdlib-js#2630
Reviewed-by: Athan Reines <kgryte@gmail.com>
Signed-off-by: Snehil Shah <snehilshah.989@gmail.com>
  • Loading branch information
Snehil-Shah authored and gunjjoshi committed Aug 21, 2024
1 parent 9273a58 commit 88fff1e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/node_modules/@stdlib/string/truncate/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
12 changes: 12 additions & 0 deletions lib/node_modules/@stdlib/string/truncate/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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...';
Expand Down

0 comments on commit 88fff1e

Please sign in to comment.