Skip to content

Commit

Permalink
feat(compat): implement escapeRegExp (#873)
Browse files Browse the repository at this point in the history
* feat(compat): implement escapeRegExp

* make lint happy

* fix type
  • Loading branch information
D-Sketon authored Dec 8, 2024
1 parent fad6457 commit c630b66
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 0 deletions.
10 changes: 10 additions & 0 deletions benchmarks/performance/escapeRegExp.bench.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand All @@ -11,6 +13,10 @@ describe('escape', () => {
escapeRegExpToolkit('^$.*+?()[]{}|\\');
});

bench('es-toolkit/compat/escapeRegExp', () => {
escapeRegExpCompatToolkit('^$.*+?()[]{}|\\');
});

bench('lodash/escapeRegExp', () => {
escapeRegExpLodash('^$.*+?()[]{}|\\');
});
Expand All @@ -19,6 +25,10 @@ describe('escape', () => {
escapeRegExpToolkit(longString);
});

bench('es-toolkit/compat/escapeRegExp - long string', () => {
escapeRegExpCompatToolkit(longString);
});

bench('lodash/escapeRegExp long string', () => {
escapeRegExpLodash(longString);
});
Expand Down
1 change: 1 addition & 0 deletions src/compat/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
27 changes: 27 additions & 0 deletions src/compat/string/escapeRegExp.spec.ts
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);
});
});
17 changes: 17 additions & 0 deletions src/compat/string/escapeRegExp.ts
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));
}

0 comments on commit c630b66

Please sign in to comment.