Skip to content

Using a replaceWith function fails in some cases #64

@hogpilot

Description

@hogpilot

Providing a function for the replaceWith option only works if the function (when converted to a string) does not contain a "." character. This is due to the regex test here:

  if(!(TEST_REGEX.test(options.replaceWith))) {
    replaceWith = options.replaceWith;
  }

Recommend allowing any function for replaceWith by changing that block to:

  if(typeof options.replaceWith === 'function' || !(TEST_REGEX.test(options.replaceWith))) {
    replaceWith = options.replaceWith;
  }

By doing this, users can be more specific with their replace value, e.g.:

mongoSanitize({ replaceWith: (match) => {
  switch (match) {
    case '$':
      return '_dollar_';
    case '.': // this line currently triggers the `TEST_REGEX.test(options.replaceWith)`
      return '_dot_';
    default:
      return '_';
  }
} })

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