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

FEATURE add includes Eel Helpers for String and Array #3359

Open
wants to merge 1 commit into
base: 8.4
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
21 changes: 21 additions & 0 deletions Neos.Eel/Classes/Helper/ArrayHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,27 @@
*/
class ArrayHelper implements ProtectedContextAwareInterface
{

/**
* determines whether an array includes a certain value among its entries,
* returning true or false as appropriate.
*
* If fromIndex is non-negative, the sequence will start at that offset in the array.
* If fromIndex is negative, the sequence will start that far from the end of the array.
*
* @param iterable $array
* @param mixed $searchElement The value to search for.
* @param int $fromIndex Zero-based index at which to start searching
* @return bool
*/
public function includes(iterable $array, mixed $searchElement, int $fromIndex = 0): bool
{
if ($array instanceof \Traversable) {
$array = iterator_to_array($array);
}
return in_array($searchElement, (array)array_slice($array, $fromIndex));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should probably use the 3 param (strict) to true or is the proposed way closer to ECMASCRIPT?

... also the (array) casts looks a little weird and im wondering in general if we should optimize and early return for the fromIndex=0 case?

}

/**
* Concatenate arrays or values to a new array
*
Expand Down
24 changes: 24 additions & 0 deletions Neos.Eel/Classes/Helper/StringHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,30 @@ public function substring($string, $start, $end = null)
return mb_substr($string, $start, $length, 'UTF-8');
}

/**
* performs a case-sensitive search to determine whether a given
* string may be found within this string, returning true or false as appropriate.
*
* This implementation follows the JavaScript specification for "includes".
*
* Examples::
*
* String.includes('Hello, World!', '') == true
* String.includes('Hello, World!', 'Hello') == true
* String.includes('Hello, World!', 'hello') == false
* String.includes('Hello, World!', 'Hello', 5) == false
* String.includes('Hello, World!', 'Eel') == false
*
* @param string $string
* @param string $searchString
* @param int $position
* @return bool true if the search string is found anywhere within the given string, including when searchString is an empty string; otherwise, false.
*/
public function includes(string $string, string $searchString, int $position = 0): bool
{
return str_contains(mb_substr($string, $position), $searchString);
}

/**
* Get the character at a specific position
*
Expand Down
21 changes: 21 additions & 0 deletions Neos.Eel/Tests/Unit/Helper/ArrayHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,27 @@
*/
class ArrayHelperTest extends \Neos\Flow\Tests\UnitTestCase
{
public function includesExamples()
{
return [
'included' => [['a', 'b', 'c'], 'a', 0, true],
'not included' => [['a', 'b', 'c'], 'd', 0, false],
'cut containing start part' => [['a', 'b', 'c'], 'a', 1, false],
'cut containing end part' => [['a', 'b', 'c'], 'a', -2, false],
];
}

/**
* @test
* @dataProvider includesExamples
*/
public function includesWorks($string, $searchString, $fromIndex, $expected)
{
$helper = new ArrayHelper();
$result = $helper->includes($string, $searchString, $fromIndex);
self::assertSame($expected, $result);
}

public function concatExamples()
{
return [
Expand Down
23 changes: 23 additions & 0 deletions Neos.Eel/Tests/Unit/Helper/StringHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,29 @@ public function substrWorks($string, $start, $length, $expected)
self::assertSame($expected, $result);
}


public function includesExamples()
{
return [
'empty string' => ['Hello, World!', '', 0, true],
'included at start' => ['Hello, World!', 'Hello', 0, true],
'case sensitive' => ['Hello, World!', 'hello', 0, false],
'cut containing part' => ['Hello, World!', 'Hello', 5, false],
'not included' => ['Hello, World!', 'Eel', 0, false],
];
}

/**
* @test
* @dataProvider includesExamples
*/
public function includesWorks($string, $searchString, $position, $expected)
{
$helper = new StringHelper();
$result = $helper->includes($string, $searchString, $position);
self::assertSame($expected, $result);
}

public function substringExamples()
{
return [
Expand Down
Loading