Skip to content

Commit

Permalink
feat(str): add a new reverse() function (#238)
Browse files Browse the repository at this point in the history
  • Loading branch information
yivi authored Oct 18, 2021
1 parent cba2124 commit 2f0dad8
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/component/str.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
- [replace_ci](./../../src/Psl/Str/replace_ci.php#L20)
- [replace_every](./../../src/Psl/Str/replace_every.php#L19)
- [replace_every_ci](./../../src/Psl/Str/replace_every_ci.php#L19)
- [reverse](./../../src/Psl/Str/reverse.php#L14)
- [search](./../../src/Psl/Str/search.php#L25)
- [search_ci](./../../src/Psl/Str/search_ci.php#L25)
- [search_last](./../../src/Psl/Str/search_last.php#L25)
Expand Down
1 change: 1 addition & 0 deletions src/Psl/Internal/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ final class Loader
'Psl\Str\replace_ci',
'Psl\Str\replace_every',
'Psl\Str\replace_every_ci',
'Psl\Str\reverse',
'Psl\Str\search',
'Psl\Str\search_ci',
'Psl\Str\search_last',
Expand Down
20 changes: 20 additions & 0 deletions src/Psl/Str/reverse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Psl\Str;

use Psl\Vec;

/**
* Returns the given string reversed.
*
* @psalm-pure
*/
function reverse(string $string, ?string $encoding = null): string
{
$chunks = chunk($string, encoding: $encoding);

/** @psalm-suppress ImpureFunctionCall */
return join(Vec\reverse($chunks), '');
}
33 changes: 33 additions & 0 deletions tests/unit/Str/ReverseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Psl\Tests\Unit\Str;

use PHPUnit\Framework\TestCase;
use Psl\Str;

final class ReverseTest extends TestCase
{

public function provideData(): array
{
return [
['Hello World', 'dlroW olleH'],
['héllö wôrld', 'dlrôw ölléh'],
['Iñigo Montoya', 'ayotnoM ogiñI'],
['某物', '物某' ],
['что-то', 'от-отч'],
['🙂😟', '😟🙂'],
['مرحبا', 'ابحرم'],
];
}

/**
* @dataProvider provideData
*/
public function testReverse(string $string, string $expected): void
{
static::assertSame(Str\reverse($string), $expected);
}
}

0 comments on commit 2f0dad8

Please sign in to comment.