Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using a replaceWith function fails in some cases #64

Open
hogpilot opened this issue May 11, 2021 · 1 comment
Open

Using a replaceWith function fails in some cases #64

hogpilot opened this issue May 11, 2021 · 1 comment

Comments

@hogpilot
Copy link

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 '_';
  }
} })
@fiznool
Copy link
Owner

fiznool commented Jan 14, 2022

Sorry for the delay in responding. This is a great idea - if you could make this change and include tests, I'd be happy to merge in.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants