Skip to content

Commit

Permalink
feat: add truncate string packages
Browse files Browse the repository at this point in the history
  • Loading branch information
steff456 committed Sep 13, 2023
1 parent aeb94f4 commit 9a94c69
Show file tree
Hide file tree
Showing 56 changed files with 3,970 additions and 93 deletions.
99 changes: 99 additions & 0 deletions lib/node_modules/@stdlib/string/base/truncate-code-point/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<!--
@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.
-->

# truncateCodePoint

> Truncate code points of a string to a specified length.
<section class="usage">

## Usage

```javascript
var truncateCodePoint = require( '@stdlib/string/base/truncate-code-point' );
```

#### truncateCodePoint( str, len\[, ending] )

Truncates code points of a string to a specified length.

```javascript
var out = truncateCodePoint( 'beep boop', 7 );
// returns 'beep...'
```

By default, the truncated string is appended with `'...'`. To customize the truncated string, provide an `ending` argument:

```javascript
var out = truncateCodePoint( 'beep boop', 7, '!' );
// returns 'beep b!'

out = truncateCodePoint( 'beep boop', 7, '!!!' );
// returns 'beep!!!'
```

</section>

<!-- /.usage -->

<section class="examples">

## Examples

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

```javascript
var truncate = require( '@stdlib/string/truncate' );

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

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

str = 'The quick fox jumps over the lazy dog.';
out = truncateCodePoint( str, 16, '...' );
// returns 'The quick fox...'

out = truncateCodePoint( 'अनुच्छेद', 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 -->
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 ], 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();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@

{{alias}}( str, len[, ending] )
Truncate code points of a string to a specified length.

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

len: integer
Output string length.

ending: string (optional)
Custom ending. Default: '...'.

Returns
-------
out: string
Output string.

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

> out = {{alias}}( str, 5, '|' )
'beep|'

See Also
--------

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

/**
* Truncate code points of a string to a specified length.
*
* @param str - input string
* @param len - output string length (including ending)
* @param ending - custom ending (default: `...`)
* @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;
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* @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( 'abc', 1 ); // $ExpectType 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
}
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 );
// => 'अनुच...'
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 code points of a string to a specified length.
*
* @module @stdlib/string/base/truncate-code-point
*
* @example
* var truncate = require( '@stdlib/string/base/truncate-code-point' );
*
* 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;
Loading

0 comments on commit 9a94c69

Please sign in to comment.