Skip to content

Commit

Permalink
feat: refactor string/remove-last
Browse files Browse the repository at this point in the history
This commit adds 3 new base string packages for removing code units, code points, and grapheme clusters, respectively. This commit subsequently refactors `string/remove-last` to depend on those base packages. As a consequence, a new option has been added to `string/remove-last` to select which processing "mode" is desired in order to balance performance considerations.

Additionally, this commit fixes a bug in `string/remove-first` due to an off-by-one indexing error.

Lastly, this commit fixes the `name` field in `string/base/remove-first*` `package.json` files.

PR-URL: 	#1079
Co-authored-by: Athan Reines <kgryte@gmail.com>
Reviewed-by: Athan Reines <kgryte@gmail.com> 
Ref: #1062
  • Loading branch information
steff456 and kgryte authored Sep 21, 2023
1 parent 4d7c4c3 commit 982de75
Show file tree
Hide file tree
Showing 47 changed files with 2,506 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ function removeFirst( str, n ) {
break;
}
}
return str.substring( cnt, str.length );
return str.substring( i + 1, str.length );
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@stdlib/string/base/first-code-point",
"name": "@stdlib/string/base/remove-first-code-point",
"version": "0.0.0",
"description": "Remove the first Unicode code point of a string.",
"license": "Apache-2.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ tape( 'the function removes the first Unicode code point of a provided string (U
out = removeFirst( '六书/六書', 1 );
t.strictEqual( out, '书/六書', 'returns expected value' );

out = removeFirst( '𐒻𐓟', 1 );
t.strictEqual( out, '𐓟', 'returns expected value' );

out = removeFirst( '\uD800', 1 );
t.strictEqual( out, '', 'returns expected value' );

t.end();
});

Expand All @@ -92,5 +98,8 @@ tape( 'the function supports removing the first `n` Unicode code points of a pro
out = removeFirst( '六书/六書', 3 );
t.strictEqual( out, '六書', 'returns expected value' );

out = removeFirst( '𐓟𐒻𐓟', 2 );
t.strictEqual( out, '𐓟', 'returns expected value' );

t.end();
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@stdlib/string/base/first-grapheme-cluster",
"name": "@stdlib/string/base/remove-first-grapheme-cluster",
"version": "0.0.0",
"description": "Remove the first grapheme cluster (i.e., user-perceived character) of a string.",
"license": "Apache-2.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@stdlib/string/base/first",
"name": "@stdlib/string/base/remove-first",
"version": "0.0.0",
"description": "Remove the first UTF-16 code unit of a string.",
"license": "Apache-2.0",
Expand Down
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.
-->

# removeLastCodePoint

> Remove the last `n` Unicode code points of a string.
<section class="usage">

## Usage

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

#### removeLastCodePoint( str, n )

Removes the last `n` Unicode code points of a string.

```javascript
var out = removeLastCodePoint( 'last man standing', 1 );
// returns 'last man standin'

out = removeLastCodePoint( 'Hidden Treasures', 1 );
// returns 'Hidden Treasure'

out = removeLastCodePoint( 'foo bar', 5 );
// returns 'fo'

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

</section>

<!-- /.usage -->

<section class="examples">

## Examples

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

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

var str = removeLastCodePoint( 'presidential election', 1 );
// returns 'presidential electio'

str = removeLastCodePoint( 'JavaScript', 1 );
// returns 'JavaScrip'

str = removeLastCodePoint( 'The Last of the Mohicans', 5 );
// returns 'The Last of the Moh'

str = removeLastCodePoint( 'अनुच्छेद', 1 );
// 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 removeLast = 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 = removeLast( 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,29 @@

{{alias}}( str, n )
Removes the last `n` Unicode code points of a string.

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

n: integer
Number of Unicode code points to remove.

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

Examples
--------
> var out = {{alias}}( 'beep', 1 )
'bee'
> out = {{alias}}( 'Boop', 1 )
'Boo'
> out = {{alias}}( 'foo bar', 5 )
'fo'

See Also
--------

Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* @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

/**
* Removes the last `n` Unicode code points of a string.
*
* @param str - input string
* @param n - number of code points to remove
* @returns output string
*
* @example
* var out = removeLast( 'last man standing', 1 );
* // returns 'last man standin'
*
* @example
* var out = removeLast( 'presidential election', 1 );
* // returns 'presidential electio'
*
* @example
* var out = removeLast( 'JavaScript', 1 );
* // returns 'JavaScrip'
*
* @example
* var out = removeLast( 'Hidden Treasures', 1 );
* // returns 'Hidden Treasure'
*
* @example
* var out = removeLast( 'foo bar', 5 );
* // returns 'fo'
*/
declare function removeLast( str: string, n: number ): string;


// EXPORTS //

export = removeLast;
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* @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 removeLast = require( './index' );


// TESTS //

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

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

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

// The compiler throws an error if the function is provided an unsupported number of arguments...
{
removeLast(); // $ExpectError
removeLast( 'abc' ); // $ExpectError
removeLast( 'abc', 1, 2 ); // $ExpectError
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* @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 removeLastCodePoint = require( './../lib' );

console.log( removeLastCodePoint( 'presidential election', 1 ) );
// => 'presidential electio'

console.log( removeLastCodePoint( 'JavaScript', 1 ) );
// => 'JavaScrip'

console.log( removeLastCodePoint( 'The Last of the Mohicans', 5 ) );
// => 'The Last of the Moh'

console.log( removeLastCodePoint( 'अनुच्छेद', 1 ) );
// => 'अनुच्छे'
Loading

1 comment on commit 982de75

@stdlib-bot
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage Report

Package Statements Branches Functions Lines
string/base/remove-first-code-point $\color{green}137/137$
$\color{green}+100.00\%$
$\color{green}12/12$
$\color{green}+100.00\%$
$\color{green}1/1$
$\color{green}+100.00\%$
$\color{green}137/137$
$\color{green}+100.00\%$
string/base/remove-first-grapheme-cluster $\color{green}121/121$
$\color{green}+100.00\%$
$\color{green}7/7$
$\color{green}+100.00\%$
$\color{green}1/1$
$\color{green}+100.00\%$
$\color{green}121/121$
$\color{green}+100.00\%$
string/base/remove-first $\color{green}96/96$
$\color{green}+100.00\%$
$\color{green}3/3$
$\color{green}+100.00\%$
$\color{green}1/1$
$\color{green}+100.00\%$
$\color{green}96/96$
$\color{green}+100.00\%$
string/base/remove-last-code-point $\color{green}137/137$
$\color{green}+100.00\%$
$\color{green}12/12$
$\color{green}+100.00\%$
$\color{green}1/1$
$\color{green}+100.00\%$
$\color{green}137/137$
$\color{green}+100.00\%$
string/base/remove-last-grapheme-cluster $\color{green}132/132$
$\color{green}+0.00\%$
$\color{green}10/10$
$\color{green}+0.00\%$
$\color{green}1/1$
$\color{green}+0.00\%$
$\color{green}132/132$
$\color{green}+0.00\%$
string/base/remove-last $\color{green}96/96$
$\color{green}+100.00\%$
$\color{green}3/3$
$\color{green}+100.00\%$
$\color{green}1/1$
$\color{green}+100.00\%$
$\color{green}96/96$
$\color{green}+100.00\%$
string/remove-first $\color{green}177/177$
$\color{green}+0.00\%$
$\color{green}20/20$
$\color{green}+0.00\%$
$\color{green}1/1$
$\color{green}+0.00\%$
$\color{green}177/177$
$\color{green}+0.00\%$
string/remove-last $\color{green}173/173$
$\color{green}+0.00\%$
$\color{green}20/20$
$\color{green}+0.00\%$
$\color{green}1/1$
$\color{green}+0.00\%$
$\color{green}173/173$
$\color{green}+0.00\%$

The above coverage report was generated for the changes in this push.

Please sign in to comment.