File tree Expand file tree Collapse file tree 1 file changed +16
-8
lines changed
lib/node_modules/@stdlib/string/base/remove-last-grapheme-cluster/lib Expand file tree Collapse file tree 1 file changed +16
-8
lines changed Original file line number Diff line number Diff line change 21
21
// MODULES //
22
22
23
23
var nextGraphemeClusterBreak = require ( '@stdlib/string/next-grapheme-cluster-break' ) ;
24
+ var numGraphemeClusters = require ( '@stdlib/string/num-grapheme-clusters' ) ;
24
25
25
26
26
27
// MAIN //
@@ -57,22 +58,29 @@ var nextGraphemeClusterBreak = require('@stdlib/string/next-grapheme-cluster-bre
57
58
* // returns 'fo'
58
59
*/
59
60
function removeLast ( str , n ) {
60
- var idx ;
61
+ var total ;
62
+ var num ;
61
63
var i ;
62
64
63
- idx = [ ] ;
65
+ if ( n === 0 ) {
66
+ return str ;
67
+ }
68
+
69
+ total = numGraphemeClusters ( str ) ;
70
+ if ( str === '' || total < n ) {
71
+ return '' ;
72
+ }
73
+
64
74
i = 0 ;
65
- while ( i < str . length ) {
75
+ num = 0 ;
76
+ while ( num < total - n ) {
66
77
i = nextGraphemeClusterBreak ( str , i ) ;
78
+ num += 1 ;
67
79
if ( i === - 1 ) {
68
80
break ;
69
81
}
70
- idx . push ( i ) ;
71
- }
72
- if ( str === '' || idx . length < n ) {
73
- return '' ;
74
82
}
75
- return str . substring ( 0 , idx [ idx . length - n ] ) ;
83
+ return str . substring ( 0 , i ) ;
76
84
}
77
85
78
86
You can’t perform that action at this time.
0 commit comments