midnite81/loopy

This package is not installable via Composer 1.x, please make sure you upgrade to Composer 2+. Read more about our Composer 1.x deprecation policy.

Adds functionality for iterables

1.0.0 2021-08-13 20:58 UTC

This package is auto-updated.

Last update: 2021-10-14 09:23:01 UTC


README

Latest Stable Version Total Downloads Latest Unstable Version License Build Coverage Status

This is a PHP package which adds some namespaced array functions. Some of these functions can be natively accessed using array_* however there are few which aren't natively available.

Installation

This package is available for PHP7.4+. To install use composer

composer require midnite81\loopy

Available Functions

Definitions

each

each(iterable $items, Closure $callback): void

This function loops over each item. If the result of the closure returns false, then it will break the iteration at the point it returns false.

Example

use function Midnite81\Loopy\each;

$colours = [
    'blue',
    'red',
    'green',
    'yellow'
];

each($colours, function($colour, $key) {
    echo $colour . " is at index " . $key . "\n";
});

Result

blue is at index 0
red is at index 1
green is at index 2
yellow is at index 3

all

all(iterable $items, Closure $callback): bool

This function checks to see if the result of the closure ($callback) is true for all iterations of the iterable passed ($items)

Example

use function Midnite81\Loopy\all;

$employees = [
    "id395" => ["name" => 'bob', "age" => 42, "dept" => 2],
    "id492" => ["name" => 'dave', "age" => 34, "dept" => 2],
    "id059" => ["name" => 'susan', "age" => 23, "dept" => 2],
];

$allBobs = all($employees, fn($employee) => $employee['name'] === 'bob');
// please note the key is also passed to the closure; therefore if the key is necessary
// to your function you could for example do the following 
// $allBobs = all($employees, fn($employee, $key) => $employee['name'] === 'bob' && $key != 'id000');

Result

$allBobs = false;
// thankfully, not everyone in the department is called bob

some

some(iterable $items, Closure $callback): bool

This function checks to see if the result of the closure ($callback) is true for one or more iterations of the iterable passed ($items)

Example

use function Midnite81\Loopy\some;

$employees = [
    "id395" => ["name" => 'bob', "age" => 42, "dept" => 2],
    "id492" => ["name" => 'dave', "age" => 34, "dept" => 2],
    "id059" => ["name" => 'susan', "age" => 23, "dept" => 2],
];

$allBobs = some($employees, fn($employee) => $employee['name'] === 'bob');

// please note the key is also passed to the closure; therefore if the key is necessary
// to your function you could for example do the following 
// $allBobs = some($employees, fn($employee, $key) => $employee['name'] === 'bob' && $key != 'id000');

Result

$allBobs = true;
// one or more people in the department are called bob

map

map(iterable $items, Closure $callback): array

Example

use function Midnite81\Loopy\map;

$employees = [
    "id395" => ["name" => 'bob', "age" => 42, "dept" => 2],
    "id492" => ["name" => 'dave', "age" => 34, "dept" => 2],
    "id059" => ["name" => 'susan', "age" => 23, "dept" => 2],
];

$allBobs = map($employees, fn($employee, $key) => $employee['name']');

Result

$allBobs = [
    'bob',
    'dave',
    'susan',
]

reduce

reduce(iterable $items, Closure $callback, string|int|float $initial = ""): string|int|float

This function reduces down the values of an array to a single string, integer or float.

Example

use function Midnite81\Loopy\reduce;

$moneyReceived = [
    20.00,
    3.92,
    3.01,
    27.00
];

$totalMoneyReceived = reduce($moneyReceived, fn($current, $value, $key) => (float)$current + $value);
// $current is the current value of the reducer

Result

$totalMoneyReceived = 53.93;

filter

filter(iterable $items, Closure $callback, bool $preserveKey = false): array

This function filters down the iterable ($items) passed by only including what is true in the Closure ($callback). By default, the key is not preserved, but you can set it to true, if you wish to preserve the key.

Example

use function Midnite81\Loopy\filter;

$users = [
            ["name" => 'dave'],
            ["name" => 'susan'],
            ["name" => 'ingrid'],
            ["name" => 'patricia'],
            ["name" => 'sally'],
        ];

$usersWhoseNamesDontStartWithS = filter($users, fn($user) => !str_starts_with($user['name'], "s"));

Result

$usersWhoseNamesDontStartWithS = [
    'dave',
    'ingrid',
    'patricia'
]

times

times(iterable $items, Closure $callback, int $times): bool

This function checks to see that the instance of the call back ($callback) should only be found the specified number of times in the iterable ($items)

Example

use function Midnite81\Loopy\times;

$peopleOnTheBus = [
    'andy',
    'bob',
    'sally',
    'wendy',
    'bob'
];

$areThereTwoBobsOnTheBus = times($peopleOnTheBus, fn($people) => $people === 'bob', 2);

Result

$areThereTwoBobsOnTheBus = true;

once

once(iterable $items, Closure $callback): bool

Once is exactly the same as times however it will ensure the result of the closure only appears once in the iterable passed;

Example

use function Midnite81\Loopy\once;

$peopleOnTheBus = [
    'andy',
    'bob',
    'sally',
    'wendy'
];

$isThereJustOneAndyOnTheBus = once($peopleOnTheBus, fn($people) => $people === 'andy');

Result

$isThereJustOneAndyOnTheBus = true;