-
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 editor away from _.find()
#46464
Conversation
@@ -122,8 +122,7 @@ export function PageAttributesParent() { | |||
const opts = getOptionsFromTree( tree ); | |||
|
|||
// Ensure the current parent is in the options list. | |||
const optsHasParent = find( | |||
opts, | |||
const optsHasParent = opts.find( |
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.
opts
will always be an array:
return sortedNodes.flat(); |
@@ -69,8 +64,7 @@ export default function PostFormat() { | |||
supportedFormats?.includes( format.id ) || postFormat === format.id | |||
); | |||
} ); | |||
const suggestion = find( | |||
formats, | |||
const suggestion = formats.find( |
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.
POST_FORMATS
are always an array:
gutenberg/packages/editor/src/components/post-format/index.js
Lines 17 to 29 in 2dea6e8
export const POST_FORMATS = [ | |
{ id: 'aside', caption: __( 'Aside' ) }, | |
{ id: 'audio', caption: __( 'Audio' ) }, | |
{ id: 'chat', caption: __( 'Chat' ) }, | |
{ id: 'gallery', caption: __( 'Gallery' ) }, | |
{ id: 'image', caption: __( 'Image' ) }, | |
{ id: 'link', caption: __( 'Link' ) }, | |
{ id: 'quote', caption: __( 'Quote' ) }, | |
{ id: 'standard', caption: __( 'Standard' ) }, | |
{ id: 'status', caption: __( 'Status' ) }, | |
{ id: 'video', caption: __( 'Video' ) }, | |
].sort( ( a, b ) => { | |
const normalizedA = a.caption.toUpperCase(); |
@@ -21,7 +21,7 @@ const getSuggestion = ( supportedFormats, suggestedPostFormat ) => { | |||
const formats = POST_FORMATS.filter( ( format ) => | |||
supportedFormats?.includes( format.id ) | |||
); | |||
return find( formats, ( format ) => format.id === suggestedPostFormat ); | |||
return formats.find( ( format ) => format.id === suggestedPostFormat ); |
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.
POST_FORMATS
are always an array:
gutenberg/packages/editor/src/components/post-format/index.js
Lines 17 to 29 in 2dea6e8
export const POST_FORMATS = [ | |
{ id: 'aside', caption: __( 'Aside' ) }, | |
{ id: 'audio', caption: __( 'Audio' ) }, | |
{ id: 'chat', caption: __( 'Chat' ) }, | |
{ id: 'gallery', caption: __( 'Gallery' ) }, | |
{ id: 'image', caption: __( 'Image' ) }, | |
{ id: 'link', caption: __( 'Link' ) }, | |
{ id: 'quote', caption: __( 'Quote' ) }, | |
{ id: 'standard', caption: __( 'Standard' ) }, | |
{ id: 'status', caption: __( 'Status' ) }, | |
{ id: 'video', caption: __( 'Video' ) }, | |
].sort( ( a, b ) => { | |
const normalizedA = a.caption.toUpperCase(); |
@@ -50,7 +50,7 @@ const isSameTermName = ( termA, termB ) => | |||
const termNamesToIds = ( names, terms ) => { | |||
return names.map( | |||
( termName ) => | |||
find( terms, ( term ) => isSameTermName( term.name, termName ) ).id | |||
terms.find( ( term ) => isSameTermName( term.name, termName ) ).id |
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.
Always an array:
gutenberg/packages/editor/src/components/post-taxonomies/flat-term-selector.js
Lines 195 to 202 in 2dea6e8
const uniqueTerms = termNames.reduce( ( acc, name ) => { | |
if ( | |
! acc.some( ( n ) => n.toLowerCase() === name.toLowerCase() ) | |
) { | |
acc.push( name ); | |
} | |
return acc; | |
}, [] ); |
@@ -95,7 +95,7 @@ export function sortBySelected( termsTree, terms ) { | |||
* @return {Object} Term object. | |||
*/ | |||
export function findTerm( terms, parent, name ) { | |||
return find( terms, ( term ) => { | |||
return terms.find( ( term ) => { |
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.
Always an array:
gutenberg/packages/editor/src/components/post-taxonomies/hierarchical-term-selector.js
Lines 211 to 213 in 2dea6e8
availableTerms: | |
getEntityRecords( 'taxonomy', slug, DEFAULT_QUERY ) || | |
EMPTY_ARRAY, |
@@ -203,7 +203,7 @@ export function FlatTermSelector( { slug } ) { | |||
|
|||
const newTermNames = uniqueTerms.filter( | |||
( termName ) => | |||
! find( availableTerms, ( term ) => | |||
! availableTerms.find( ( term ) => |
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.
Declared as an array above:
gutenberg/packages/editor/src/components/post-taxonomies/flat-term-selector.js
Lines 191 to 194 in 2dea6e8
const availableTerms = [ | |
...( terms ?? [] ), | |
...( searchResults ?? [] ), | |
]; |
Size Change: -25 B (0%) Total Size: 1.32 MB
ℹ️ View Unchanged
|
What?
This PR removes Lodash's
_.find()
from theeditor
package. There are a few usages in that package and conversion is pretty straightforward.Why?
Lodash is known to unnecessarily inflate the bundle size of packages, and in most cases, it can be replaced with native language functionality. See these for more information and rationale:
@wordpress/api-fetch
package haslodash
as a dependency #39495How?
We're using
Array.prototype.find()
instead of_.find()
.Testing Instructions