Skip to content

Commit

Permalink
Auto-generated commit
Browse files Browse the repository at this point in the history
  • Loading branch information
stdlib-bot committed Mar 21, 2024
1 parent 4546f43 commit 81dc0f3
Show file tree
Hide file tree
Showing 11 changed files with 678 additions and 0 deletions.
1 change: 1 addition & 0 deletions CONTRIBUTORS
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Anudeep Sanapala <[email protected]>
Athan Reines <[email protected]>
Brendan Graetz <[email protected]>
Bruno Fenzl <[email protected]>
Bryan Elee <[email protected]>
Chinmay Joshi <[email protected]>
Christopher Dambamuromo <[email protected]>
Dan Rose <[email protected]>
Expand Down
95 changes: 95 additions & 0 deletions base/last-grapheme-cluster/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<!--
@license Apache-2.0
Copyright (c) 2024 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.
-->

# lastGraphemeCluster

> Return the last `n` grapheme clusters (i.e., user-perceived characters) of a string.
<section class="usage">

## Usage

```javascript
var lastGraphemeCluster = require( '@stdlib/string/base/last-grapheme-cluster' );
```

#### lastGraphemeCluster( str, n )

Returns the last `n` grapheme clusters (i.e., user-perceived characters) of a string.

```javascript
var out = lastGraphemeCluster( 'Hello World', 1 );
// returns 'd'

out = lastGraphemeCluster( 'Evening', 3 );
// returns 'ing'

out = lastGraphemeCluster( 'foo bar', 10 );
// returns 'foo bar'
```

</section>

<!-- /.usage -->

<section class="examples">

## Examples

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

```javascript
var lastGraphemeCluster = require( '@stdlib/string/base/last-grapheme-cluster' );

var str = lastGraphemeCluster( 'Hello World!', 1 );
// returns '!'

str = lastGraphemeCluster( 'JavaScript', 6 );
// returns 'Script'

str = lastGraphemeCluster( 'stdlib', 10 );
// returns 'stdlib'

str = lastGraphemeCluster( '🐶🐮🐷🐰🐸', 2 );
// returns '🐰🐸'

str = lastGraphemeCluster( '六书/六書', 2 );
// 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 -->
58 changes: 58 additions & 0 deletions base/last-grapheme-cluster/benchmark/benchmark.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2024 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 last = 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 = last( 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();
});
30 changes: 30 additions & 0 deletions base/last-grapheme-cluster/docs/repl.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

{{alias}}( str, n )
Returns the last `n` grapheme clusters (i.e., user-perceived characters) of
a string.

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

n: integer
Number of grapheme clusters to return.

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

Examples
--------
> var out = {{alias}}( 'beep', 1 )
'p'
> out = {{alias}}( 'Boop', 2 )
'op'
> out = {{alias}}( 'JavaScript', 6 )
'Script'

See Also
--------

53 changes: 53 additions & 0 deletions base/last-grapheme-cluster/docs/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* @license Apache-2.0
*
* Copyright (c) 2024 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

/**
* Returns the last `n` grapheme clusters (i.e., user-perceived characters) of a string.
*
* @param str - input string
* @param n - number of grapheme clusters to return
* @returns output string
*
* @example
* var out = last( 'Hello World', 1 );
* // returns 'd'
*
* @example
* var out = last( 'Evening', 3 );
* // returns 'ing'
*
* @example
* var out = last( 'JavaScript', 6 );
* // returns 'Script'
*
* @example
* var out = last( '🐶🐮🐷🐰🐸', 2 );
* // returns '🐰🐸'
*
* @example
* var out = last( 'foo bar', 5 );
* // returns 'o bar'
*/
declare function last( str: string, n: number ): string;


// EXPORTS //

export = last;
57 changes: 57 additions & 0 deletions base/last-grapheme-cluster/docs/types/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* @license Apache-2.0
*
* Copyright (c) 2024 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 last = require( './index' );


// TESTS //

// The function returns a string...
{
last( 'abc', 1 ); // $ExpectType string
}

// The compiler throws an error if the function is provided a value other than a string...
{
last( true, 1 ); // $ExpectError
last( false, 1 ); // $ExpectError
last( null, 1 ); // $ExpectError
last( undefined, 1 ); // $ExpectError
last( 5, 1 ); // $ExpectError
last( [], 1 ); // $ExpectError
last( {}, 1 ); // $ExpectError
last( ( x: number ): number => x, 1 ); // $ExpectError
}

// The compiler throws an error if the function is provided a second argument that is not a number...
{
last( 'abc', true ); // $ExpectError
last( 'abc', false ); // $ExpectError
last( 'abc', null ); // $ExpectError
last( 'abc', 'abc' ); // $ExpectError
last( 'abc', [] ); // $ExpectError
last( 'abc', {} ); // $ExpectError
last( 'abc', ( x: number ): number => x ); // $ExpectError
}

// The compiler throws an error if the function is provided an unsupported number of arguments...
{
last(); // $ExpectError
last( 'abc' ); // $ExpectError
last( 'abc', 1, 2 ); // $ExpectError
}
36 changes: 36 additions & 0 deletions base/last-grapheme-cluster/examples/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2024 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 lastGraphemeCluster = require( './../lib' );

console.log( lastGraphemeCluster( 'Hello World!', 1 ) );
// => '!'

console.log( lastGraphemeCluster( 'JavaScript', 6 ) );
// => 'Script'

console.log( lastGraphemeCluster( 'stdlib', 10 ) );
// => 'stdlib'

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

console.log( lastGraphemeCluster( '六书/六書', 2 ) );
// => '六書'
46 changes: 46 additions & 0 deletions base/last-grapheme-cluster/lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2024 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';

/**
* Return the last `n` grapheme clusters (i.e., user-perceived characters) of a string.
*
* @module @stdlib/string/base/last-grapheme-cluster
*
* @example
* var last = require( '@stdlib/string/base/last-grapheme-cluster' );
*
* var out = last( 'Hello', 1 );
* // returns 'o';
*
* out = last( 'JavaScript', 1 );
* // returns 'Script';
*
* out = last( '🐮🐷🐸🐵', 2 );
* // returns '🐸🐵';
*/

// MODULES //

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


// EXPORTS //

module.exports = main;
Loading

0 comments on commit 81dc0f3

Please sign in to comment.