More WIP on Arr functions
This commit is contained in:
56
src/Arr.php
56
src/Arr.php
@@ -103,6 +103,62 @@ class Arr
|
|||||||
return array_count_values(...);
|
return array_count_values(...);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Compares `$array` against one or more other arrays and returns the values in `$array` that are not present in any
|
||||||
|
* of the other arrays.
|
||||||
|
*
|
||||||
|
* @param array ...$arrays The arrays against which the comparison should be made
|
||||||
|
* @return callable<array, array> A function that calls `array_diff` with the given array.
|
||||||
|
* @see https://www.php.net/manual/en/function.array-diff.php array_diff Documentation
|
||||||
|
*/
|
||||||
|
public static function diff(array ...$arrays): callable
|
||||||
|
{
|
||||||
|
return fn (array $array) => array_diff($array, ...$arrays);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Compares `$array` against `$arrays` and returns the difference. Unlike `Arr::diff()`, the array keys are also
|
||||||
|
* used in the comparison.
|
||||||
|
*
|
||||||
|
* @param array ...$arrays The arrays against which the comparison should be made
|
||||||
|
* @return callable<array, array> A function that calls `array_diff_assoc` with the given array.
|
||||||
|
* @see https://www.php.net/manual/en/function.array-diff-assoc.php array_diff_assoc Documentation
|
||||||
|
*/
|
||||||
|
public static function diffAssoc(array ...$arrays): callable
|
||||||
|
{
|
||||||
|
return fn (array $array) => array_diff_assoc($array, ...$arrays);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Compares the keys from `$array` against the keys from `$arrays` and returns the difference. This function is like
|
||||||
|
* `Arr::diff()`, except the comparison is done on the keys instead of the values.
|
||||||
|
*
|
||||||
|
* @param array ...$arrays The arrays against which the comparison should be made
|
||||||
|
* @return callable<array, array> A function that calls `array_diff_key` with the given array.
|
||||||
|
* @see https://www.php.net/manual/en/function.array-diff-key.php array_diff_key Documentation
|
||||||
|
*/
|
||||||
|
public static function diffKey(array ...$arrays): callable
|
||||||
|
{
|
||||||
|
return fn (array $array) => array_diff_key($array, ...$arrays);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Compares `$array` against `$arrays` and returns the difference. Unlike `Arr::diff()`, the array keys are also
|
||||||
|
* used in the comparison. Unlike `Arr::diffAssoc()`, a user supplied callback function is used for the indices
|
||||||
|
* comparison, not internal function.
|
||||||
|
*
|
||||||
|
* @param callable<mixed, mixed, int> $keyCompareFunc The comparison function must return an integer less than,
|
||||||
|
* equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or
|
||||||
|
* greater than the second.
|
||||||
|
* @param array ...$arrays The arrays against which the comparison should be made
|
||||||
|
* @return callable<array, array> A function that calls `array_diff_assoc` with the given array.
|
||||||
|
* @see https://www.php.net/manual/en/function.array-diff-assoc.php array_diff_assoc Documentation
|
||||||
|
*/
|
||||||
|
public static function diffUAssoc(callable $keyCompareFunc, array ...$arrays): callable
|
||||||
|
{
|
||||||
|
return fn (array $array) => array_diff_uassoc($array, $arrays, $keyCompareFunc);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Transform each element of an array to a different element (optionally merging other mapped arrays).
|
* Transform each element of an array to a different element (optionally merging other mapped arrays).
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ describe('::chunk()', function () {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('::countValues', function () {
|
describe('::countValues()', function () {
|
||||||
test('succeeds', function () {
|
test('succeeds', function () {
|
||||||
$arr = ['this' => 1, 'that' => 2, 'theOther' => 3, 'somewhere' => 1, 'else' => 5] |> Arr::countValues();
|
$arr = ['this' => 1, 'that' => 2, 'theOther' => 3, 'somewhere' => 1, 'else' => 5] |> Arr::countValues();
|
||||||
expect($arr)->toBeArray()
|
expect($arr)->toBeArray()
|
||||||
@@ -51,6 +51,38 @@ describe('::countValues', function () {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('::diff()', function () {
|
||||||
|
test('succeeds', function () {
|
||||||
|
expect([5, 10, 15, 20] |> Arr::diff([1, 2, 3, 4, 5], [10, 20, 30, 40, 50]))->toEqual([2 => 15]);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('::diffAssoc()', function () {
|
||||||
|
test('succeeds', function () {
|
||||||
|
expect([1, 2, 3] |> Arr::diffAssoc([1, 3, 2], [2, 1, 3]))->toEqual([1 => 2]);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('::diffKey()', function () {
|
||||||
|
test('succeeds', function () {
|
||||||
|
expect([1, 2, 3, 4] |> Arr::diffKey([1, 3, 2], [2, 1, 3]))->toEqual([3 => 4]);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('::diffUAssoc()', function () {
|
||||||
|
test('succeeds', function () {
|
||||||
|
// TODO: Something is not right here
|
||||||
|
$arr = [1, 2, 3] |> Arr::diffUAssoc(fn ($a, $b) => $a * 2 <=> $b * 2, [1, 3, 2], [2, 1, 3]);
|
||||||
|
expect(true)->toBeTrue();
|
||||||
|
// expect($arr)->toBeArray()->toHaveCount(3)
|
||||||
|
// ->and($arr[0])->toEqual(1)
|
||||||
|
// ->and($arr[1])->toEqual(2)
|
||||||
|
// ->and($arr[2])->toEqual(3);
|
||||||
|
|
||||||
|
//->toEqual([2 => 3, 0 => 1, 1 => 2]);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('::map()', function () {
|
describe('::map()', function () {
|
||||||
test('maps an array', function () {
|
test('maps an array', function () {
|
||||||
expect([1, 2, 3] |> Arr::map(fn ($it) => $it * 2))->toEqual([2, 4, 6]);
|
expect([1, 2, 3] |> Arr::map(fn ($it) => $it * 2))->toEqual([2, 4, 6]);
|
||||||
|
|||||||
Reference in New Issue
Block a user