Relevant source files
- index.js
- index.test-d.ts
Architecture
Overview
The escape-string-regexp library is a simple utility that escapes special characters in a string to make it a valid regular expression pattern.
Purpose
The primary purpose of this library is to provide a straightforward way to escape special characters in a string, making it safe for use in regular expressions.
Key Flows
- The library takes a string as input and checks if it's a valid regular expression pattern.
- If the string is not a valid regular expression pattern, the library escapes special characters in the string.
- The escaped string is then returned as the result.
Implementation
The implementation of the library is straightforward and relies on the RegExp object to check if the input string is a valid regular expression pattern. If it's not, the library uses the String.prototype.replace() method to escape special characters in the string.
Testing
The library includes a test suite to ensure that it works correctly in different scenarios. The tests cover various edge cases, including strings with special characters and invalid regular expression patterns.
graph TD
A[input string]
B[check if valid regex]
C[escape special chars]
D[return escaped string]
A --> B
B -->|yes| C
B -->|no| C
C --> DSources: index.js index.test-d.ts