Skip to content

Commit d859ee0

Browse files
steff456kgryte
andauthored
feat: add string/base/truncate-middle
PR-URL: #1118 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]> Ref: #1062
1 parent fc8fba0 commit d859ee0

File tree

10 files changed

+630
-0
lines changed

10 files changed

+630
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2023 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# truncateMiddle
22+
23+
> Truncate the middle UTF-16 code units of a string to return a string having a specified length.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var truncateMiddle = require( '@stdlib/string/base/truncate-middle' );
31+
```
32+
33+
#### truncateMiddle( str, len, seq )
34+
35+
Truncates the middle UTF-16 code units of a string to return a string having a specified length.
36+
37+
```javascript
38+
var out = truncateMiddle( 'beep boop', 7, '...' );
39+
// returns 'be...op'
40+
41+
out = truncateMiddle( 'beep boop', 7, '!' );
42+
// returns 'bee!oop'
43+
44+
out = truncateMiddle( 'beep boop', 7, '!!!' );
45+
// returns 'be!!!op'
46+
```
47+
48+
</section>
49+
50+
<!-- /.usage -->
51+
52+
<section class="examples">
53+
54+
## Examples
55+
56+
<!-- eslint no-undef: "error" -->
57+
58+
```javascript
59+
var truncateMiddle = require( '@stdlib/string/base/truncate-middle' );
60+
61+
var str = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.';
62+
var out = truncateMiddle( str, 15, '...' );
63+
// returns 'Lorem ... elit.'
64+
65+
str = 'To be or not to be, that is the question';
66+
out = truncateMiddle( str, 19, '|' );
67+
// returns 'To be or | question'
68+
69+
str = 'The quick fox jumps over the lazy dog.';
70+
out = truncateMiddle( str, 28, '...' );
71+
// returns 'The quick fox...he lazy dog.'
72+
```
73+
74+
</section>
75+
76+
<!-- /.examples -->
77+
78+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
79+
80+
<section class="related">
81+
82+
</section>
83+
84+
<!-- /.related -->
85+
86+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
87+
88+
<section class="links">
89+
90+
</section>
91+
92+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var isString = require( '@stdlib/assert/is-string' ).isPrimitive;
25+
var pkg = require( './../package.json' ).name;
26+
var truncateMiddle = require( './../lib' );
27+
28+
29+
// MAIN //
30+
31+
bench( pkg, function benchmark( b ) {
32+
var values;
33+
var out;
34+
var i;
35+
36+
values = [
37+
'beep boop',
38+
'foo bar',
39+
'xyz abc',
40+
'🐶🐮🐷🐰🐸'
41+
];
42+
43+
b.tic();
44+
for ( i = 0; i < b.iterations; i++ ) {
45+
out = truncateMiddle( values[ i%values.length ], 7, '...' );
46+
if ( typeof out !== 'string' ) {
47+
b.fail( 'should return a string' );
48+
}
49+
}
50+
b.toc();
51+
if ( !isString( out ) ) {
52+
b.fail( 'should return a string' );
53+
}
54+
b.pass( 'benchmark finished' );
55+
b.end();
56+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
{{alias}}( str, len, seq )
3+
Truncates the middle UTF-16 code units of a string to return a string
4+
having a specified length.
5+
6+
Parameters
7+
----------
8+
str: string
9+
Input string.
10+
11+
len: integer
12+
Output string length.
13+
14+
seq: string
15+
Custom replacement sequence.
16+
17+
Returns
18+
-------
19+
out: string
20+
Truncated string.
21+
22+
Examples
23+
--------
24+
> var str = 'beep boop';
25+
> var out = {{alias}}( str, 5, '...' )
26+
'b...p'
27+
> out = {{alias}}( str, 5, '|' )
28+
'be|op'
29+
30+
See Also
31+
--------
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
// TypeScript Version: 4.1
20+
21+
/**
22+
* Truncates the middle UTF-16 code units of a string to return a string having a specified length.
23+
*
24+
* @param str - input string
25+
* @param len - output string length (including sequence)
26+
* @param seq - custom replacement sequence
27+
* @returns truncated string
28+
*
29+
* @example
30+
* var str = 'beep boop';
31+
* var out = truncateMiddle( str, 5, '...' );
32+
* // returns 'b...p'
33+
*
34+
* @example
35+
* var str = 'beep boop';
36+
* var out = truncateMiddle( str, 5, '>>>' );
37+
* // returns 'b>>>p'
38+
*
39+
* @example
40+
* var str = 'beep boop';
41+
* var out = truncateMiddle( str, 10, '...' );
42+
* // returns 'beep boop'
43+
*
44+
* @example
45+
* var str = 'beep boop';
46+
* var out = truncateMiddle( str, 0, '...' );
47+
* // returns ''
48+
*
49+
* @example
50+
* var str = 'beep boop';
51+
* var out = truncateMiddle( str, 2, '...' );
52+
* // returns '..'
53+
*/
54+
declare function truncateMiddle( str: string, len: number, seq: string ): string;
55+
56+
57+
// EXPORTS //
58+
59+
export = truncateMiddle;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
import truncateMiddle = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a string...
25+
{
26+
truncateMiddle( 'abcdefghi', 3, '...' ); // $ExpectType string
27+
}
28+
29+
// The compiler throws an error if the function is not provided a string as its first argument...
30+
{
31+
truncateMiddle( true, 6, '...' ); // $ExpectError
32+
truncateMiddle( false, 6, '...' ); // $ExpectError
33+
truncateMiddle( 3, 6, '...' ); // $ExpectError
34+
truncateMiddle( [], 6, '...' ); // $ExpectError
35+
truncateMiddle( {}, 6, '...' ); // $ExpectError
36+
truncateMiddle( ( x: number ): number => x, 6, '...' ); // $ExpectError
37+
}
38+
39+
// The compiler throws an error if the function is not provided a number as its second argument...
40+
{
41+
truncateMiddle( 'abd', true, '...' ); // $ExpectError
42+
truncateMiddle( 'abd', false, '...' ); // $ExpectError
43+
truncateMiddle( 'abd', 'abc', '...' ); // $ExpectError
44+
truncateMiddle( 'abd', [], '...' ); // $ExpectError
45+
truncateMiddle( 'abd', {}, '...' ); // $ExpectError
46+
truncateMiddle( 'abd', ( x: number ): number => x, '...' ); // $ExpectError
47+
}
48+
49+
// The compiler throws an error if the function is not provided a string as its third argument...
50+
{
51+
truncateMiddle( 'beep boop', 4, true ); // $ExpectError
52+
truncateMiddle( 'beep boop', 4, false ); // $ExpectError
53+
truncateMiddle( 'beep boop', 4, 123 ); // $ExpectError
54+
truncateMiddle( 'beep boop', 4, [], 0 ); // $ExpectError
55+
truncateMiddle( 'beep boop', 4, {}, 0 ); // $ExpectError
56+
truncateMiddle( 'beep boop', 4, ( x: number ): number => x, 0 ); // $ExpectError
57+
}
58+
59+
// The compiler throws an error if the function is provided an unsupported number of arguments...
60+
{
61+
truncateMiddle(); // $ExpectError
62+
truncateMiddle( 'abc', 4, '|', true ); // $ExpectError
63+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
var truncateMiddle = require( './../lib' );
22+
23+
var str = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.';
24+
var out = truncateMiddle( str, 15, '...' );
25+
console.log( out );
26+
// => 'Lorem ... elit.'
27+
28+
str = 'To be or not to be, that is the question';
29+
out = truncateMiddle( str, 19, '|' );
30+
console.log( out );
31+
// => 'To be or | question'
32+
33+
str = 'The quick fox jumps over the lazy dog.';
34+
out = truncateMiddle( str, 28, '...' );
35+
console.log( out );
36+
// => 'The quick fox...he lazy dog.'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
/**
22+
* Truncate the middle UTF-16 code units of a string to return a string having a specified length.
23+
*
24+
* @module @stdlib/string/base/truncate-middle
25+
*
26+
* @example
27+
* var truncateMiddle = require( '@stdlib/string/base/truncate-middle' );
28+
*
29+
* var out = truncateMiddle( 'beep boop', 7, '...' );
30+
* // returns 'be...op'
31+
*
32+
* out = truncateMiddle( 'beep boop', 7, '|' );
33+
* // returns 'bee|oop'
34+
*/
35+
36+
// MODULES //
37+
38+
var main = require( './main.js' );
39+
40+
41+
// EXPORTS //
42+
43+
module.exports = main;

0 commit comments

Comments
 (0)