export const {
/**
- * A RegExp·like object which only matches entire strings.
+ * A RegExp·like object which only matches entire strings, and may
+ * have additional constraints specified.
*
* Matchers are callable objects and will return true if they are
* called with a string that they match, and false otherwise.
* Matchers will always return false if called with nonstrings,
- * although other methods like `exec` may still return true.
+ * although other methods like `exec` coerce their arguments and may
+ * still return true.
*/
Matcher,
} = (() => {
Object.getOwnPropertyDescriptor(rePrototype, "unicode").get;
const Matcher = class extends identity {
+ #constraint;
#regExp;
/**
*
* 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
+ * 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.
+ *
* ☡ If the provided source regular expression uses nongreedy
* quantifiers, it may not match the whole string even if a match
* with the whole string is possible. Surround the regular
* expression with `^(?:` and `)$` if you don’t want nongreedy
* regular expressions to fail when shorter matches are possible.
*/
- constructor(source, name = undefined) {
+ constructor(source, name = undefined, constraint = null) {
super(
($) => {
if (typeof $ !== "string") {
} else {
// The provided value is a string. Set the `lastIndex` of
// the regular expression to 0 and see if the first attempt
- // at a match matches the whole string.
+ // at a match matches the whole string and passes the
+ // provided constraint (if present).
regExp.lastIndex = 0;
- return call(reExec, regExp, [$])?.[0] === $;
+ return call(reExec, regExp, [$])?.[0] === $ &&
+ (constraint === null || constraint($, this));
}
},
);
return new RE(source);
}
})();
- return defineOwnProperties(
- setPrototype(this, matcherPrototype),
- {
- lastIndex: {
- configurable: false,
- enumerable: false,
- value: 0,
- writable: false,
- },
- name: {
- value: name != null
- ? `${name}`
- : `Matcher(${call(reToString, regExp, [])})`,
+ if (constraint !== null && typeof constraint !== "function") {
+ throw new TypeError(
+ "Piscēs: Cannot construct Matcher: Constraint is not callable.",
+ );
+ } else {
+ this.#constraint = constraint;
+ return defineOwnProperties(
+ setPrototype(this, matcherPrototype),
+ {
+ lastIndex: {
+ configurable: false,
+ enumerable: false,
+ value: 0,
+ writable: false,
+ },
+ name: {
+ value: name != null
+ ? `${name}`
+ : `Matcher(${call(reToString, regExp, [])})`,
+ },
},
- },
- );
+ );
+ }
}
/** Gets whether the dotAll flag is present on this Matcher. */
*/
exec($) {
const regExp = this.#regExp;
+ const constraint = this.#constraint;
const string = `${$}`;
regExp.lastIndex = 0;
const result = call(reExec, regExp, [string]);
- if (result?.[0] === string) {
- // The entire string was matched.
+ if (
+ result?.[0] === string &&
+ (constraint === null || constraint(string, this))
+ ) {
+ // The entire string was matched and the constraint, if
+ // present, returned a truthy value.
return result;
} else {
- // The entire string was not matched.
+ // The entire string was not matched or the constraint returned
+ // a falsey value.
return null;
}
}