Skip to content
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

feat: refactor string package reverse #1082

Merged
merged 9 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from 8 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
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 'देछ्चुनअ'
kgryte marked this conversation as resolved.
Show resolved Hide resolved
```

</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( 'अनुच्छेद' ) );
// => 'देछ्चुनअ'
kgryte marked this conversation as resolved.
Show resolved Hide resolved
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
Loading