Skip to content

Commit de17736

Browse files
steff456kgryte
andauthored
feat: refactor string/reverse and add mode option
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
1 parent a6b94fe commit de17736

File tree

41 files changed

+2143
-35
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+2143
-35
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2023 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# reverseCodePoints
22+
23+
> Reverse the Unicode code points of a string.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var reverseCodePoints = require( '@stdlib/string/base/reverse-code-points' );
31+
```
32+
33+
#### reverseCodePoints( str )
34+
35+
Reverses the Unicode code points of a string.
36+
37+
```javascript
38+
var out = reverseCodePoints( 'last man standing' );
39+
// returns 'gnidnats nam tsal'
40+
41+
out = reverseCodePoints( 'Hidden Treasures' );
42+
// returns 'serusaerT neddiH'
43+
44+
out = reverseCodePoints( 'foo bar' );
45+
// returns 'rab oof'
46+
```
47+
48+
</section>
49+
50+
<!-- /.usage -->
51+
52+
<section class="examples">
53+
54+
## Examples
55+
56+
<!-- eslint no-undef: "error" -->
57+
58+
```javascript
59+
var reverseCodePoints = require( '@stdlib/string/base/reverse-code-points' );
60+
61+
var str = reverseCodePoints( 'presidential election' );
62+
// returns 'noitcele laitnediserp'
63+
64+
str = reverseCodePoints( 'JavaScript' );
65+
// returns 'tpircSavaJ'
66+
67+
str = reverseCodePoints( 'The Last of the Mohicans' );
68+
// returns 'snacihoM eht fo tsaL ehT'
69+
70+
str = reverseCodePoints( 'अनुच्छेद' );
71+
// returns 'देछ्चुनअ'
72+
```
73+
74+
</section>
75+
76+
<!-- /.examples -->
77+
78+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
79+
80+
<section class="related">
81+
82+
</section>
83+
84+
<!-- /.related -->
85+
86+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
87+
88+
<section class="links">
89+
90+
</section>
91+
92+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var isString = require( '@stdlib/assert/is-string' ).isPrimitive;
25+
var pkg = require( './../package.json' ).name;
26+
var reverse = require( './../lib' );
27+
28+
29+
// MAIN //
30+
31+
bench( pkg, function benchmark( b ) {
32+
var values;
33+
var out;
34+
var i;
35+
36+
values = [
37+
'beep boop',
38+
'foo bar',
39+
'xyz abc',
40+
'🐶🐮🐷🐰🐸'
41+
];
42+
43+
b.tic();
44+
for ( i = 0; i < b.iterations; i++ ) {
45+
out = reverse( values[ i%values.length ], 1 );
46+
if ( typeof out !== 'string' ) {
47+
b.fail( 'should return a string' );
48+
}
49+
}
50+
b.toc();
51+
if ( !isString( out ) ) {
52+
b.fail( 'should return a string' );
53+
}
54+
b.pass( 'benchmark finished' );
55+
b.end();
56+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
{{alias}}( str )
3+
Reverses the Unicode code points of a string.
4+
5+
Parameters
6+
----------
7+
str: string
8+
Input string.
9+
10+
Returns
11+
-------
12+
out: string
13+
Output string.
14+
15+
Examples
16+
--------
17+
> var out = {{alias}}( 'beep' )
18+
'peeb'
19+
> out = {{alias}}( 'Boop' )
20+
'pooB'
21+
> out = {{alias}}( 'foo bar' )
22+
'rab oof'
23+
24+
See Also
25+
--------
26+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
// TypeScript Version: 2.0
20+
21+
/**
22+
* Reverses the Unicode code points of a string.
23+
*
24+
* @param str - input string
25+
* @returns output string
26+
*
27+
* @example
28+
* var out = reverse( 'last man standing' );
29+
* // returns 'gnidnats nam tsal'
30+
*
31+
* @example
32+
* var out = reverse( 'presidential election' );
33+
* // returns 'noitcele laitnediserp'
34+
*
35+
* @example
36+
* var out = reverse( 'JavaScript' );
37+
* // returns 'tpircSavaJ'
38+
*
39+
* @example
40+
* var out = reverse( 'Hidden Treasures' );
41+
* // returns 'serusaerT neddiH'
42+
*
43+
* @example
44+
* var out = reverse( 'foo bar' );
45+
* // returns 'rab oof'
46+
*/
47+
declare function reverse( str: string ): string;
48+
49+
50+
// EXPORTS //
51+
52+
export = reverse;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
import reverse = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a string...
25+
{
26+
reverse( 'abc' ); // $ExpectType string
27+
}
28+
29+
// The compiler throws an error if the function is provided a value other than a string...
30+
{
31+
reverse( true ); // $ExpectError
32+
reverse( false ); // $ExpectError
33+
reverse( null ); // $ExpectError
34+
reverse( undefined ); // $ExpectError
35+
reverse( 5 ); // $ExpectError
36+
reverse( [] ); // $ExpectError
37+
reverse( {} ); // $ExpectError
38+
reverse( ( x: number ): number => x ); // $ExpectError
39+
}
40+
41+
// The compiler throws an error if the function is provided an unsupported number of arguments...
42+
{
43+
reverse(); // $ExpectError
44+
reverse( 'abc', 1 ); // $ExpectError
45+
reverse( 'abc', 1, 2 ); // $ExpectError
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
var reverse = require( './../lib' );
22+
23+
console.log( reverse( 'presidential election' ) );
24+
// => 'noitcele laitnediserp'
25+
26+
console.log( reverse( 'JavaScript' ) );
27+
// => 'tpircSavaJ'
28+
29+
console.log( reverse( 'The Last of the Mohicans' ) );
30+
// => 'snacihoM eht fo tsaL ehT'
31+
32+
console.log( reverse( 'अनुच्छेद' ) );
33+
// => 'देछ्चुनअ'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
/**
22+
* Reverse the Unicode code points of a string.
23+
*
24+
* @module @stdlib/string/base/reverse-code-points
25+
*
26+
* @example
27+
* var reverse = require( '@stdlib/string/base/reverse-code-points' );
28+
*
29+
* var out = reverse( 'last man standing' );
30+
* // returns 'gnidnats nam tsal'
31+
*
32+
* out = reverse( 'Hidden Treasures' );
33+
* // returns 'serusaerT neddiH';
34+
*/
35+
36+
// MODULES //
37+
38+
var main = require( './main.js' );
39+
40+
41+
// EXPORTS //
42+
43+
module.exports = main;

0 commit comments

Comments
 (0)