Skip to content

Feedback on the library #25

@graphemecluster

Description

@graphemecluster

Hello! Your DM suggested I share some thoughts on the library, but since it would be lengthy to fit into a single Twitter meessage, I put them into this issue. I think it might be too many to open one issue per item, so I've put them all here for you:

  • It would be great if subroutines could be defined separately from the regex body.
    I could have defined them with variables outside and directly interpolated them without the use of subroutines, but consider:

    const dateRegex = regex`\d{4}-\d{2}-\d{2}`; // I want to but can't name the variable `date`
    const { groups: { date, time } } = regex`(?<date>${dateRegex})[T ](?<time>${timeRegexFromElsewhere})`.exec(str);

    This causes pollution to the current scope, while

    const { groups: { date, time } } = regex({
        subroutines: {
            date: regex`\d{4}-\d{2}-\d{2}`,
            time: timeRegexFromElsewhere,
        },
    })`(?<date>\g<date>)[T ](?<time>\g<time>)`.exec(str);

    doesn't.

  • It might be out of scope, but you want to think about transforming Unicode properties that aren't yet supported in ECMAScript, i.e. those other than General_Category, Script and Script_Extensions. The particularly useful ones are Block (blk), East_Asian_Width (ea), Numeric_Type (nt) and Vertical_Orientation (vo). (I once needed East_Asian_Width in my project, and ended up another dependency specific to it.)

  • Consider allowing direct interpolation after \u, \p, \P and \q to get rid of the tricky {${…}} syntax.

  • A subtle one, but let's wrap regex.js line 109 in a try block such that the transformed RegExp is visible on error.

Some questions:

  • What is the point to use patterns instead of directly interpolating regexs inside a regex, given that flags can be altered and RegExps can be nested with regex but not with pattern?

  • Is there any particular reason not to use [Symbol.match] and [Symbol.replace] but a(n optional) wrapper class? Is it because using them would be insufficient to cover all cases?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions