Skip to content

feat: add string/base/last #1697

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

Merged
merged 4 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 100 additions & 0 deletions lib/node_modules/@stdlib/string/base/last/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<!--

@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.

-->

# last

> Return the last `n` UTF-16 code units of a string.

<section class="usage">

## Usage

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

#### last( str, n )

Returns the last `n` UTF-16 code units of a string.

```javascript
var s = last( 'hello world', 1 );
// returns 'd'

s = last( 'this is stdlib', 1 );
// returns 'b'

s = last( 'foo', 2 );
// returns 'oo'

s = last( 'foo bar', 10 );
// returns 'foo bar'
```

</section>

<!-- /.usage -->

<section class="examples">

## Examples

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

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

var str = last( 'elections', 1 );
// returns 's'

str = last( 'JavaScript', 1 );
// returns 't'

str = last( 'good night', 5 );
// returns 'night'
```

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

<!-- <related-links> -->


<!-- </related-links> -->

</section>

<!-- /.links -->
60 changes: 60 additions & 0 deletions lib/node_modules/@stdlib/string/base/last/benchmark/benchmark.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* @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 = [
'hello world',
'abc',
'',
'Javascript',
'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();
});
29 changes: 29 additions & 0 deletions lib/node_modules/@stdlib/string/base/last/docs/repl.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

{{alias}}( str, n )
Returns the last `n` UTF-16 code units of a string.

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

n: integer
Number of UTF-16 code units to return.

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

Examples
--------
> var out = {{alias}}( 'hello', 1 )
'o'
> out = {{alias}}( 'JavaScript', 6 )
'Script'
> out = {{alias}}( 'foo bar', 10 )
'foo bar'

See Also
--------

49 changes: 49 additions & 0 deletions lib/node_modules/@stdlib/string/base/last/docs/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* @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` UTF-16 code units of a string.
*
* @param str - input string
* @param n - number of code units to return
* @returns output string
*
* @example
* var s = last( 'hello world', 1 );
* // returns 'd'
*
* @example
* var s = last( 'foo', 2 );
* // returns 'oo'
*
* @example
* var s = last( 'JavaScript', 6 );
* // returns 'Script'
*
* @example
* var s = last( 'foo bar', 10 );
* // returns 'foo bar'
*/
declare function last( str: string, n: number ): string;


// EXPORTS //

export = last;
57 changes: 57 additions & 0 deletions lib/node_modules/@stdlib/string/base/last/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
}
33 changes: 33 additions & 0 deletions lib/node_modules/@stdlib/string/base/last/examples/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* @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 last = require( './../lib' );

console.log( last( 'hello world', 1 ) );
// => 'd'

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

console.log( last( 'New', 6 ) );
// => 'New'

console.log( last( 'EVENING', 5 ) );
// => 'ENING'
43 changes: 43 additions & 0 deletions lib/node_modules/@stdlib/string/base/last/lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* @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` UTF-16 code units of a string.
*
* @module @stdlib/string/base/last
*
* @example
* var last = require( '@stdlib/string/base/last' );
*
* var s = last( 'hello world', 1 );
* // returns 'd'
*
* var out = last( 'JavaScript', 6 );
* // returns 'Script'
*/

// MODULES //

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


// EXPORTS //

module.exports = main;
Loading