Skip to content

Commit

Permalink
Merge pull request #51 from aoi-tech/fix_distinct_apis
Browse files Browse the repository at this point in the history
Fixed distinct functions
  • Loading branch information
tumugin authored Apr 5, 2022
2 parents 75d7b19 + d39b679 commit 262f6bc
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/SnList/SnFloatList.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,21 @@ public function toFloatArray(): array
->map(fn(SnFloat $v) => $v->toFloat())
->toArray();
}

/**
* Returns the unique list
*
* @return static
*/
public function distinct()
{
$uniqueRawValues = array_values(
array_unique(
$this->map(
fn(SnFloat $value) => $value->toFloat()
)->toArray()
)
);
return static::byFloatArray($uniqueRawValues);
}
}
17 changes: 17 additions & 0 deletions src/SnList/SnIntegerList.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,21 @@ public function toIntArray(): array
->map(fn(SnInteger $v) => $v->toInt())
->toArray();
}

/**
* Returns the unique list
*
* @return static
*/
public function distinct()
{
$uniqueRawValues = array_values(
array_unique(
$this->map(
fn(SnInteger $value) => $value->toInt()
)->toArray()
)
);
return static::byIntArray($uniqueRawValues);
}
}
19 changes: 19 additions & 0 deletions src/SnList/SnStringList.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Assert\Assertion;
use Assert\AssertionFailedException;
use Tumugin\Stannum\SnBaseValue;
use Tumugin\Stannum\SnString;

/**
Expand Down Expand Up @@ -91,4 +92,22 @@ public function toStringArray(): array
->map(fn(SnString $s) => $s->toString())
->toArray();
}

/**
* Returns the unique list
*
* @return static
* @throws AssertionFailedException
*/
public function distinct()
{
$uniqueRawValues = array_values(
array_unique(
$this->map(
fn(SnString $value) => $value->toString()
)->toArray()
)
);
return static::byStringArray($uniqueRawValues);
}
}
65 changes: 65 additions & 0 deletions test/IntegrationTest/ListFunctionsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

declare(strict_types=1);

namespace Tumugin\Stannum\Test\IntegrationTest;

use PHPUnit\Framework\TestCase;
use Tumugin\Stannum\SnFloat;
use Tumugin\Stannum\SnInteger;
use Tumugin\Stannum\SnString;

class ListFunctionsTest extends TestCase
{
public function testWithStringListFunctions(): void
{
$result = SnString::byString(
'read_list,read_list,write_list,'
)->split(SnString::byString(','))
->filter(fn(SnString $v) => !$v->isEmpty())
->map(fn(SnString $v) => $v->capitalizeAll())
->toSnStringList()
->distinct()
->joinToString(SnString::byString("\n"))
->toString();

$this->assertSame(
"READ_LIST\nWRITE_LIST",
$result
);
}

public function testWithIntegerListFunctions(): void
{
$result = SnString::byString(
'1,2,2,2,3,'
)->split(SnString::byString(','))
->filter(fn(SnString $v) => !$v->isEmpty())
->map(fn(SnString $v) => SnInteger::byString($v->toString()))
->toSnIntegerList()
->distinct()
->toIntArray();

$this->assertSame(
[1, 2, 3],
$result
);
}

public function testWithFloatListFunctions(): void
{
$result = SnString::byString(
'1.1,2.1,2.1,2.1,3.1,'
)->split(SnString::byString(','))
->filter(fn(SnString $v) => !$v->isEmpty())
->map(fn(SnString $v) => SnFloat::byString($v->toString()))
->toSnFloatList()
->distinct()
->toFloatArray();

$this->assertSame(
[1.1, 2.1, 3.1],
$result
);
}
}

0 comments on commit 262f6bc

Please sign in to comment.