Skip to content

Commit 76d08ac

Browse files
committed
fix: update implementation to avoid using an array
1 parent 228b921 commit 76d08ac

File tree

1 file changed

+16
-8
lines changed
  • lib/node_modules/@stdlib/string/base/remove-last-grapheme-cluster/lib

1 file changed

+16
-8
lines changed

lib/node_modules/@stdlib/string/base/remove-last-grapheme-cluster/lib/main.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
// MODULES //
2222

2323
var nextGraphemeClusterBreak = require('@stdlib/string/next-grapheme-cluster-break');
24+
var numGraphemeClusters = require( '@stdlib/string/num-grapheme-clusters' );
2425

2526

2627
// MAIN //
@@ -57,22 +58,29 @@ var nextGraphemeClusterBreak = require('@stdlib/string/next-grapheme-cluster-bre
5758
* // returns 'fo'
5859
*/
5960
function removeLast( str, n ) {
60-
var idx;
61+
var total;
62+
var num;
6163
var i;
6264

63-
idx = [];
65+
if ( n === 0 ) {
66+
return str;
67+
}
68+
69+
total = numGraphemeClusters( str );
70+
if ( str === '' || total < n ) {
71+
return '';
72+
}
73+
6474
i = 0;
65-
while ( i < str.length ) {
75+
num = 0;
76+
while ( num < total - n ) {
6677
i = nextGraphemeClusterBreak( str, i );
78+
num += 1;
6779
if ( i === -1 ) {
6880
break;
6981
}
70-
idx.push(i);
71-
}
72-
if ( str === '' || idx.length < n ) {
73-
return '';
7482
}
75-
return str.substring( 0, idx[ idx.length - n ] );
83+
return str.substring( 0, i );
7684
}
7785

7886

0 commit comments

Comments
 (0)