Skip to content

Commit

Permalink
feat: add string/base/truncate-middle
Browse files Browse the repository at this point in the history
PR-URL: 	#1118
Co-authored-by: Athan Reines <[email protected]>
Reviewed-by: Athan Reines <[email protected]>
Ref: #1062
  • Loading branch information
steff456 and kgryte authored Nov 10, 2023
1 parent fc8fba0 commit d859ee0
Show file tree
Hide file tree
Showing 10 changed files with 630 additions and 0 deletions.
92 changes: 92 additions & 0 deletions lib/node_modules/@stdlib/string/base/truncate-middle/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<!--
@license Apache-2.0
Copyright (c) 2023 The Stdlib Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

# truncateMiddle

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

## Usage

```javascript
var truncateMiddle = require( '@stdlib/string/base/truncate-middle' );
```

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

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, '...' );
// returns 'be...op'

out = truncateMiddle( 'beep boop', 7, '!' );
// returns 'bee!oop'

out = truncateMiddle( 'beep boop', 7, '!!!' );
// returns 'be!!!op'
```

</section>

<!-- /.usage -->

<section class="examples">

## Examples

<!-- eslint no-undef: "error" -->

```javascript
var truncateMiddle = require( '@stdlib/string/base/truncate-middle' );

var str = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.';
var out = truncateMiddle( str, 15, '...' );
// returns 'Lorem ... elit.'

str = 'To be or not to be, that is the question';
out = truncateMiddle( str, 19, '|' );
// returns 'To be or | question'

str = 'The quick fox jumps over the lazy dog.';
out = truncateMiddle( str, 28, '...' );
// returns 'The quick fox...he lazy dog.'
```

</section>

<!-- /.examples -->

<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->

<section class="related">

</section>

<!-- /.related -->

<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="links">

</section>

<!-- /.links -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2023 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var bench = require( '@stdlib/bench' );
var isString = require( '@stdlib/assert/is-string' ).isPrimitive;
var pkg = require( './../package.json' ).name;
var truncateMiddle = require( './../lib' );


// MAIN //

bench( pkg, function benchmark( b ) {
var values;
var out;
var i;

values = [
'beep boop',
'foo bar',
'xyz abc',
'🐶🐮🐷🐰🐸'
];

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
out = truncateMiddle( values[ i%values.length ], 7, '...' );
if ( typeof out !== 'string' ) {
b.fail( 'should return a string' );
}
}
b.toc();
if ( !isString( out ) ) {
b.fail( 'should return a string' );
}
b.pass( 'benchmark finished' );
b.end();
});
31 changes: 31 additions & 0 deletions lib/node_modules/@stdlib/string/base/truncate-middle/docs/repl.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

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

Parameters
----------
str: string
Input string.

len: integer
Output string length.

seq: string
Custom replacement sequence.

Returns
-------
out: string
Truncated string.

Examples
--------
> var str = 'beep boop';
> var out = {{alias}}( str, 5, '...' )
'b...p'
> out = {{alias}}( str, 5, '|' )
'be|op'

See Also
--------
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* @license Apache-2.0
*
* Copyright (c) 2023 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// TypeScript Version: 4.1

/**
* 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)
* @param seq - custom replacement sequence
* @returns truncated string
*
* @example
* var str = 'beep boop';
* var out = truncateMiddle( str, 5, '...' );
* // returns 'b...p'
*
* @example
* var str = 'beep boop';
* var out = truncateMiddle( str, 5, '>>>' );
* // returns 'b>>>p'
*
* @example
* var str = 'beep boop';
* var out = truncateMiddle( str, 10, '...' );
* // returns 'beep boop'
*
* @example
* var str = 'beep boop';
* var out = truncateMiddle( str, 0, '...' );
* // returns ''
*
* @example
* var str = 'beep boop';
* var out = truncateMiddle( str, 2, '...' );
* // returns '..'
*/
declare function truncateMiddle( str: string, len: number, seq: string ): string;


// EXPORTS //

export = truncateMiddle;
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* @license Apache-2.0
*
* Copyright (c) 2023 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import truncateMiddle = require( './index' );


// TESTS //

// The function returns a string...
{
truncateMiddle( 'abcdefghi', 3, '...' ); // $ExpectType string
}

// The compiler throws an error if the function is not provided a string as its first argument...
{
truncateMiddle( true, 6, '...' ); // $ExpectError
truncateMiddle( false, 6, '...' ); // $ExpectError
truncateMiddle( 3, 6, '...' ); // $ExpectError
truncateMiddle( [], 6, '...' ); // $ExpectError
truncateMiddle( {}, 6, '...' ); // $ExpectError
truncateMiddle( ( x: number ): number => x, 6, '...' ); // $ExpectError
}

// The compiler throws an error if the function is not provided a number as its second argument...
{
truncateMiddle( 'abd', true, '...' ); // $ExpectError
truncateMiddle( 'abd', false, '...' ); // $ExpectError
truncateMiddle( 'abd', 'abc', '...' ); // $ExpectError
truncateMiddle( 'abd', [], '...' ); // $ExpectError
truncateMiddle( 'abd', {}, '...' ); // $ExpectError
truncateMiddle( 'abd', ( x: number ): number => x, '...' ); // $ExpectError
}

// The compiler throws an error if the function is not provided a string as its third argument...
{
truncateMiddle( 'beep boop', 4, true ); // $ExpectError
truncateMiddle( 'beep boop', 4, false ); // $ExpectError
truncateMiddle( 'beep boop', 4, 123 ); // $ExpectError
truncateMiddle( 'beep boop', 4, [], 0 ); // $ExpectError
truncateMiddle( 'beep boop', 4, {}, 0 ); // $ExpectError
truncateMiddle( 'beep boop', 4, ( x: number ): number => x, 0 ); // $ExpectError
}

// The compiler throws an error if the function is provided an unsupported number of arguments...
{
truncateMiddle(); // $ExpectError
truncateMiddle( 'abc', 4, '|', true ); // $ExpectError
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2023 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

var truncateMiddle = require( './../lib' );

var str = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.';
var out = truncateMiddle( str, 15, '...' );
console.log( out );
// => 'Lorem ... elit.'

str = 'To be or not to be, that is the question';
out = truncateMiddle( str, 19, '|' );
console.log( out );
// => 'To be or | question'

str = 'The quick fox jumps over the lazy dog.';
out = truncateMiddle( str, 28, '...' );
console.log( out );
// => 'The quick fox...he lazy dog.'
43 changes: 43 additions & 0 deletions lib/node_modules/@stdlib/string/base/truncate-middle/lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2023 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

/**
* Truncate the middle UTF-16 code units of a string to return a string having a specified length.
*
* @module @stdlib/string/base/truncate-middle
*
* @example
* var truncateMiddle = require( '@stdlib/string/base/truncate-middle' );
*
* var out = truncateMiddle( 'beep boop', 7, '...' );
* // returns 'be...op'
*
* out = truncateMiddle( 'beep boop', 7, '|' );
* // returns 'bee|oop'
*/

// MODULES //

var main = require( './main.js' );


// EXPORTS //

module.exports = main;
Loading

1 comment on commit d859ee0

@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/truncate-middle $\color{green}132/132$
$\color{green}+100.00\%$
$\color{red}8/9$
$\color{green}+88.89\%$
$\color{green}1/1$
$\color{green}+100.00\%$
$\color{green}132/132$
$\color{green}+100.00\%$

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

Please sign in to comment.