Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lodash: Refactor away from _.escapeRegExp() #43629

Merged
merged 2 commits into from
Aug 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/**
* External dependencies
*/
const { escapeRegExp } = require( 'lodash' );
const glob = require( 'glob' ).sync;
const { join } = require( 'path' );

Expand All @@ -17,7 +16,8 @@ const { version } = require( './package' );
* @type {string}
*/
const majorMinorRegExp =
escapeRegExp( version.replace( /\.\d+$/, '' ) ) + '(\\.\\d+)?';
version.replace( /\.\d+$/, '' ).replace( /[\\^$.*+?()[\]{}|]/g, '\\$&' ) +
'(\\.\\d+)?';

/**
* The list of patterns matching files used only for development purposes.
Expand Down Expand Up @@ -94,6 +94,7 @@ module.exports = {
'differenceWith',
'dropRight',
'each',
'escapeRegExp',
'extend',
'findIndex',
'findKey',
Expand Down
13 changes: 12 additions & 1 deletion bin/plugin/commands/changelog.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
const { groupBy, escapeRegExp, flow } = require( 'lodash' );
const { groupBy, flow } = require( 'lodash' );
const Octokit = require( '@octokit/rest' );
const { sprintf } = require( 'sprintf-js' );
const semver = require( 'semver' );
Expand Down Expand Up @@ -185,6 +185,17 @@ const REWORD_TERMS = {
docs: 'documentation',
};

/**
* Escapes the RegExp special characters.
*
* @param {string} string Input string.
*
* @return {string} Regex-escaped string.
*/
function escapeRegExp( string ) {
return string.replace( /[\\^$.*+?()[\]{}|]/g, '\\$&' );
}

/**
* Returns candidates based on whether the given labels
* are part of the allowed list.
Expand Down
2 changes: 2 additions & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@
- `TreeGrid`: Refactor away from `_.includes()` ([#43518](https://github.com/WordPress/gutenberg/pull/43518/)).
- `FormTokenField`: use `KeyboardEvent.code`, refactor tests to modern RTL and `user-event` ([#43442](https://github.com/WordPress/gutenberg/pull/43442/)).
- `DropdownMenu`: use `KeyboardEvent.code`, refactor tests to model RTL and `user-event` ([#43439](https://github.com/WordPress/gutenberg/pull/43439/)).
- `Autocomplete`: Refactor away from `_.escapeRegExp()` ([#43629](https://github.com/WordPress/gutenberg/pull/43629/)).
- `TextHighlight`: Refactor away from `_.escapeRegExp()` ([#43629](https://github.com/WordPress/gutenberg/pull/43629/)).

### Experimental

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
/**
* External dependencies
*/
import { debounce, escapeRegExp } from 'lodash';
import { debounce } from 'lodash';
import removeAccents from 'remove-accents';

/**
* WordPress dependencies
*/
import { useLayoutEffect, useState } from '@wordpress/element';

/**
* Internal dependencies
*/
import { escapeRegExp } from '../utils/strings';

function filterOptions( search, options = [], maxResults = 10 ) {
const filtered = [];
for ( let i = 0; i < options.length; i++ ) {
Expand Down
3 changes: 2 additions & 1 deletion packages/components/src/autocomplete/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { escapeRegExp, find } from 'lodash';
import { find } from 'lodash';
import removeAccents from 'remove-accents';

/**
Expand Down Expand Up @@ -34,6 +34,7 @@ import { speak } from '@wordpress/a11y';
* Internal dependencies
*/
import { getAutoCompleterUI } from './autocompleter-ui';
import { escapeRegExp } from '../utils/strings';

/**
* A raw completer option.
Expand Down
6 changes: 1 addition & 5 deletions packages/components/src/text-highlight/index.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* External dependencies
*/
import { escapeRegExp } from 'lodash';

/**
* WordPress dependencies
*/
Expand All @@ -11,6 +6,7 @@ import { createInterpolateElement } from '@wordpress/element';
/**
* Internal dependencies
*/
import { escapeRegExp } from '../utils/strings';
import type { TextHighlightProps } from './types';

/**
Expand Down
11 changes: 11 additions & 0 deletions packages/components/src/utils/strings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,14 @@ export const normalizeTextString = ( value: string ): string => {
.toLocaleLowerCase()
.replace( ALL_UNICODE_DASH_CHARACTERS, '-' );
};

/**
* Escapes the RegExp special characters.
*
* @param {string} string Input string.
*
* @return {string} Regex-escaped string.
*/
export function escapeRegExp( string: string ): string {
return string.replace( /[\\^$.*+?()[\]{}|]/g, '\\$&' );
}
12 changes: 11 additions & 1 deletion tools/webpack/blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* External dependencies
*/
const CopyWebpackPlugin = require( 'copy-webpack-plugin' );
const { escapeRegExp } = require( 'lodash' );
const { join, sep } = require( 'path' );
const fastGlob = require( 'fast-glob' );

Expand Down Expand Up @@ -30,6 +29,17 @@ const blockViewRegex = new RegExp(
*/
const prefixFunctions = [ 'build_query_vars_from_query_block' ];

/**
* Escapes the RegExp special characters.
*
* @param {string} string Input string.
*
* @return {string} Regex-escaped string.
*/
function escapeRegExp( string ) {
return string.replace( /[\\^$.*+?()[\]{}|]/g, '\\$&' );
}

const createEntrypoints = () => {
/*
* Returns an array of paths to block view files within the `@wordpress/block-library` package.
Expand Down