-
Notifications
You must be signed in to change notification settings - Fork 355
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(compat): implement escapeRegExp (#873)
* feat(compat): implement escapeRegExp * make lint happy * fix type
- Loading branch information
Showing
4 changed files
with
55 additions
and
0 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
import { escapeRegExp } from './escapeRegExp'; | ||
import { map } from '../array/map'; | ||
import { stubString } from '../util/stubString'; | ||
|
||
describe('escapeRegExp', () => { | ||
const escaped = '\\^\\$\\.\\*\\+\\?\\(\\)\\[\\]\\{\\}\\|\\\\'; | ||
const unescaped = '^$.*+?()[]{}|\\'; | ||
|
||
it('should escape values', () => { | ||
expect(escapeRegExp(unescaped + unescaped)).toBe(escaped + escaped); | ||
}); | ||
|
||
it('should handle strings with nothing to escape', () => { | ||
expect(escapeRegExp('abc')).toBe('abc'); | ||
}); | ||
|
||
it('should return an empty string for empty values', () => { | ||
// eslint-disable-next-line no-sparse-arrays | ||
const values = [, null, undefined, '']; | ||
const expected = map(values, stubString); | ||
|
||
const actual = map(values, (value, index) => (index ? escapeRegExp(value as any) : escapeRegExp())); | ||
|
||
expect(actual).toEqual(expected); | ||
}); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { escapeRegExp as escapeRegExpToolkit } from '../../string/escapeRegExp.ts'; | ||
import { toString } from '../util/toString.ts'; | ||
|
||
/** | ||
* Escapes the RegExp special characters "^", "$", "\\", ".", "*", "+", "?", "(", ")", "[", "]", "{", "}", and "|" in `str`. | ||
* | ||
* @param {string} str The string to escape. | ||
* @returns {string} Returns the escaped string. | ||
* | ||
* @example | ||
* import { escapeRegExp } from 'es-toolkit/string'; | ||
* | ||
* escapeRegExp('[es-toolkit](https://es-toolkit.slash.page/)'); // returns '\[es-toolkit\]\(https://es-toolkit\.slash\.page/\)' | ||
*/ | ||
export function escapeRegExp(str?: string): string { | ||
return escapeRegExpToolkit(toString(str)); | ||
} |