Skip to content

Commit 318fa88

Browse files
committed
feat: do not return same type as input iterable anymore in iterable_map() and iterable_filter()
1 parent 60d35f0 commit 318fa88

File tree

3 files changed

+9
-15
lines changed

3 files changed

+9
-15
lines changed

src/iterable-functions.php

+3-10
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use Traversable;
1010

1111
use function array_values;
12-
use function is_array;
1312
use function iterator_to_array;
1413

1514
/**
@@ -26,9 +25,7 @@
2625
*/
2726
function iterable_map(iterable $iterable, callable $mapper): iterable
2827
{
29-
$mapped = iterable($iterable)->map($mapper);
30-
31-
return is_array($iterable) ? $mapped->asArray() : $mapped;
28+
return iterable($iterable)->map($mapper);
3229
}
3330

3431
/**
@@ -86,9 +83,7 @@ function iterable_to_traversable(iterable $iterable): Traversable
8683
*/
8784
function iterable_filter(iterable $iterable, ?callable $filter = null): iterable
8885
{
89-
$filtered = iterable($iterable)->filter($filter);
90-
91-
return is_array($iterable) ? $filtered->asArray() : $filtered;
86+
return iterable($iterable)->filter($filter);
9287
}
9388

9489
/**
@@ -125,9 +120,7 @@ function iterable_reduce(iterable $iterable, callable $reduce, $initial = null)
125120
*/
126121
function iterable_values(iterable $iterable): iterable
127122
{
128-
$withoutKeys = iterable($iterable)->values();
129-
130-
return is_array($iterable) ? $withoutKeys->asArray() : $withoutKeys;
123+
return iterable($iterable)->values();
131124
}
132125

133126
/**

tests/IterableFilterTest.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@
99

1010
use function assert;
1111
use function BenTools\IterableFunctions\iterable_filter;
12+
use function BenTools\IterableFunctions\iterable_to_array;
1213
use function it;
1314
use function iterator_to_array;
1415
use function PHPUnit\Framework\assertSame;
1516

1617
it('filters an array', function (): void {
1718
$iterable = [false, true];
18-
assertSame([1 => true], iterable_filter($iterable));
19+
assertSame([1 => true], iterable_to_array(iterable_filter($iterable)));
1920
});
2021

2122
it('filters a Traversable object', function (): void {
@@ -32,7 +33,7 @@
3233
static function ($input): bool {
3334
return $input === 'bar';
3435
};
35-
assertSame([1 => 'bar'], iterable_filter($iterable, $filter));
36+
assertSame([1 => 'bar'], iterable_to_array(iterable_filter($iterable, $filter)));
3637
});
3738

3839
it('filters a Travsersable object with a callback', function (): void {

tests/IterableMapTest.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,23 @@
1212

1313
use function assert;
1414
use function BenTools\IterableFunctions\iterable_map;
15+
use function BenTools\IterableFunctions\iterable_to_array;
1516
use function it;
16-
use function iterator_to_array;
1717
use function PHPUnit\Framework\assertInstanceOf;
1818
use function PHPUnit\Framework\assertSame;
1919

2020
it('maps an array', function (): void {
2121
$iterable = ['foo', 'bar'];
2222
$map = 'strtoupper';
23-
assertSame(['FOO', 'BAR'], iterable_map($iterable, $map));
23+
assertSame(['FOO', 'BAR'], iterable_to_array(iterable_map($iterable, $map)));
2424
});
2525

2626
it('maps a Traversable object', function (): void {
2727
$iterable = SplFixedArray::fromArray(['foo', 'bar']);
2828
$map = 'strtoupper';
2929
$mapped = iterable_map($iterable, $map);
3030
assert($mapped instanceof Traversable);
31-
assertSame(['FOO', 'BAR'], iterator_to_array($mapped));
31+
assertSame(['FOO', 'BAR'], iterable_to_array($mapped));
3232
});
3333

3434
it('maps iterable with object keys', function (): void {

0 commit comments

Comments
 (0)