Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add review changes
Browse files Browse the repository at this point in the history
steff456 committed Nov 10, 2023
1 parent 910bd93 commit 8f97891
Showing 8 changed files with 16 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -20,7 +20,7 @@ limitations under the License.

# truncateMiddle

> Truncate the middle UTF-16 code units of a string to a specified length.
> Truncate the middle UTF-16 code units of a string to return a string having a specified length.
<section class="usage">

@@ -32,7 +32,7 @@ var truncateMiddle = require( '@stdlib/string/base/truncate-middle' );

#### truncateMiddle( str, len, seq )

Truncates the middle UTF-16 code units of a string to a specified length.
Truncates the middle UTF-16 code units of a string to return a string having a specified length.

```javascript
var out = truncateMiddle( 'beep boop', 7, '...' );
Original file line number Diff line number Diff line change
@@ -42,7 +42,7 @@ bench( pkg, function benchmark( b ) {

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
out = truncateMiddle( values[ i%values.length ], 1, '...' );
out = truncateMiddle( values[ i%values.length ], 7, '...' );
if ( typeof out !== 'string' ) {
b.fail( 'should return a string' );
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

{{alias}}( str, len, seq )
Truncates the middle UTF-16 code units of a string to a specified length.
Truncates the middle UTF-16 code units of a string to return a string
having a specified length.

Parameters
----------
Original file line number Diff line number Diff line change
@@ -19,7 +19,7 @@
// TypeScript Version: 4.1

/**
* Truncates the middle UTF-16 code units of a string to a specified length.
* Truncates the middle UTF-16 code units of a string to return a string having a specified length.
*
* @param str - input string
* @param len - output string length (including sequence)
Original file line number Diff line number Diff line change
@@ -19,7 +19,7 @@
'use strict';

/**
* Truncate the middle UTF-16 code units of a string to a specified length.
* Truncate the middle UTF-16 code units of a string to return a string having a specified length.
*
* @module @stdlib/string/base/truncate-middle
*
10 changes: 6 additions & 4 deletions lib/node_modules/@stdlib/string/base/truncate-middle/lib/main.js
Original file line number Diff line number Diff line change
@@ -27,7 +27,7 @@ var floor = require( '@stdlib/math/base/special/floor' );
// MAIN //

/**
* Truncates the middle UTF-16 code units of a string to a specified length.
* Truncates the middle UTF-16 code units of a string to return a string having a specified length.
*
* @param {string} str - input string
* @param {integer} len - output string length (including sequence)
@@ -60,6 +60,7 @@ var floor = require( '@stdlib/math/base/special/floor' );
* // returns '..'
*/
function truncateMiddle( str, len, seq ) {
var finalLength;
var seqLength;
var strLength;
var seqStart;
@@ -70,12 +71,13 @@ function truncateMiddle( str, len, seq ) {
if ( len > strLength ) {
return str;
}
if ( len - seqLength < 0 ) {
finalLength = len - seqLength;
if ( finalLength < 0 ) {
return seq.slice( 0, len );
}

seqStart = round( ( len - seqLength ) / 2 );
seqEnd = strLength - floor( ( len - seqLength ) / 2 );
seqStart = round( finalLength / 2 );
seqEnd = strLength - floor( finalLength / 2 );

return str.substring( 0, seqStart ) + seq + str.substring( seqEnd );
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@stdlib/string/base/truncate-middle",
"version": "0.0.0",
"description": "Truncate the middle UTF-16 code units of a string to a specified length.",
"description": "Truncate the middle UTF-16 code units of a string to return a string having a specified length.",
"license": "Apache-2.0",
"author": {
"name": "The Stdlib Authors",
Original file line number Diff line number Diff line change
@@ -78,8 +78,8 @@ tape( 'the function truncates a string to the specified length (custom replaceme
t.strictEqual( actual, expected, 'returns expected value' );

str = 'beep boop';
len = 10;
expected = 'beep boop';
len = 7;
expected = 'bee!oop';
actual = truncateMiddle( str, len, '!' );
t.strictEqual( actual, expected, 'returns expected value' );

0 comments on commit 8f97891

Please sign in to comment.