Skip to content

Commit

Permalink
feat: add "base" string packages and refactor string/remove-first
Browse files Browse the repository at this point in the history
This commit adds the following new packages:

- `@stdlib/string/base/remove-first`
- `@stdlib/string/base/remove-first-code-point`
- `@stdlib/string/base/remove-first-grapheme-cluster`

The top-level `@stdlib/string/remove-first` is refactored to depend on those "base" packages, with the default behavior remaining the same (i.e., removing the first grapheme cluster) and a new "mode" option for specifying what type of character to remove.

PR-URL: 	#1073
Co-authored-by: Athan Reines <kgryte@gmail.com>
Reviewed-by: Athan Reines <kgryte@gmail.com> 
Ref: #1062
  • Loading branch information
steff456 and kgryte authored Aug 4, 2023
1 parent e09914b commit d1ded85
Show file tree
Hide file tree
Showing 41 changed files with 2,491 additions and 40 deletions.
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.
-->

# removeFirstCodePoint

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

## Usage

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

#### removeFirstCodePoint( str, n )

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

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

out = removeFirstCodePoint( 'Hidden Treasures', 1 );
// returns 'idden Treasures'

out = removeFirstCodePoint( 'foo bar', 5 );
// returns 'ar'

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

</section>

<!-- /.usage -->

<section class="examples">

## Examples

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

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

var str = removeFirstCodePoint( 'presidential election', 1 );
// returns 'residential election'

str = removeFirstCodePoint( 'JavaScript', 1 );
// returns 'avaScript'

str = removeFirstCodePoint( 'The Last of the Mohicans', 5 );
// returns 'ast of the Mohicans'

str = removeFirstCodePoint( 'अनुच्छेद', 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 removeFirst = 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 = removeFirst( 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 first `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 )
'eep'
> out = {{alias}}( 'Boop', 1 )
'oop'
> out = {{alias}}( 'foo bar', 5 )
'ar'

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 first `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 = removeFirst( 'last man standing', 1 );
* // returns 'ast man standing'
*
* @example
* var out = removeFirst( 'presidential election', 1 );
* // returns 'residential election'
*
* @example
* var out = removeFirst( 'JavaScript', 1 );
* // returns 'avaScript'
*
* @example
* var out = removeFirst( 'Hidden Treasures', 1 );
* // returns 'idden Treasures'
*
* @example
* var out = removeFirst( 'foo bar', 5 );
* // returns 'ar'
*/
declare function removeFirst( str: string, n: number ): string;


// EXPORTS //

export = removeFirst;
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 removeFirst = require( './index' );


// TESTS //

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

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

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

// The compiler throws an error if the function is provided an unsupported number of arguments...
{
removeFirst(); // $ExpectError
removeFirst( 'abc' ); // $ExpectError
removeFirst( '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 removeFirstCodePoint = require( './../lib' );

console.log( removeFirstCodePoint( 'presidential election', 1 ) );
// => 'residential election'

console.log( removeFirstCodePoint( 'JavaScript', 1 ) );
// => 'avaScript'

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

console.log( removeFirstCodePoint( 'अनुच्छेद', 1 ) );
// => 'नुच्छेद'
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';

/**
* Remove the first `n` Unicode code points of a string.
*
* @module @stdlib/string/base/remove-first-code-point
*
* @example
* var removeFirst = require( '@stdlib/string/base/remove-first-code-point' );
*
* var out = removeFirst( 'last man standing', 1 );
* // returns 'ast man standing'
*
* out = removeFirst( 'Hidden Treasures', 1 );
* // returns 'idden Treasures';
*/

// MODULES //

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


// EXPORTS //

module.exports = main;
Loading

0 comments on commit d1ded85

Please sign in to comment.