public function testGroupBy() { // from js $parity = __u::groupBy(array(1, 2, 3, 4, 5, 6), function ($num) { return $num % 2; }); $this->assertEquals(array(array(2, 4, 6), array(1, 3, 5)), $parity, 'created a group for each value'); // extra $parity = __u(array(1, 2, 3, 4, 5, 6))->groupBy(function ($num) { return $num % 2; }); $this->assertEquals(array(array(2, 4, 6), array(1, 3, 5)), $parity, 'created a group for each value using OO-style call'); $vals = array(array('name' => 'rejected', 'yesno' => 'no'), array('name' => 'accepted', 'yesno' => 'yes'), array('name' => 'allowed', 'yesno' => 'yes'), array('name' => 'denied', 'yesno' => 'no')); $grouped = __u::groupBy($vals, 'yesno'); $this->assertEquals('rejected denied', join(' ', __u::pluck($grouped['no'], 'name')), 'pulls no entries'); $this->assertEquals('accepted allowed', join(' ', __u::pluck($grouped['yes'], 'name')), 'pulls yes entries'); // docs $result = __u::groupBy(array(1, 2, 3, 4, 5), function ($n) { return $n % 2; }); $this->assertEquals(array(0 => array(2, 4), 1 => array(1, 3, 5)), $result); $values = array(array('name' => 'Apple', 'grp' => 'a'), array('name' => 'Bacon', 'grp' => 'b'), array('name' => 'Avocado', 'grp' => 'a')); $expected = array('a' => array(array('name' => 'Apple', 'grp' => 'a'), array('name' => 'Avocado', 'grp' => 'a')), 'b' => array(array('name' => 'Bacon', 'grp' => 'b'))); $this->assertEquals($expected, __u::groupBy($values, 'grp')); }