Skip to content

0.38.0 - Revamped replacing interface

Compare
Choose a tag to compare
@danon danon released this 23 Oct 11:43
· 221 commits to master since this release

😎 T-Regx The Dinosaur is really proud to announce its release 0.38.0!

Previous state of replacing API in T-Regx was, frankly, hopeless. It was full of chainable, complex interface, polymorphic instances, unhelpful names, reading count required passing an anonymous function to counting() - it was horrible. We've started working on a revamp 6 months ago, simulateneuously delivering other updates, and today we're proud to say the replacing API revamp is complete. We removed horrible by() function, it functionallity is now available through callback()/withGroup() methods. We removed counting() callback interface, for a more object-oriented Replace.count() call, which is performance optimized. That is, when Replace.count() is called on its own, it will perform a global search, counting values, but if Replace.with() (or other replacing function) is called, then its amount is used and further calls to Replace.count() will not perform additional calls. That way the user can freely use Replace.count() without worying about what calls will do what. Chainable interface was minimized to the absolute minimum - nothing is chainable, except for limit(int) method. There is also no polymorphic APIs, which greatly increase the readability of the API and its cohesion.

The newly revamped replacing API:

/**
 * Instantiate your pattern
 */
$pattern = Pattern::of('\d+');

/**
 * Match the pattern against the subject
 */
$replace = $pattern->replace("My numbers are: 12, 13, 14");

/**
 * Replace occurrances of the pattern in the subject
 */
$replace->callback(fn(Detail $detail) => "X");

/**
 * Count the replacements performed
 */
echo "I replaced " . $replace->count() . " occurrences";

The detailed list of changes is in ChangeLog.md.

  • Breaking changes
    • Revamped Pattern.replace() functionality.
    • Removed focus() and substitute() - useful feature, however it proved to be unreliable when used with
      look-aheads, look-behinds and \K resets. The functionallity is available through a separate "t-regx" package.
    • Callback in replace().callback() no longer accepts Detail or Group as a return value.

Migration guide:

  • Removed focus() - To continue using functinallity, use a separate package.
  • Removed Group.substitute() - To continue using functinallity, use a separate package.
  • Removed replace().by() - use replace().callback() or replace().withGroup()
  • Removed replace().counting() - use replace.count()
  • Removed replace().all() - omit call to all()
  • Updated replace().callback() return type - return Detail.text() or Group.text() as a return value.
  • Renamed ReplaceLimit to Replace.

The detailed list of changes is in ChangeLog.md.

Rawrrrrrrr!