Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
head/first aligned with lodash/underscore
  • Loading branch information
sfinktah committed Sep 10, 2023
commit 32773f3260729a0dba77fcd0424128ff6a914d8b
40 changes: 34 additions & 6 deletions src/Array/head.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* @alias first
* @category Array
*
* @param array $array The array to query.
* @param mixed $array The array to query.
*
* @return mixed Returns the first element of `array`.
*
Expand All @@ -30,15 +30,43 @@
* // => null
* </code>
*/
function head(array $array)
function head(mixed $array)
{
reset($array);

return current($array) ?: null;
if ((is_array($array) || $array instanceof \ArrayObject) && count($array)) {
return reset($array);
}
else if (is_string($array) && strlen($array)) {
return substr($array, 0, 1);
}
return null;
}

/* alias to head() */
function first(array $array)
function first(mixed $array)
{
return head($array);
}

$COMMENT = <<<JSON
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can move to the docblock of the head function

# lodash (4.17.15) (JavaScript)
```js
function head(array) {
return (array && array.length) ? array[0] : undefined;
}
```
> a = [ [], [1], [1, 2], "123", "1", null, false, "" ];
> b = _.map(a, function(v) { return _.first(v); });
> JSON.stringify({a:a, b:b});
{"a":[[],[1],[1,2],"123","1",null,false,""],"b":[null,1,1,"1","1",null,null,null]}


# Underscore.js (1.13.6) (JavaScript)
{"a":[[],[1],[1,2],"123","1",null,false,""],"b":[null,1,1,"1","1",null,null,null]}
```js
export default function first(array, n, guard) {
if (array == null || array.length < 1) return n == null || guard ? void 0 : [];
if (n == null || guard) return array[0];
return initial(array, array.length - n);
}
```
JSON;
21 changes: 21 additions & 0 deletions tests/Array/HeadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,26 @@ public function testHead()
$this->assertSame(1, head([1, 2, 3]));
$this->assertSame(null, head([]));
$this->assertSame(1, first([1, 2, 3])); // Test the alias


# Generated from lodash in JavaScript via:
# $a = [ [], [1], [1, 2], "123", "1", null, false, "", {a: 'bcd'} ];
# $b = _.map($a, $v => _.first($v));
# _.each(_.zip($a, $b), function(v) {
# a = JSON.stringify([v[0]]);
# b = JSON.stringify([v[1]]);
# a = a.substring(1, a.length - 1);
# b = b.substring(1, b.length - 1);
# console.log(` $this->assertSame(${b}, ${a});`)
# });
$this->assertSame(null, []);
$this->assertSame(1, [1]);
$this->assertSame(1, [1,2]);
$this->assertSame("1", "123");
$this->assertSame("1", "1");
$this->assertSame(null, null);
$this->assertSame(null, false);
$this->assertSame(null, "");
$this->assertSame(null, ["a" => "bcd"]);
}
}