Skip to content

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

Closed
kylemilloy wants to merge 5 commits intobrick:masterfrom
kylemilloy:master
Closed

feat(#84): add ability to optionally apply abs and negated functions inline#85
kylemilloy wants to merge 5 commits intobrick:masterfrom
kylemilloy:master

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.

@BenMorel
Copy link
Member

Closing as per #84.

@BenMorel BenMorel closed this Feb 28, 2025
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

2 participants