-
Notifications
You must be signed in to change notification settings - Fork 54
head/first aligned with lodash/underscore #31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the SolidWorx Lodash-PHP project. | ||
* | ||
* @author Pierre du Plessis <open-source@solidworx.co> | ||
* @copyright Copyright (c) 2017 | ||
*/ | ||
|
||
namespace _; | ||
|
||
/** | ||
* Gets the first element of `array` or `default` if the array is empty | ||
* | ||
* @alias firstOr | ||
* @param mixed $array The array to query. | ||
* @param mixed|callable $default Value or Callable if array is empty * | ||
* | ||
* @return mixed Returns the first element of `array` or `default` if empty. | ||
* | ||
* @category Array | ||
* | ||
* @example | ||
* <code> | ||
* head([1, 2, 3]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The code example should reference |
||
* // => 1 | ||
* | ||
* head([]) | ||
* // => null | ||
* </code> | ||
*/ | ||
function headOr(mixed $array, mixed $default) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
{ | ||
if ((is_array($array) || $array instanceof \ArrayObject) && count($array)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think most of this logic can go to the function headOr($array, $default)
{
return head($array) ?? (is_callable($default) ? $default($array) : $default);
} |
||
return reset($array); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Calling There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Your many comments are appreciated and inarguably valid. I will not waste time responding to the others, but this one is quite interesting. Personally, I have always disliked using I would much rather write function head($array) {
foreach ($array as $value) {
return $value;
}
return null;
}
function headOr($array, $default) {
head($array) ?? (is_callable($default) ? $default($array) : $default);
} As you suggested in another comment, That does upset my OCD a little though: $array = [null, 1, 2];
printf("First: %s\n", head($array, "Empty Array")); Would return "Empty Array", would it not? |
||
} | ||
else if (is_string($array) && strlen($array)) { | ||
return substr($array, 0, 1); | ||
} | ||
return is_callable($default) ? $default($array) : $default; | ||
} | ||
|
||
/* alias to head() */ | ||
function firstOr(mixed $array, mixed $default) | ||
{ | ||
return headOr($array, $default); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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`. | ||
* | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can move to the docblock of the |
||
# 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; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the SolidWorx Lodash-PHP project. | ||
* | ||
* @author Pierre du Plessis <open-source@solidworx.co> | ||
* @copyright Copyright (c) 2017 | ||
*/ | ||
|
||
namespace _; | ||
|
||
/** | ||
* Casts `value` as an array if it's not one. | ||
* | ||
* @param mixed $value The value to cast | ||
* | ||
* @returns {Array} Returns the cast array. | ||
* @param array $array The array to concatenate. | ||
* | ||
* @return array Returns the cast array. | ||
* | ||
* @example | ||
* | ||
* _.castArray(1); | ||
* // => [1] | ||
* | ||
* _.castArray({ 'a': 1 }); | ||
* // => [{ 'a': 1 }] | ||
* | ||
* _.castArray('abc'); | ||
* // => ['abc'] | ||
* | ||
* _.castArray(null); | ||
* // => [null] | ||
* | ||
* _.castArray(undefined); | ||
* // => [undefined] | ||
* | ||
* _.castArray(); | ||
* // => [] | ||
* | ||
* var array = [1, 2, 3]; | ||
* console.log(_.castArray(array) === array); | ||
* // => true | ||
*/ | ||
function castArray($value): array | ||
{ | ||
$check = function ($value): array { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be simplified to the following: function castArray($value): array
{
return (array) $value;
} |
||
return \is_array($value) ? $value : [$value]; | ||
}; | ||
|
||
return $check($value); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the SolidWorx Lodash-PHP project. | ||
* | ||
* @author Pierre du Plessis <open-source@solidworx.co> | ||
* @copyright Copyright (c) 2017 | ||
*/ | ||
|
||
namespace _; | ||
|
||
use SebastianBergmann\Comparator\ComparisonFailure; | ||
use SebastianBergmann\Comparator\Factory; | ||
|
||
/** | ||
* Check if the value is an Array and that the keys are entirely numeric (integer) | ||
* | ||
* @category Lang | ||
* | ||
* @param mixed $value The value to check. | ||
* | ||
* @return bool Returns `true` if the value is an array, else `false`. | ||
* | ||
* @example | ||
* <code> | ||
* | ||
* $object = [ 'a' => 1] | ||
* $array = [ 'a' ] | ||
* | ||
* isArrayLike($array) | ||
* // => true | ||
* | ||
* isArrayLike($object) | ||
* // => false | ||
* | ||
* </code> | ||
*/ | ||
function isArrayLike(mixed $value): bool { | ||
return is_array($value) && every(array_keys($value), function ($key) { | ||
return is_int($key); | ||
}); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the SolidWorx Lodash-PHP project. | ||
* | ||
* @author Pierre du Plessis <open-source@solidworx.co> | ||
* @copyright Copyright (c) 2017 | ||
*/ | ||
|
||
namespace _; | ||
|
||
use SebastianBergmann\Comparator\ComparisonFailure; | ||
use SebastianBergmann\Comparator\Factory; | ||
|
||
/** | ||
* Check if the value is an Array and that not all keys are entirely numeric (integer) | ||
* | ||
* @category Lang | ||
* | ||
* @param mixed $value The value to check. | ||
* | ||
* @return bool Returns `true` if the value is an object-like array, else `false`. | ||
* | ||
* @example | ||
* <code> | ||
* | ||
* $object = [ 'a' => 1] | ||
* $array = [ 'a' ] | ||
* | ||
* isObjectLike($array) | ||
* // => false | ||
* | ||
* isObjectLike($object) | ||
* // => true | ||
* | ||
* </code> | ||
*/ | ||
function isObjectLike(mixed $value): bool { | ||
return $value instanceof \stdClass || is_array($value) && !every(array_keys($value), function ($key) { | ||
return is_int($key); | ||
}); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the SolidWorx Lodash-PHP project. | ||
* | ||
* @author Pierre du Plessis <open-source@solidworx.co> | ||
* @copyright Copyright (c) 2018 | ||
*/ | ||
|
||
namespace _; | ||
|
||
use function _\internal\basePick; | ||
use function _\internal\flatRest; | ||
|
||
/** | ||
* Assigns own enumerable string keyed properties of source objects to the | ||
* destination object. Source objects are applied from left to right. | ||
* Subsequent sources overwrite property assignments of previous sources. | ||
* | ||
* **Note:** This method mutates `object` and is loosely based on | ||
* [`Object.assign`](https://mdn.io/Object/assign). | ||
* | ||
* @param object $object The destination object. | ||
* @param object ...$sources The source objects. | ||
* @return object Returns the new object. | ||
* @category Object | ||
* @since 0.10.0 | ||
* @example | ||
* | ||
* function Foo() { | ||
* this.a = 1; | ||
* } | ||
* | ||
* function Bar() { | ||
* this.c = 3; | ||
* } | ||
* | ||
* Foo.prototype.b = 2; | ||
* Bar.prototype.d = 4; | ||
* | ||
* _.assign({ 'a': 0 }, new Foo, new Bar); | ||
* // => { 'a': 1, 'c': 3 } | ||
*/ | ||
function assign(object $object, object ...$sources): \stdClass | ||
{ | ||
foreach ($sources as $source) { | ||
foreach ($source as $key => $value) { | ||
$object->$key = $value; | ||
} | ||
} | ||
return $object; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the SolidWorx Lodash-PHP project. | ||
* | ||
* @author Pierre du Plessis <open-source@solidworx.co> | ||
* @copyright Copyright (c) 2018 | ||
*/ | ||
|
||
namespace _; | ||
|
||
use function _\internal\basePick; | ||
use function _\internal\flatRest; | ||
|
||
/** | ||
* The opposite of `_.pick`; this method creates an object composed of the | ||
* own and inherited enumerable property paths of `object` that are not omitted. | ||
* | ||
* **Note:** This method is considerably slower than `_.pick`. | ||
* | ||
* @static | ||
* @since 0.1.0 | ||
* @memberOf _ | ||
* @category Object | ||
* @param array $object The source object. | ||
* @returns array Returns the new object. | ||
* @example | ||
* | ||
* var object = { 'a': 1, 'b': '2', 'c': 3 }; | ||
* | ||
* _.omit(object, ['a', 'c']); | ||
* // => { 'b': '2' } | ||
*/ | ||
function omit(array $object, array $paths): array | ||
{ | ||
$result = []; | ||
foreach ($object as $key => $value) { | ||
if (!in_array($key, $paths)) { | ||
$result[$key] = $value; | ||
} | ||
} | ||
return $result; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All the changes in this file should be reverted