Skip to content
Draft
104 changes: 104 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,104 @@
<!--

@license Apache-2.0

Copyright (c) 2026 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.

-->

# truncate

> Truncate a string to a specified length in code points.

<section class="usage">

## Usage

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

#### truncate( str, len, ending )

Truncates a string to a specified length in code points.

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

out = truncate( 'beep boop', 5, '>>>' );
// returns 'be>>>'
```

</section>

<!-- /.usage -->

<section class="notes">

## Notes

- This function assumes that the input arguments are valid. For input validation, use the top-level [`@stdlib/string/truncate`][@stdlib/string/truncate].

</section>

<!-- /.notes -->

<section class="examples">

## Examples

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

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

console.log( truncate( 'beep boop', 7, '...' ) );
// => 'beep...'

console.log( truncate( 'beep boop', 5, '>>>' ) );
// => 'be>>>'

console.log( truncate( '🐺 Wolf Brothers 🐺', 7, '...' ) );
// => '🐺 Wo...'

console.log( truncate( '🐶🐮🐷🐰🐸', 2, '...' ) );
// => '..'

console.log( truncate( '🐶🐮🐷🐰🐸', 10, '...' ) );
// => '🐶🐮🐷🐰🐸'
```

</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">

[@stdlib/string/truncate]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/string/truncate

</section>

<!-- /.links -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2026 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 randu = require( '@stdlib/random/base/randu' );
var pkg = require( './../package.json' ).name;
var truncate = require( './../lib' );


// MAIN //

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

strings = [
'beep boop',
'foo bar',
'hello world',
'🐺 Wolf Brothers 🐺',
'🐶🐮🐷🐰🐸'
];

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
out = truncate( strings[ i % strings.length ], (randu() * 10)|0, '...' );
if ( typeof out !== 'string' ) {
b.fail( 'should return a string' );
}
}
b.toc();
if ( typeof out !== 'string' ) {
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 )
Truncates a string to a specified length in code points (i.e.,
user-perceived characters).

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

len: NonNegativeInteger
Output string length (including ending).

ending: string
Custom ending.

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

Examples
--------
> var out = {{alias}}( 'beep boop', 7, '...' )
'beep...'
> out = {{alias}}( 'beep boop', 5, '>>>' )
'be>>>'
> out = {{alias}}( 'foo bar', 10, '...' )
'foo bar'

See Also
--------

Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2026 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 a string to a specified length in code points.
*
* @param str - input string
* @param len - output string length
* @param ending - custom ending
* @returns truncated string
*
* @example
* var out = truncate( 'beep boop', 7, '...' );
* // returns 'beep...'
*
* @example
* var out = truncate( 'beep boop', 5, '>>>' );
* // returns 'be>>>'
*/
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,71 @@
/*
* @license Apache-2.0
*
* Copyright (c) 2026 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( 'beep boop', 5, '...' ); // $ExpectType string
}

// The compiler throws an error if the function is provided a first argument which is not a string...
{
truncate( 5, 5, '...' ); // $ExpectError
truncate( false, 5, '...' ); // $ExpectError
truncate( true, 5, '...' ); // $ExpectError
truncate( null, 5, '...' ); // $ExpectError
truncate( undefined, 5, '...' ); // $ExpectError
truncate( [], 5, '...' ); // $ExpectError
truncate( {}, 5, '...' ); // $ExpectError
truncate( ( x: number ): number => x, 5, '...' ); // $ExpectError
}

// The compiler throws an error if the function is provided a second argument which is not a number...
{
truncate( 'beep boop', '5', '...' ); // $ExpectError
truncate( 'beep boop', false, '...' ); // $ExpectError
truncate( 'beep boop', true, '...' ); // $ExpectError
truncate( 'beep boop', null, '...' ); // $ExpectError
truncate( 'beep boop', undefined, '...' ); // $ExpectError
truncate( 'beep boop', [], '...' ); // $ExpectError
truncate( 'beep boop', {}, '...' ); // $ExpectError
truncate( 'beep boop', ( x: number ): number => x, '...' ); // $ExpectError
}

// The compiler throws an error if the function is provided a third argument which is not a string...
{
truncate( 'beep boop', 5, 5 ); // $ExpectError
truncate( 'beep boop', 5, false ); // $ExpectError
truncate( 'beep boop', 5, true ); // $ExpectError
truncate( 'beep boop', 5, null ); // $ExpectError
truncate( 'beep boop', 5, undefined ); // $ExpectError
truncate( 'beep boop', 5, [] ); // $ExpectError
truncate( 'beep boop', 5, {} ); // $ExpectError
truncate( 'beep boop', 5, ( x: number ): number => x ); // $ExpectError
}

// The compiler throws an error if the function is provided an unsupported number of arguments...
{
truncate(); // $ExpectError
truncate( 'beep boop' ); // $ExpectError
truncate( 'beep boop', 5 ); // $ExpectError
truncate( 'beep boop', 5, '...', 10 ); // $ExpectError
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2026 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' );

console.log( truncate( 'beep boop', 7, '...' ) );
// => 'beep...'

console.log( truncate( 'beep boop', 5, '>>>' ) );
// => 'be>>>'

console.log( truncate( '🐺 Wolf Brothers 🐺', 7, '...' ) );
// => '🐺 Wo...'

console.log( truncate( '🐶🐮🐷🐰🐸', 2, '...' ) );
// => '..'

console.log( truncate( '🐶🐮🐷🐰🐸', 10, '...' ) );
// => '🐶🐮🐷🐰🐸'
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2026 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 a string to a specified length in code points.
*
* @module @stdlib/string/base/truncate-code-point
*
* @example
* var truncate = require( '@stdlib/string/base/truncate-code-point' );
*
* var str = truncate( 'beep boop', 7, '...' );
* // returns 'beep...'
*
* str = truncate( 'beep boop', 5, '>>>' );
* // returns 'be>>>'
*/

// MODULES //

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


// EXPORTS //

module.exports = main;
Loading