Skip to content

Added minBy.php #30

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
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
55 changes: 55 additions & 0 deletions src/Math/minBy.php
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) 2018
*/

namespace _;

use function _\internal\baseIteratee;

/**
* This method is like `min` except that it accepts `iteratee` which is
* invoked for each element in `array` to generate the criterion by which
* the value is ranked. The iteratee is invoked with one argument: (value).
*
* @category Math
*
* @param array $array The array to iterate over.
* @param callable|string $iteratee The iteratee invoked per element.
*
* @return mixed Returns the minimum value.
* @example
* <code>
* $objects = [['n' => 1], ['n' => 2]];
*
* minBy($objects, function($o) { return $o['n']; });
* // => ['n' => 1]
*
* // The `property` iteratee shorthand.
* minBy($objects, 'n');
* // => ['n' => 1]
* </code>
*/
function minBy(?array $array, $iteratee)
{
$iteratee = baseIteratee($iteratee);
$result = null;
$computed = null;

foreach ($array as $key => $value) {
$current = $iteratee($value);

if (null !== $current && (null === $computed ? ($current === $current) : $current < $computed)) {
Copy link
Member

Choose a reason for hiding this comment

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

$current === $current will always be true here

Copy link
Author

Choose a reason for hiding this comment

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

Сopy-paste from maxBy 😕

$computed = $current;
$result = $value;
}
}

return $result;
}
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy