-
-
Notifications
You must be signed in to change notification settings - Fork 836
feat: refactor truncate string package #1097
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
18
commits into
develop
Choose a base branch
from
truncate
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
18 commits
Select commit
Hold shift + click to select a range
9a94c69
feat: add truncate string packages
steff456 c85a6df
fix: rename base packages
steff456 9a428dc
docs: fix readme import
steff456 e306508
docs: fix readme example import
steff456 31cb844
docs: add missing import in readme example
steff456 dbc41cc
docs: update truncate-code-points description
steff456 ed5c70e
fix: remove variadic interfaces in base truncate packages
steff456 020a675
Apply suggestions from code review
kgryte 64c2fc7
Apply suggestions from code review
kgryte dfe50f7
Apply suggestions from code review
kgryte 5f56e1d
docs: update package description and update benchmark
steff456 a90b498
Apply suggestions from code review
kgryte 45a9001
Apply suggestions from code review
kgryte 695a9f4
Apply suggestions from code review
kgryte 976eefa
Apply suggestions from code review
kgryte 27e7e30
fix: add review changes
steff456 a124837
Apply suggestions from code review
kgryte 9e69e38
Apply suggestions from code review
kgryte 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
95 changes: 95 additions & 0 deletions
95
lib/node_modules/@stdlib/string/base/truncate-code-points/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,95 @@ | ||
<!-- | ||
|
||
@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. | ||
|
||
--> | ||
|
||
# truncateCodePoints | ||
|
||
> Truncate the Unicode code points of a provided string in order to return a string having a specified number of Unicode code points. | ||
|
||
<section class="usage"> | ||
|
||
## Usage | ||
|
||
```javascript | ||
var truncateCodePoints = require( '@stdlib/string/base/truncate-code-points' ); | ||
``` | ||
|
||
#### truncateCodePoints( str, len, ending ) | ||
|
||
Truncates the Unicode code points of a provided string in order to return a string having a specified number of Unicode code points. | ||
|
||
```javascript | ||
var out = truncateCodePoints( 'beep boop', 7, '...' ); | ||
// returns 'beep...' | ||
|
||
out = truncateCodePoints( 'beep boop', 7, '!' ); | ||
// returns 'beep b!' | ||
|
||
out = truncateCodePoints( 'beep boop', 7, '!!!' ); | ||
// returns 'beep!!!' | ||
``` | ||
|
||
</section> | ||
|
||
<!-- /.usage --> | ||
|
||
<section class="examples"> | ||
|
||
## Examples | ||
|
||
<!-- eslint no-undef: "error" --> | ||
|
||
```javascript | ||
var truncateCodePoints = require( '@stdlib/string/base/truncate-code-points' ); | ||
|
||
var str = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.'; | ||
var out = truncateCodePoints( str, 14, '...' ); | ||
// returns 'Lorem ipsum...' | ||
|
||
str = 'To be or not to be, that is the question'; | ||
out = truncateCodePoints( str, 19, '!' ); | ||
// returns 'To be or not to be!' | ||
|
||
str = 'The quick fox jumps over the lazy dog.'; | ||
out = truncateCodePoints( str, 16, '...' ); | ||
// returns 'The quick fox...' | ||
|
||
out = truncateCodePoints( 'अनुच्छेद', 7, '...' ); | ||
// returns 'अनुच...' | ||
``` | ||
|
||
</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-code-points/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 truncate = 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 = truncate( 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(); | ||
}); |
32 changes: 32 additions & 0 deletions
32
lib/node_modules/@stdlib/string/base/truncate-code-points/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,32 @@ | ||
|
||
{{alias}}( str, len, ending ) | ||
Truncate the Unicode code points of a provided string in order to return a | ||
string having a specified number of Unicode code points. | ||
|
||
Parameters | ||
---------- | ||
str: string | ||
Input string. | ||
|
||
len: integer | ||
Output string length. | ||
|
||
ending: string | ||
Custom ending. | ||
|
||
Returns | ||
------- | ||
out: string | ||
Output string. | ||
|
||
Examples | ||
-------- | ||
> var str = 'beep boop'; | ||
> var out = {{alias}}( str, 5, '...' ) | ||
'be...' | ||
> out = {{alias}}( str, 5, '|' ) | ||
'beep|' | ||
|
||
See Also | ||
-------- | ||
|
42 changes: 42 additions & 0 deletions
42
lib/node_modules/@stdlib/string/base/truncate-code-points/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,42 @@ | ||
/* | ||
* @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: 2.0 | ||
|
||
/** | ||
* Truncates the Unicode code points of a provided string in order to return a string having a specified number of Unicode code points. | ||
* | ||
* @param str - input string | ||
* @param len - output string length (including ending) | ||
* @param ending - custom ending | ||
* @returns truncated string | ||
* | ||
* @example | ||
* var out = truncate( 'beep boop', 7, '...' ); | ||
* // returns 'beep...' | ||
* | ||
* @example | ||
* var out = truncate( 'beep boop', 7, '|' ); | ||
* // returns 'beep b|' | ||
*/ | ||
declare function truncate( str: string, len: number, ending: string ): string; | ||
|
||
|
||
// EXPORTS // | ||
|
||
export = truncate; |
68 changes: 68 additions & 0 deletions
68
lib/node_modules/@stdlib/string/base/truncate-code-points/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,68 @@ | ||
/* | ||
* @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 truncate = require( './index' ); | ||
|
||
|
||
// TESTS // | ||
|
||
// The function returns a string... | ||
{ | ||
truncate( 'abcdefghi', 10, '|' ); // $ExpectType string | ||
} | ||
|
||
// The compiler throws an error if the function is provided a value other than a string as its first argument... | ||
{ | ||
truncate( true, 1, '...' ); // $ExpectError | ||
truncate( false, 1, '...' ); // $ExpectError | ||
truncate( null, 1, '...' ); // $ExpectError | ||
truncate( undefined, 1, '...' ); // $ExpectError | ||
truncate( 5, 1, '...' ); // $ExpectError | ||
truncate( [], 1, '...' ); // $ExpectError | ||
truncate( {}, 1, '...' ); // $ExpectError | ||
truncate( ( x: number ): number => x, 1, '...' ); // $ExpectError | ||
} | ||
|
||
// The compiler throws an error if the function is provided a second argument that is not a number... | ||
{ | ||
truncate( 'abc', true, '...' ); // $ExpectError | ||
truncate( 'abc', false, '...' ); // $ExpectError | ||
truncate( 'abc', null, '...' ); // $ExpectError | ||
truncate( 'abc', 'abc', '...' ); // $ExpectError | ||
truncate( 'abc', [], '...' ); // $ExpectError | ||
truncate( 'abc', {}, '...' ); // $ExpectError | ||
truncate( 'abc', ( x: number ): number => x, '...' ); // $ExpectError | ||
} | ||
|
||
// The compiler throws an error if the function is provided a third argument that is not a string... | ||
{ | ||
truncate( 'beep boop', 4, true ); // $ExpectError | ||
truncate( 'beep boop', 4, false ); // $ExpectError | ||
truncate( 'beep boop', 4, null ); // $ExpectError | ||
truncate( 'beep boop', 4, 123 ); // $ExpectError | ||
truncate( 'beep boop', 4, [], 0 ); // $ExpectError | ||
truncate( 'beep boop', 4, {}, 0 ); // $ExpectError | ||
truncate( 'beep boop', 4, ( x: number ): number => x, 0 ); // $ExpectError | ||
} | ||
|
||
// The compiler throws an error if the function is provided an unsupported number of arguments... | ||
{ | ||
truncate(); // $ExpectError | ||
truncate( 'abc' ); // $ExpectError | ||
truncate( 'abc', 1, '.', true ); // $ExpectError | ||
} |
41 changes: 41 additions & 0 deletions
41
lib/node_modules/@stdlib/string/base/truncate-code-points/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,41 @@ | ||
/** | ||
* @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 truncate = require( './../lib' ); | ||
|
||
var str = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.'; | ||
var out = truncate( str, 14, '...' ); | ||
console.log( out ); | ||
// => 'Lorem ipsum...' | ||
|
||
str = 'To be or not to be, that is the question'; | ||
out = truncate( str, 19, '!' ); | ||
console.log( out ); | ||
// => 'To be or not to be!' | ||
|
||
str = 'The quick fox jumps over the lazy dog.'; | ||
out = truncate( str, 16, '...' ); | ||
console.log( out ); | ||
// => 'The quick fox...' | ||
|
||
str = 'अनुच्छेद'; | ||
out = truncate( str, 7, '...' ); | ||
console.log( out ); | ||
// => 'अनुच...' |
43 changes: 43 additions & 0 deletions
43
lib/node_modules/@stdlib/string/base/truncate-code-points/lib/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,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 Unicode code points of a provided string in order to return a string having a specified number of Unicode code points. | ||
* | ||
* @module @stdlib/string/base/truncate-code-points | ||
* | ||
* @example | ||
* var truncate = require( '@stdlib/string/base/truncate-code-points' ); | ||
* | ||
* var out = truncate( 'beep boop', 7, '...' ); | ||
* // returns 'beep...' | ||
* | ||
* out = truncate( 'beep boop', 7, '|' ); | ||
* // returns 'beep b|' | ||
*/ | ||
|
||
// MODULES // | ||
|
||
var main = require( './main.js' ); | ||
|
||
|
||
// EXPORTS // | ||
|
||
module.exports = main; |
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.
Uh oh!
There was an error while loading. Please reload this page.