Skip to content

Commit 4345839

Browse files
authored
fix: resolve bug in string/truncate
PR-URL: #2635 Closes: #2630 Reviewed-by: Athan Reines <kgryte@gmail.com> Signed-off-by: Snehil Shah <snehilshah.989@gmail.com>
1 parent c89a171 commit 4345839

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

lib/node_modules/@stdlib/string/truncate/lib/main.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,10 @@ function truncate( str, len, ending ) {
8989
ending = ending || '...';
9090
endingLength = numGraphemeClusters( ending );
9191
fromIndex = 0;
92-
if ( len > numGraphemeClusters( str ) ) {
92+
if ( len >= numGraphemeClusters( str ) ) {
9393
return str;
9494
}
95-
if ( len - endingLength < 0 ) {
95+
if ( len - endingLength <= 0 ) {
9696
return ending.slice( 0, len );
9797
}
9898
nVisual = 0;

lib/node_modules/@stdlib/string/truncate/test/test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,18 @@ tape( 'the function truncates a string to the specified length', function test(
145145
actual = truncate( str, len );
146146
t.strictEqual( actual, expected, 'returns expected value' );
147147

148+
str = 'beep boop';
149+
len = 3;
150+
expected = '...';
151+
actual = truncate( str, len );
152+
t.strictEqual( actual, expected, 'returns expected value' );
153+
154+
str = 'beep boop';
155+
len = 9;
156+
expected = 'beep boop';
157+
actual = truncate( str, len );
158+
t.strictEqual( actual, expected, 'returns expected value' );
159+
148160
str = '🐺 Wolf Brothers 🐺';
149161
len = 6;
150162
expected = '🐺 W...';

0 commit comments

Comments
 (0)