Skip to content

Commit 140edad

Browse files
committed
Lodash: Refactor away from _.words()
1 parent 8e8a8c5 commit 140edad

File tree

4 files changed

+22
-5
lines changed

4 files changed

+22
-5
lines changed

.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ module.exports = {
142142
'uniqueId',
143143
'uniqWith',
144144
'values',
145+
'words',
145146
'zip',
146147
],
147148
message:

package-lock.json

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/block-editor/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
"@wordpress/url": "file:../url",
6060
"@wordpress/warning": "file:../warning",
6161
"@wordpress/wordcount": "file:../wordcount",
62+
"change-case": "^4.1.2",
6263
"classnames": "^2.3.1",
6364
"colord": "^2.7.0",
6465
"diff": "^4.0.2",
@@ -68,6 +69,7 @@
6869
"react-autosize-textarea": "^7.1.0",
6970
"react-easy-crop": "^3.0.0",
7071
"rememo": "^4.0.0",
72+
"remove-accents": "^0.4.2",
7173
"traverse": "^0.6.6"
7274
},
7375
"peerDependencies": {

packages/block-editor/src/components/inserter/search-items.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
/**
22
* External dependencies
33
*/
4-
import { deburr, find, words } from 'lodash';
4+
import { noCase } from 'change-case';
5+
import removeAccents from 'remove-accents';
6+
import { find } from 'lodash';
57

68
// Default search helpers.
79
const defaultGetName = ( item ) => item.name || '';
@@ -21,7 +23,7 @@ const defaultGetCollection = () => null;
2123
function normalizeSearchInput( input = '' ) {
2224
// Disregard diacritics.
2325
// Input: "média"
24-
input = deburr( input );
26+
input = removeAccents( input );
2527

2628
// Accommodate leading slash, matching autocomplete expectations.
2729
// Input: "/media"
@@ -34,6 +36,17 @@ function normalizeSearchInput( input = '' ) {
3436
return input;
3537
}
3638

39+
/**
40+
* Extracts words from an input string.
41+
*
42+
* @param {string} input The input string.
43+
*
44+
* @return {Array} Words, extracted from the input string.
45+
*/
46+
function extractWords( input = '' ) {
47+
return noCase( input ).split( ' ' ).filter( Boolean );
48+
}
49+
3750
/**
3851
* Converts the search term into a list of normalized terms.
3952
*
@@ -42,8 +55,7 @@ function normalizeSearchInput( input = '' ) {
4255
* @return {string[]} The normalized list of search terms.
4356
*/
4457
export const getNormalizedSearchTerms = ( input = '' ) => {
45-
// Extract words.
46-
return words( normalizeSearchInput( input ) );
58+
return extractWords( normalizeSearchInput( input ) );
4759
};
4860

4961
const removeMatchingTerms = ( unmatchedTerms, unprocessedTerms ) => {
@@ -150,7 +162,7 @@ export function getItemSearchRank( item, searchTerm, config = {} ) {
150162
category,
151163
collection,
152164
].join( ' ' );
153-
const normalizedSearchTerms = words( normalizedSearchInput );
165+
const normalizedSearchTerms = extractWords( normalizedSearchInput );
154166
const unmatchedTerms = removeMatchingTerms(
155167
normalizedSearchTerms,
156168
terms

0 commit comments

Comments
 (0)