-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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 _.words()
#42467
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -142,6 +142,7 @@ module.exports = { | |
'uniqueId', | ||
'uniqWith', | ||
'values', | ||
'words', | ||
'zip', | ||
], | ||
message: | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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 || ''; | ||
|
@@ -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" | ||
|
@@ -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 ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: I would prefer There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, we're doing that in quite a bunch of places in Gutenberg already, and it's already become idiomatic in this codebase IMHO. |
||
} | ||
|
||
/** | ||
* Converts the search term into a list of normalized terms. | ||
* | ||
|
@@ -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 ) => { | ||
|
@@ -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 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
noCase
could be overkill given the string being operated on is human search text. Seems like the main thing it does is insert spaces into camel cased text, but I don't expect humans will write that way (even those of us that are coders 😄).edit - ah, I missed that this is also used on the block name, github had collapsed that part of the code, you can ignore this comment!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the feedback - FWIW
_.words()
is pretty costly, and while I haven't directly compared the costs withnoCase()
, I'm almost certain thatnoCase()
will be cheaper. Can thoroughly compare if you have doubts, though.Did you have any other reservations with the changes suggested in this PR, @talldan?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're all good. This was just a passing comment while I was reviewing #42459, which discussed this file.