Last active
April 15, 2023 04:18
-
-
Save westc/767681a206bb4aa768e6ac808a3b3a26 to your computer and use it in GitHub Desktop.
quoteRegExp() - This is an example of overloading a function and annotating it with JSDoc.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @type {{ | |
* (input: string): string; | |
* (input: string, returnRegExp: true): RegExp; | |
* (input: string, flags: string): RegExp; | |
* }} | |
*/ | |
const quoteRegExp = function(input, flags) { | |
input = input.replace(/[/\-\\^$*+?.()|[\]{}]/g, '\\$&'); | |
if (flags === true) flags = ''; | |
return 'string' === typeof flags ? new RegExp(input, flags) : input; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can also do the following but the names will not carry over (nor will any associated comments):