diff --git a/benchmarks/performance/escapeRegExp.bench.ts b/benchmarks/performance/escapeRegExp.bench.ts index c5b998040..78b92cc08 100644 --- a/benchmarks/performance/escapeRegExp.bench.ts +++ b/benchmarks/performance/escapeRegExp.bench.ts @@ -1,8 +1,10 @@ import { bench, describe } from 'vitest'; import { escapeRegExp as escapeRegExpToolkit_ } from 'es-toolkit'; +import { escapeRegExp as escapeRegExpCompatToolkit_ } from 'es-toolkit/compat'; import { escapeRegExp as escapeRegExpLodash_ } from 'lodash'; const escapeRegExpToolkit = escapeRegExpToolkit_; +const escapeRegExpCompatToolkit = escapeRegExpCompatToolkit_; const escapeRegExpLodash = escapeRegExpLodash_; describe('escape', () => { @@ -11,6 +13,10 @@ describe('escape', () => { escapeRegExpToolkit('^$.*+?()[]{}|\\'); }); + bench('es-toolkit/compat/escapeRegExp', () => { + escapeRegExpCompatToolkit('^$.*+?()[]{}|\\'); + }); + bench('lodash/escapeRegExp', () => { escapeRegExpLodash('^$.*+?()[]{}|\\'); }); @@ -19,6 +25,10 @@ describe('escape', () => { escapeRegExpToolkit(longString); }); + bench('es-toolkit/compat/escapeRegExp - long string', () => { + escapeRegExpCompatToolkit(longString); + }); + bench('lodash/escapeRegExp long string', () => { escapeRegExpLodash(longString); }); diff --git a/src/compat/index.ts b/src/compat/index.ts index 62ad8c21e..fae1660c3 100644 --- a/src/compat/index.ts +++ b/src/compat/index.ts @@ -166,6 +166,7 @@ export { camelCase } from './string/camelCase.ts'; export { deburr } from './string/deburr.ts'; export { endsWith } from './string/endsWith.ts'; export { escape } from './string/escape.ts'; +export { escapeRegExp } from './string/escapeRegExp.ts'; export { kebabCase } from './string/kebabCase.ts'; export { lowerCase } from './string/lowerCase.ts'; export { lowerFirst } from './string/lowerFirst.ts'; diff --git a/src/compat/string/escapeRegExp.spec.ts b/src/compat/string/escapeRegExp.spec.ts new file mode 100644 index 000000000..574f877d3 --- /dev/null +++ b/src/compat/string/escapeRegExp.spec.ts @@ -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); + }); +}); diff --git a/src/compat/string/escapeRegExp.ts b/src/compat/string/escapeRegExp.ts new file mode 100644 index 000000000..796b332a6 --- /dev/null +++ b/src/compat/string/escapeRegExp.ts @@ -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)); +}