Skip to content

Commit

Permalink
feat: refactor string/reverse and add mode option
Browse files Browse the repository at this point in the history
This commit also adds the following "base" packages:

- `string/base/reverse`
- `string/base/reverse-code-points`
- `string/base/reverse-grapheme-clusters`

PR-URL: 	#1082
Co-authored-by: Athan Reines <[email protected]>
Reviewed-by: Athan Reines <[email protected]> 
Ref: #1062
  • Loading branch information
steff456 and kgryte authored Oct 31, 2023
1 parent a6b94fe commit de17736
Show file tree
Hide file tree
Showing 41 changed files with 2,143 additions and 35 deletions.
92 changes: 92 additions & 0 deletions lib/node_modules/@stdlib/string/base/reverse-code-points/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<!--
@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.
-->

# reverseCodePoints

> Reverse the Unicode code points of a string.
<section class="usage">

## Usage

```javascript
var reverseCodePoints = require( '@stdlib/string/base/reverse-code-points' );
```

#### reverseCodePoints( str )

Reverses the Unicode code points of a string.

```javascript
var out = reverseCodePoints( 'last man standing' );
// returns 'gnidnats nam tsal'

out = reverseCodePoints( 'Hidden Treasures' );
// returns 'serusaerT neddiH'

out = reverseCodePoints( 'foo bar' );
// returns 'rab oof'
```

</section>

<!-- /.usage -->

<section class="examples">

## Examples

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

```javascript
var reverseCodePoints = require( '@stdlib/string/base/reverse-code-points' );

var str = reverseCodePoints( 'presidential election' );
// returns 'noitcele laitnediserp'

str = reverseCodePoints( 'JavaScript' );
// returns 'tpircSavaJ'

str = reverseCodePoints( 'The Last of the Mohicans' );
// returns 'snacihoM eht fo tsaL ehT'

str = reverseCodePoints( 'अनुच्छेद' );
// 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 reverse = 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 = reverse( 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,26 @@

{{alias}}( str )
Reverses the Unicode code points of a string.

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

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

Examples
--------
> var out = {{alias}}( 'beep' )
'peeb'
> out = {{alias}}( 'Boop' )
'pooB'
> out = {{alias}}( 'foo bar' )
'rab oof'

See Also
--------

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

/**
* Reverses the Unicode code points of a string.
*
* @param str - input string
* @returns output string
*
* @example
* var out = reverse( 'last man standing' );
* // returns 'gnidnats nam tsal'
*
* @example
* var out = reverse( 'presidential election' );
* // returns 'noitcele laitnediserp'
*
* @example
* var out = reverse( 'JavaScript' );
* // returns 'tpircSavaJ'
*
* @example
* var out = reverse( 'Hidden Treasures' );
* // returns 'serusaerT neddiH'
*
* @example
* var out = reverse( 'foo bar' );
* // returns 'rab oof'
*/
declare function reverse( str: string ): string;


// EXPORTS //

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


// TESTS //

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

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

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

console.log( reverse( 'presidential election' ) );
// => 'noitcele laitnediserp'

console.log( reverse( 'JavaScript' ) );
// => 'tpircSavaJ'

console.log( reverse( 'The Last of the Mohicans' ) );
// => 'snacihoM eht fo tsaL ehT'

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

/**
* Reverse the Unicode code points of a string.
*
* @module @stdlib/string/base/reverse-code-points
*
* @example
* var reverse = require( '@stdlib/string/base/reverse-code-points' );
*
* var out = reverse( 'last man standing' );
* // returns 'gnidnats nam tsal'
*
* out = reverse( 'Hidden Treasures' );
* // returns 'serusaerT neddiH';
*/

// MODULES //

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


// EXPORTS //

module.exports = main;
Loading

1 comment on commit de17736

@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/reverse-code-points $\color{green}135/135$
$\color{green}+100.00\%$
$\color{green}10/10$
$\color{green}+100.00\%$
$\color{green}1/1$
$\color{green}+100.00\%$
$\color{green}135/135$
$\color{green}+100.00\%$
string/base/reverse-grapheme-clusters $\color{green}127/127$
$\color{green}+0.00\%$
$\color{green}6/6$
$\color{green}+0.00\%$
$\color{green}1/1$
$\color{green}+0.00\%$
$\color{green}127/127$
$\color{green}+0.00\%$
string/base/reverse $\color{green}102/102$
$\color{green}+100.00\%$
$\color{green}4/4$
$\color{green}+100.00\%$
$\color{green}1/1$
$\color{green}+100.00\%$
$\color{green}102/102$
$\color{green}+100.00\%$
string/reverse $\color{green}148/148$
$\color{green}+0.00\%$
$\color{green}12/12$
$\color{green}+0.00\%$
$\color{green}1/1$
$\color{green}+0.00\%$
$\color{green}148/148$
$\color{green}+0.00\%$

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

Please sign in to comment.