Skip to content

Commit

Permalink
fix: update implementation to avoid using an array
Browse files Browse the repository at this point in the history
  • Loading branch information
steff456 committed Sep 4, 2023
1 parent 228b921 commit 76d08ac
Showing 1 changed file with 16 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
// MODULES //

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


// MAIN //
Expand Down Expand Up @@ -57,22 +58,29 @@ var nextGraphemeClusterBreak = require('@stdlib/string/next-grapheme-cluster-bre
* // returns 'fo'
*/
function removeLast( str, n ) {
var idx;
var total;
var num;
var i;

idx = [];
if ( n === 0 ) {
return str;
}

total = numGraphemeClusters( str );
if ( str === '' || total < n ) {
return '';
}

i = 0;
while ( i < str.length ) {
num = 0;
while ( num < total - n ) {
i = nextGraphemeClusterBreak( str, i );
num += 1;
if ( i === -1 ) {
break;
}
idx.push(i);
}
if ( str === '' || idx.length < n ) {
return '';
}
return str.substring( 0, idx[ idx.length - n ] );
return str.substring( 0, i );
}


Expand Down

1 comment on commit 76d08ac

@stdlib-bot
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage Report

Package Statements Branches Functions Lines
string/base/remove-last-grapheme-cluster $\color{red}133/135$
$\color{red}--1.48\%$
$\color{red}10/11$
$\color{red}--9.09\%$
$\color{green}1/1$
$\color{green}+0.00\%$
$\color{red}133/135$
$\color{red}--1.48\%$

The above coverage report was generated for the changes in this push.

Please sign in to comment.