Skip to content

Commit

Permalink
Lodash: Remove from @wordpress/data (#47848)
Browse files Browse the repository at this point in the history
  • Loading branch information
tyxla authored Feb 8, 2023
1 parent 2634d17 commit 1646f03
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 7 deletions.
5 changes: 2 additions & 3 deletions package-lock.json

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

10 changes: 9 additions & 1 deletion packages/data/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,14 +164,22 @@ Integrating an existing redux store with its own reducers, store enhancers and m
_Example:_

```js
import { mapValues } from 'lodash';
import { register } from '@wordpress/data';
import existingSelectors from './existing-app/selectors';
import existingActions from './existing-app/actions';
import createStore from './existing-app/store';

const reduxStore = createStore();

const mapValues = ( obj, callback ) =>
Object.entries( obj ).reduce(
( acc, [ key, value ] ) => ( {
...acc,
[ key ]: callback( value ),
} ),
{}
);

const boundSelectors = mapValues(
existingSelectors,
( selector ) =>
Expand Down
1 change: 0 additions & 1 deletion packages/data/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
"equivalent-key-map": "^0.2.2",
"is-plain-object": "^5.0.0",
"is-promise": "^4.0.0",
"lodash": "^4.17.21",
"redux": "^4.1.2",
"turbo-combine-reducers": "^1.0.2",
"use-memo-one": "^1.1.1"
Expand Down
20 changes: 18 additions & 2 deletions packages/data/src/redux-store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* External dependencies
*/
import { createStore, applyMiddleware } from 'redux';
import { get, mapValues } from 'lodash';
import combineReducers from 'turbo-combine-reducers';
import EquivalentKeyMap from 'equivalent-key-map';

Expand Down Expand Up @@ -45,6 +44,23 @@ const trimUndefinedValues = ( array ) => {
return result;
};

/**
* Creates a new object with the same keys, but with `callback()` called as
* a transformer function on each of the values.
*
* @param {Object} obj The object to transform.
* @param {Function} callback The function to transform each object value.
* @return {Array} Transformed object.
*/
const mapValues = ( obj, callback ) =>
Object.entries( obj ?? {} ).reduce(
( acc, [ key, value ] ) => ( {
...acc,
[ key ]: callback( value, key ),
} ),
{}
);

// Convert Map objects to plain objects
const mapToObject = ( key, state ) => {
if ( state instanceof Map ) {
Expand Down Expand Up @@ -608,7 +624,7 @@ function mapResolvers( resolvers, selectors, store, resolversCache ) {
* @param {Array} args Selector Arguments.
*/
async function fulfillResolver( store, resolvers, selectorName, ...args ) {
const resolver = get( resolvers, [ selectorName ] );
const resolver = resolvers[ selectorName ];
if ( ! resolver ) {
return;
}
Expand Down

1 comment on commit 1646f03

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in 1646f03.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/4123071877
📝 Reported issues:

Please sign in to comment.