Skip to content

Commit

Permalink
fix: use nextGraphemeCluster in implementation, fix tests and docstrings
Browse files Browse the repository at this point in the history
  • Loading branch information
steff456 committed Sep 26, 2023
1 parent a02dc34 commit 6a05b25
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

// MODULES //

var prevGraphemeClusterBreak = require( '@stdlib/string/prev-grapheme-cluster-break' );
var nextGraphemeClusterBreak = require( '@stdlib/string/next-grapheme-cluster-break' );


// MAIN //
Expand Down Expand Up @@ -52,18 +52,24 @@ var prevGraphemeClusterBreak = require( '@stdlib/string/prev-grapheme-cluster-br
* // returns '🐸🐰🐷🐮🐶'
*/
function reverse( str ) {
var cluster;
var out;
var idx;
var brk;
var i;

out = '';
idx = str.length - 1;
while ( idx >= 0 ) {
brk = prevGraphemeClusterBreak( str, idx );
for ( i = brk + 1; i <= idx; i++ ) {
out += str.charAt( i );
idx = 0;
while ( idx < str.length ) {
brk = nextGraphemeClusterBreak( str, idx );
if ( brk === -1 ) {
brk = str.length;
}
cluster = '';
for ( i = idx; i < brk; i++ ) {
cluster += str.charAt( i );
}
out = cluster + out;
idx = brk;
}
return out;
Expand Down
10 changes: 10 additions & 0 deletions lib/node_modules/@stdlib/string/reverse/docs/types/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@ import reverseString = require( './index' );
reverseString( ( x: number ): number => x, {} ); // $ExpectError
}

// The compiler throws an error if the function is provided a value other than an object as second argument...
{
reverseString( 'abc', true ); // $ExpectError
reverseString( 'abc', false ); // $ExpectError
reverseString( 'abc', null ); // $ExpectError
reverseString( 'abc', '' ); // $ExpectError
reverseString( 'abc', [] ); // $ExpectError
reverseString( 'abc', ( x: number ): number => x ); // $ExpectError
}

// The compiler throws an error if the function is provided an invalid `mode` option...
{
reverseString( 'abc', { 'mode': true } ); // $ExpectError
Expand Down
1 change: 1 addition & 0 deletions lib/node_modules/@stdlib/string/reverse/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ var isMode = contains( MODES );
*
* @param {string} str - input string
* @param {Options} [options] - options
* @param {string} [options.mode="grapheme"] - type of "character" to return (must be either `grapheme`, `code_point`, or `code_unit`)
* @throws {TypeError} must provide a string primitive
* @throws {TypeError} options argument must be an object
* @throws {TypeError} must provide valid options
Expand Down
4 changes: 2 additions & 2 deletions lib/node_modules/@stdlib/string/reverse/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,8 @@ tape( 'the function reverses the first string (mode=grapheme)', function test( t
out = reverseString( '🌷', opts );
t.strictEqual( out, '🌷', 'returns expected value' );

out = reverseString( '👉🏿', opts );
t.strictEqual( out, '🏿👉', 'returns expected value' );
out = reverseString( '👉🏿🌷', opts );
t.strictEqual( out, '🌷👉🏿', 'returns expected value' );

t.end();
});
Expand Down

0 comments on commit 6a05b25

Please sign in to comment.