-
-
Notifications
You must be signed in to change notification settings - Fork 806
feat: add truncate-middle-grapheme-cluster string package #1120
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
steff456
wants to merge
4
commits into
develop
Choose a base branch
from
truncate-middle-grapheme-cluster
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
98 changes: 98 additions & 0 deletions
98
lib/node_modules/@stdlib/string/base/truncate-middle-grapheme-cluster/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
<!-- | ||
|
||
@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. | ||
|
||
--> | ||
|
||
# truncateMiddleGraphemeCluster | ||
|
||
> Truncate grapheme clusters of string in the middle to a specified length. | ||
|
||
<section class="usage"> | ||
|
||
## Usage | ||
|
||
<!-- eslint-disable id-length --> | ||
|
||
```javascript | ||
var truncateMiddleGraphemeCluster = require( '@stdlib/string/base/truncate-middle-grapheme-cluster' ); | ||
``` | ||
|
||
#### truncateMiddleGraphemeCluster( str, len, seq ) | ||
|
||
Truncates grapheme clusters of string in the middle to a specified length. | ||
|
||
<!-- eslint-disable id-length --> | ||
|
||
```javascript | ||
var out = truncateMiddleGraphemeCluster( 'beep boop', 7, '...' ); | ||
// returns 'be...op' | ||
|
||
out = truncateMiddleGraphemeCluster( 'beep boop', 7, '!' ); | ||
// returns 'bee!oop' | ||
|
||
out = truncateMiddleGraphemeCluster( 'beep boop', 7, '!!!' ); | ||
// returns 'be!!!op' | ||
``` | ||
|
||
</section> | ||
|
||
<!-- /.usage --> | ||
|
||
<section class="examples"> | ||
|
||
## Examples | ||
|
||
<!-- eslint no-undef: "error" --> | ||
|
||
<!-- eslint-disable id-length --> | ||
|
||
```javascript | ||
var truncateMiddleGraphemeCluster = require( '@stdlib/string/base/truncate-middle-grapheme-cluster' ); | ||
|
||
var str = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.'; | ||
var out = truncateMiddleGraphemeCluster( str, 15, '...' ); | ||
// returns 'Lorem ... elit.' | ||
|
||
str = 'To be or not to be, that is the question'; | ||
out = truncateMiddleGraphemeCluster( str, 19, '|' ); | ||
// returns 'To be or | question' | ||
|
||
str = 'The quick fox jumps over the lazy dog.'; | ||
out = truncateMiddleGraphemeCluster( 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 --> |
56 changes: 56 additions & 0 deletions
56
lib/node_modules/@stdlib/string/base/truncate-middle-grapheme-cluster/benchmark/benchmark.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ], 1, '...' ); | ||
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
31
lib/node_modules/@stdlib/string/base/truncate-middle-grapheme-cluster/docs/repl.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
|
||
{{alias}}( str, len, seq ) | ||
Truncate grapheme clusters of string in the middle to 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 | ||
-------- |
59 changes: 59 additions & 0 deletions
59
lib/node_modules/@stdlib/string/base/truncate-middle-grapheme-cluster/docs/types/index.d.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
/** | ||
* Truncate grapheme clusters of string in the middle to 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; // tslint-disable-line max-line-length | ||
|
||
|
||
// EXPORTS // | ||
|
||
export = truncateMiddle; |
64 changes: 64 additions & 0 deletions
64
lib/node_modules/@stdlib/string/base/truncate-middle-grapheme-cluster/docs/types/test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* @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 | ||
truncateMiddle( 'abcdefghi', 10, '|' ); // $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 | ||
} |
51 changes: 51 additions & 0 deletions
51
lib/node_modules/@stdlib/string/base/truncate-middle-grapheme-cluster/examples/index.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/** | ||
* @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.' | ||
|
||
str = 'अनुच्छेद'; | ||
out = truncateMiddle( str, 5, '...' ); | ||
console.log( out ); | ||
// => 'अ...छेद' | ||
|
||
str = '🐺 Wolf Brothers 🐺'; | ||
out = truncateMiddle( str, 7, '...' ); | ||
console.log( out ); | ||
// => '🐺 ... 🐺' | ||
|
||
str = '🐺 Wolf Pack 🐺'; | ||
out = truncateMiddle( str, 7, '🐺🐺🐺' ); | ||
console.log( out ); | ||
// => '🐺 🐺🐺🐺 🐺' |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same description change as in other PRs.