-
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 _.forEach()
#43807
Conversation
Size Change: +84 B (0%) Total Size: 1.25 MB
ℹ️ View Unchanged
|
6d95697
to
c875693
Compare
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.
LGTM, great work as usual, @tyxla! 👍
@@ -32,8 +27,8 @@ export function hideApp( unhiddenElement ) { | |||
if ( isHidden ) { | |||
return; | |||
} | |||
const elements = document.body.children; | |||
forEach( elements, ( element ) => { | |||
const elements = Array.from( document.body.children ); |
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.
TIL the collections returned by Element.prototype.children()
don't have a forEach
method...
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.
Yeah, HTMLCollection
is limited to having .length
, but doesn't reliably implement the iterator protocol in all browsers. Some browser support some means of iteration (like for/of
and for/in
for example) but not reliably, so Array.from()
ensures we'll be able to iterate the collection of elements.
What?
This PR removes the
_.forEach()
usage completely and deprecates the function.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 essentially replacing with
Array.prototype.forEach()
, with a few specifics in mind:Object.entries()
when we use both the object keys and values.Object.keys()
when we use only the object keys.Object.values()
when we use only the object values.The PR might look a little on the large side, but that's mostly because lots of the indentation needs to change after the minor changes, so most of it consists of whitespace changes.
Testing Instructions
@wordpress/babel-plugin-makepot
works, runnpm run test:unit packages/babel-plugin-makepot/test/index.js
and verify they still pass.withGlobalEvents
work, verify its tests still pass:npm run test:unit packages/compose/src/higher-order/with-global-events/test/index.js
@wordpress/data
registry still works, verify these tests still pass:npm run test:unit packages/data/src/test/registry.js
trunk
and ensure nothing has changed).npm run test:unit packages/edit-site/src/components/global-styles
npm run test:unit packages/viewport