callstack/ts-regex-builder

feat: implement `capture`

okwasniewski opened this issue · 2 comments

Describe the feature

Implement capture() component

let regex = buildRegex(
    "a", capture(one("c"), oneOrMore("ab")) // a(c(?:ab)+)
)

References

https://developer.apple.com/documentation/regexbuilder/capture

Related Issues

My suggestions:

Swift:

Regex {
    // Basic
    Capture {
      OneOrMore(.digit)
      "."
      Repeat(.digit, count: 2)
    }
   // With transform
    Capture {
      OneOrMore(.digit)
      "."
      Repeat(.digit, count: 2)
    } transform: { Double($0)! }
}

Proposed API:

buildRegex(
  // Basic
  capture(
     oneOrMore(digit),
     ".",
     repeat({ count: 2}, digit),
  )
  // With transform
  capture(
     { transform: (n) => parseFloat(n) },
     oneOrMore(digit),
     ".",
     repeat({ count: 2}, digit),
  )
}

Providing transform is optional.

I don't know if config (transform) should be the first or last parameter. JS conventions would suggest first due to the presence of vargargs.

Note that we probably should keep the transform key explicit, as the Swift API also has some as an argument, which we might want to implement)

Implemented in #24