public function testUniqueId() { // docs $this->assertEquals(0, __u::uniqueId()); $this->assertEquals('stooge_1', __u::uniqueId('stooge_')); $this->assertEquals(2, __u::uniqueId()); // from js $ids = array(); $i = 0; while ($i++ < 100) { array_push($ids, __u::uniqueId()); } $this->assertEquals(count($ids), count(__u::uniq($ids))); // extra $this->assertEquals('stooges', join('', __u::first(__u::uniqueId('stooges'), 7)), 'prefix assignment works'); $this->assertEquals('stooges', join('', __u(__u('stooges')->uniqueId())->first(7)), 'prefix assignment works in OO-style call'); while ($i++ < 100) { array_push($ids, __u()->uniqueId()); } $this->assertEquals(count($ids), count(__u()->uniq($ids))); }
public function testUniq() { // from js $list = array(1, 2, 1, 3, 1, 9); $this->assertEquals(array(1, 2, 3, 9), __u::uniq($list), 'can find the unique values of an unsorted array'); $list = array(1, 1, 1, 2, 2, 3); $this->assertEquals(array(1, 2, 3), __u::uniq($list), 'can find the unique values of a sorted array faster'); $func = function () { return __u::uniq(func_get_args()); }; $result = $func(1, 2, 1, 3, 1, 4); $this->assertEquals(array(1, 2, 3, 4), $result, 'works on an arguments object'); $list = array((object) array('name' => 'moe'), (object) array('name' => 'curly'), (object) array('name' => 'larry'), (object) array('name' => 'curly')); $iterator = function ($value) { return $value->name; }; $this->assertEquals(array('moe', 'curly', 'larry'), __u::map(__u::uniq($list, false, $iterator), $iterator), 'can find the unique values of an array using a custom iterator'); $iterator = function ($value) { return $value + 1; }; $list = array(1, 2, 2, 3, 4, 4); $this->assertEquals(array(1, 2, 3, 4), __u::uniq($list, true, $iterator), 'iterator works with sorted array'); // extra $this->assertEquals(array(4, 5, 6), __u(array(4, 5, 4, 4, 5, 5, 6))->uniq(), 'works with OO call'); $this->assertEquals(array(4, 5, 6), __u(array(4, 5, 4, 4, 5, 5, 6))->unique(), 'aliased as "unique"'); // docs $this->assertEquals(array(2, 4, 1), __u::uniq(array(2, 2, 4, 4, 4, 1, 1, 1))); }