Skip to content

[Feature request] in replace and replaceAll, allow the replacer function to return "no-op" value #225

@Tal500

Description

@Tal500

The replace and replaceAll functions are mimicking the standard method of the string prototype in JS.

If you enter a replacer function on standard JS, and you see some match you don't want to change, you're simply return the original match in the function body.
This strategy allows the replacer function to do a "no-op" replacement.

Today, you might think you can do the same on magic-string, but in the sourcemap I think it will be shown that the substring was changed(e.g. "blabla" was replaced by "blabla"), but this was not the intention.

Can we allow for the replacement function to return sometimes a value of undefined? This way, whenever this value is being returned, magic-string will know not to replace this incident.

It might make sense in some cases to mark a substring as "replaced" in the sourcemap, even though it didn't change at all. This is why I'm not suggesting to check if the replacer return value was different, but rather suggest to check if it was returning undefined.

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