]> Lady’s Gitweb - Pisces/commitdiff
Pass exec result to constraint in Matcher
authorLady <redacted>
Thu, 8 Sep 2022 04:14:53 +0000 (21:14 -0700)
committerLady <redacted>
Fri, 12 May 2023 03:56:48 +0000 (20:56 -0700)
string.js
string.test.js

index 8c858cd66a527b56c8a0d591a75d77e1f5134b60..28412fc98b8854b9b26f248c5e8b8ae98526d033 100644 (file)
--- a/string.js
+++ b/string.js
@@ -65,11 +65,11 @@ export const {
      * A name for the matcher may be provided as the second argument.
      *
      * A callable constraint on acceptable inputs may be provided as a
-     * third argument. If provided, it will be called with two
+     * third argument. If provided, it will be called with three
      * arguments whenever a match appears successful: first, the string
-     * being matched, and second, the Matcher object itself. If the
-     * return value of this call is falsey, then the match will be
-     * considered a failure.
+     * being matched, second, the match result, and third, the Matcher
+     * object itself. If the return value of this call is falsey, then
+     * the match will be considered a failure.
      *
      * ☡ If the provided source regular expression uses nongreedy
      * quantifiers, it may not match the whole string even if a match
@@ -89,8 +89,9 @@ export const {
             // at a match matches the whole string and passes the
             // provided constraint (if present).
             regExp.lastIndex = 0;
-            return call(reExec, regExp, [$])?.[0] === $ &&
-              (constraint === null || constraint($, this));
+            const result = call(reExec, regExp, [$]);
+            return result?.[0] === $ &&
+              (constraint === null || constraint($, result, this));
           }
         },
       );
@@ -148,6 +149,10 @@ export const {
      *
      * Matchers only match if they can match the entire value on the
      * first attempt.
+     *
+     * ☡ The match result returned by this method will be the same as
+     * that passed to the constraint function—and may have been
+     * modified by said function prior to being returned.
      */
     exec($) {
       const regExp = this.#regExp;
@@ -157,7 +162,7 @@ export const {
       const result = call(reExec, regExp, [string]);
       if (
         result?.[0] === string &&
-        (constraint === null || constraint(string, this))
+        (constraint === null || constraint(string, result, this))
       ) {
         // The entire string was matched and the constraint, if
         // present, returned a truthy value.
index 52e848c3b75bcde588593512b66137d20ce34c22..6407d9415b2aa469006e517db5fc66adbd2c93e7 100644 (file)
@@ -95,15 +95,15 @@ describe("Matcher", () => {
 
     it("[[Call]] calls the constraint if the match succeeds", () => {
       const constraint = spy((_) => true);
-      const matcher = new Matcher(".*", undefined, constraint);
-      matcher.exec({
+      const matcher = new Matcher("(.).*", undefined, constraint);
+      const result = matcher.exec({
         toString() {
           return "etaoin";
         },
       });
       assertSpyCalls(constraint, 1);
       assertSpyCall(constraint, 0, {
-        args: ["etaoin", matcher],
+        args: ["etaoin", result, matcher],
         self: undefined,
       });
     });
@@ -204,13 +204,13 @@ describe("Matcher", () => {
 
     it("[[Call]] calls the constraint if the match succeeds", () => {
       const constraint = spy((_) => true);
-      const matcher = new Matcher(".*", undefined, constraint);
+      const matcher = new Matcher("(.).*", undefined, constraint);
       matcher("etaoin");
       assertSpyCalls(constraint, 1);
-      assertSpyCall(constraint, 0, {
-        args: ["etaoin", matcher],
-        self: undefined,
-      });
+      assertEquals(constraint.calls[0].args[0], "etaoin");
+      assertEquals([...constraint.calls[0].args[1]], ["etaoin", "e"]);
+      assertEquals(constraint.calls[0].args[2], matcher);
+      assertEquals(constraint.calls[0].self, undefined);
     });
 
     it("[[Call]] does not call the constraint if the match fails", () => {
This page took 0.027545 seconds and 4 git commands to generate.