Skip to content

Commit

Permalink
Lodash: Refactor away from _.words()
Browse files Browse the repository at this point in the history
  • Loading branch information
tyxla committed Jul 15, 2022
1 parent 2c23ebe commit e63e974
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 20 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ module.exports = {
'uniqueId',
'uniqWith',
'values',
'words',
],
message:
'This Lodash method is not recommended. Please use native functionality instead. If using `memoize`, please use `memize` instead.',
Expand Down
17 changes: 2 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions packages/block-editor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"@wordpress/url": "file:../url",
"@wordpress/warning": "file:../warning",
"@wordpress/wordcount": "file:../wordcount",
"change-case": "^4.1.2",
"classnames": "^2.3.1",
"colord": "^2.7.0",
"diff": "^4.0.2",
Expand All @@ -68,6 +69,7 @@
"react-autosize-textarea": "^7.1.0",
"react-easy-crop": "^3.0.0",
"rememo": "^4.0.0",
"remove-accents": "^0.4.2",
"traverse": "^0.6.6"
},
"peerDependencies": {
Expand Down
22 changes: 17 additions & 5 deletions packages/block-editor/src/components/inserter/search-items.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/**
* External dependencies
*/
import { deburr, find, words } from 'lodash';
import { noCase } from 'change-case';
import removeAccents from 'remove-accents';
import { find } from 'lodash';

// Default search helpers.
const defaultGetName = ( item ) => item.name || '';
Expand All @@ -21,7 +23,7 @@ const defaultGetCollection = () => null;
function normalizeSearchInput( input = '' ) {
// Disregard diacritics.
// Input: "média"
input = deburr( input );
input = removeAccents( input );

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

/**
* Extracts words from an input string.
*
* @param {string} input The input string.
*
* @return {Array} Words, extracted from the input string.
*/
function extractWords( input = '' ) {
return noCase( input ).split( ' ' ).filter( Boolean );
}

/**
* Converts the search term into a list of normalized terms.
*
Expand All @@ -42,8 +55,7 @@ function normalizeSearchInput( input = '' ) {
* @return {string[]} The normalized list of search terms.
*/
export const getNormalizedSearchTerms = ( input = '' ) => {
// Extract words.
return words( normalizeSearchInput( input ) );
return extractWords( normalizeSearchInput( input ) );
};

const removeMatchingTerms = ( unmatchedTerms, unprocessedTerms ) => {
Expand Down Expand Up @@ -150,7 +162,7 @@ export function getItemSearchRank( item, searchTerm, config = {} ) {
category,
collection,
].join( ' ' );
const normalizedSearchTerms = words( normalizedSearchInput );
const normalizedSearchTerms = extractWords( normalizedSearchInput );
const unmatchedTerms = removeMatchingTerms(
normalizedSearchTerms,
terms
Expand Down

0 comments on commit e63e974

Please sign in to comment.