Skip to content
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

feat(#84): add ability to optionally apply abs and negated functions inline #85

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

kylemilloy
Copy link

@kylemilloy kylemilloy commented Feb 4, 2025

This tickets resolves #84.

Added tests to cover off true/false and closures that return the same. The idea here is that you can optionally assign whether or not you want to make a value absolute or negated based on a logic gate inline. Compare the two code examples of how this is done:

What you would have to do before:

$number = $number->multipliedBy(3)
  ->dividedBy(2);

if ($number->toInt() % 2 === 0) {
  $number = $number->negated();
}

return $number->add(5)->toInt();

And what this allows you to do now:

return $number->multipliedBy(3)
  ->dividedBy(2)
  ->negated(fn (BigNumber $number) => $number->toInt() % 2 === 0)
  ->add(5)
  ->toInt();

or

function getBalance(bool $accountIsCreditNormal) {
    return $number->multipliedBy(3)
      ->dividedBy(2)
      ->negated($accountIsCreditNormal)
      ->add(5)
      ->toInt();
}

Syntax is much more clear and succinct. My use case for this as outlined in #84 is to bring this into brick/money where we require this where we're manipulating values in an accounting system.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature request: QoL improvement, optionally apply helpers like negated or abs with fluent API
1 participant