Skip to content

Commit 8249695

Browse files
committed
Update handling of out-of-bounds indices
1 parent 1fd94a0 commit 8249695

2 files changed

Lines changed: 22 additions & 16 deletions

File tree

  • lib/node_modules/@stdlib/string/prev-grapheme-cluster-break

lib/node_modules/@stdlib/string/prev-grapheme-cluster-break/lib/main.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,13 @@ function prevGraphemeClusterBreak( str, fromIndex ) {
8282
} else {
8383
idx = len - 1;
8484
}
85-
if ( idx < 0 ) {
86-
idx += len;
87-
}
88-
if ( len === 0 || idx >= len ) {
85+
if ( len === 0 || idx <= 0 ) {
8986
return -1;
9087
}
88+
if ( idx >= len ) {
89+
idx = len - 1;
90+
}
91+
9192
// Initialize caches for storing grapheme break and emoji properties:
9293
breaks = [];
9394
emoji = [];

lib/node_modules/@stdlib/string/prev-grapheme-cluster-break/test/test.js

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -121,20 +121,25 @@ tape( 'the function throws an error if the second argument is not an integer', f
121121
}
122122
});
123123

124-
tape( 'the function returns -1 if the provided position is greater than or equal to the string length', function test( t ) {
124+
tape( 'the function returns uses the default position if the provided position is greater than or equal to the string length', function test( t ) {
125+
var expected;
125126
var out;
126127

127128
out = prevGraphemeClusterBreak( 'last man standing', 17 );
128-
t.strictEqual( out, -1, 'returns expected value' );
129+
expected = prevGraphemeClusterBreak( 'last man standing' );
130+
t.strictEqual( out, expected, 'returns expected value' );
129131

130132
out = prevGraphemeClusterBreak( 'presidential election', 22 );
131-
t.strictEqual( out, -1, 'returns expected value' );
133+
expected = prevGraphemeClusterBreak( 'presidential election' );
134+
t.strictEqual( out, expected, 'returns expected value' );
132135

133136
out = prevGraphemeClusterBreak( 'अनुच्छेद', 10 );
134-
t.strictEqual( out, -1, 'returns expected value' );
137+
expected = prevGraphemeClusterBreak( 'अनुच्छेद' );
138+
t.strictEqual( out, expected, 'returns expected value' );
135139

136140
out = prevGraphemeClusterBreak( '🌷', 2 );
137-
t.strictEqual( out, -1, 'returns expected value' );
141+
expected = prevGraphemeClusterBreak( '🌷' );
142+
t.strictEqual( out, expected, 'returns expected value' );
138143

139144
t.end();
140145
});
@@ -187,26 +192,26 @@ tape( 'the function returns the previous grapheme break position in a specified
187192
t.end();
188193
});
189194

190-
tape( 'the function supports negative integers for second argument', function test( t ) {
195+
tape( 'the function returns zero when the second argument is a negative integer (equivalent to clamping the second argument to zero)', function test( t ) {
191196
var out;
192197

193198
out = prevGraphemeClusterBreak( 'last man standing', -13 );
194-
t.strictEqual( out, 3, 'returns expected value' );
199+
t.strictEqual( out, -1, 'returns expected value' );
195200

196201
out = prevGraphemeClusterBreak( 'presidential election', -13 );
197-
t.strictEqual( out, 7, 'returns expected value' );
202+
t.strictEqual( out, -1, 'returns expected value' );
198203

199204
out = prevGraphemeClusterBreak( 'अनुच्छेद', -6 );
200-
t.strictEqual( out, 0, 'returns expected value' );
205+
t.strictEqual( out, -1, 'returns expected value' );
201206

202207
out = prevGraphemeClusterBreak( 'अनुच्छेद', -5 );
203-
t.strictEqual( out, 2, 'returns expected value' );
208+
t.strictEqual( out, -1, 'returns expected value' );
204209

205210
out = prevGraphemeClusterBreak( 'अनुच्छेद', -2 );
206-
t.strictEqual( out, 4, 'returns expected value' );
211+
t.strictEqual( out, -1, 'returns expected value' );
207212

208213
out = prevGraphemeClusterBreak( '🌷🐷', -2 );
209-
t.strictEqual( out, 1, 'returns expected value' );
214+
t.strictEqual( out, -1, 'returns expected value' );
210215

211216
t.end();
212217
});

0 commit comments

Comments
 (0)