Skip to content

Commit e6b088e

Browse files
committed
Auto-generated commit
1 parent a5c51fd commit e6b088e

File tree

2 files changed

+13
-18
lines changed

2 files changed

+13
-18
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ var out = truncateMiddle( 'beep boop', 7 );
6161
// returns 'be...op'
6262
```
6363

64-
By default, the truncated string uses the replacement sequence `'...'` in the middle. To customize the replacement sequence, provide a `seq` argument:
64+
By default, the truncated string uses the replacement sequence `'...'`. To customize the replacement sequence, provide a `seq` argument:
6565

6666
```javascript
6767
var out = truncateMiddle( 'beep boop', 7, '!' );
@@ -99,7 +99,7 @@ var out = truncateMiddle( str, 15 );
9999
// returns 'Lorem ... elit.'
100100

101101
str = 'To be or not to be, that is the question';
102-
out = truncateMiddle( str, 19, '!' );
102+
out = truncateMiddle( str, 19, '|' );
103103
// returns 'To be or | question'
104104

105105
str = 'The quick fox jumps over the lazy dog.';

lib/main.js

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ function truncateMiddle( str, len, seq ) {
7878
var seqStart;
7979
var nVisual;
8080
var seqEnd;
81-
var idx;
82-
var out;
81+
var idx2;
82+
var idx1;
8383
if ( !isString( str ) ) {
8484
throw new TypeError( 'invalid argument. First argument must be a string primitive. Value: `' + str + '`.' );
8585
}
@@ -95,7 +95,6 @@ function truncateMiddle( str, len, seq ) {
9595
seqLength = numGraphemeClusters( seq );
9696
strLength = numGraphemeClusters( str );
9797
fromIndex = 0;
98-
out = '';
9998
if ( len > strLength ) {
10099
return str;
101100
}
@@ -106,24 +105,20 @@ function truncateMiddle( str, len, seq ) {
106105
seqEnd = strLength - floor( ( len - seqLength ) / 2 );
107106
nVisual = 0;
108107
while ( nVisual < seqStart ) {
109-
idx = nextGraphemeClusterBreak( str, fromIndex );
110-
out += str.slice( fromIndex, idx );
111-
fromIndex = idx;
108+
idx1 = nextGraphemeClusterBreak( str, fromIndex );
109+
fromIndex = idx1;
112110
nVisual += 1;
113111
}
114-
out += seq;
115-
while ( idx > 0 ) {
116-
idx = nextGraphemeClusterBreak( str, fromIndex );
117-
if ( idx === -1 ) {
118-
out += str.slice( fromIndex );
112+
idx2 = idx1;
113+
while ( idx2 > 0 ) {
114+
idx2 = nextGraphemeClusterBreak( str, fromIndex );
115+
if ( idx2 >= seqEnd + fromIndex - nVisual ) {
116+
break;
119117
}
120-
else if ( idx > seqEnd + fromIndex - nVisual ) {
121-
out += str.slice( fromIndex, idx );
122-
}
123-
fromIndex = idx;
118+
fromIndex = idx2;
124119
nVisual += 1;
125120
}
126-
return out;
121+
return str.substring( 0, idx1 ) + seq + str.substring( idx2 );
127122
}
128123

129124

0 commit comments

Comments
 (0)