diff --git a/src/CSVelte/Collection/AbstractCollection.php b/src/CSVelte/Collection/AbstractCollection.php index 22a6938..ae4d914 100644 --- a/src/CSVelte/Collection/AbstractCollection.php +++ b/src/CSVelte/Collection/AbstractCollection.php @@ -543,8 +543,8 @@ public function filter(callable $callback, $flag = ARRAY_FILTER_USE_BOTH) public function first(callable $callback) { foreach ($this->data as $index => $value) { - if ($ret = $callback($value, $index)) { - return $ret; + if ($callback($value, $index)) { + return $value; } } return null; diff --git a/tests/CSVelte/Collection/CharCollectionTest.php b/tests/CSVelte/Collection/CharCollectionTest.php index 882c0a0..da40faf 100644 --- a/tests/CSVelte/Collection/CharCollectionTest.php +++ b/tests/CSVelte/Collection/CharCollectionTest.php @@ -183,4 +183,71 @@ public function testMapUsesCallbackOnEachChar() }); $this->assertEquals('-------------nopqrstuvwxyz', (string) $newstr); } + + public function testWalkUsesCallbackOnEachChar() + { + $str = new CharCollection($exp = 'abcdefghijklmnopqrstuvwxyz'); + $exp = []; + $str->walk(function($val, $key, $extra) use (&$exp) { + if ($key % 2 == 0) { + $exp[] = $val . $extra[0]; + } else { + $exp[] = $val . $extra[1]; + } + }, ['foo','bar']); + $this->assertEquals("afoo", $exp[0]); + $this->assertEquals("bbar", $exp[1]); + } + + public function testReduceUsesCallbackToReturnSingleValue() + { + $str = new CharCollection($exp = 'abcdefghijklmnopqrstuvwxyz'); + $isstr = $str->reduce(function($carry, $elem){ + return (is_string($elem) && $carry); + }, true); + $this->assertTrue($isstr); + $isnum= $str->reduce(function($carry, $elem){ + return (is_numeric($elem) && $carry); + }, true); + $this->assertFalse($isnum); + } + + public function testFilterRemovesCorrectChars() + { + $str = new CharCollection($exp = 'abcdefghijklmnopqrstuvwxyz'); + $newstr = $str->filter(function($val) { + return ($val > 'm'); + }); + $this->assertEquals('nopqrstuvwxyz', (string) $newstr); + } + + public function testFirstReturnsFirstMatchingValue() + { + $str = new CharCollection($exp = 'I like char collections.'); + $char = $str->first(function($val) { + return ($val > 'm'); + }); + $this->assertEquals('r', $char); + } + + public function testLastReturnsLastMatchingValue() + { + $str = new CharCollection($exp = 'I like char collections.'); + $char = $str->last(function($val) { + return ($val > 'm'); + }); + $this->assertEquals('s', $char); + } + + public function testReverse() + { + $str = new CharCollection($exp = 'I like char collections.'); + $this->assertEquals(strrev($exp), (string) $str->reverse()); + } + + public function testUnique() + { + $str = new CharCollection($exp = 'I like char collections.'); + $this->assertEquals('I likecharotns.', (string) $str->unique()); + } } \ No newline at end of file