Skip to content

Commit

Permalink
Lodash: Remove from blocks package
Browse files Browse the repository at this point in the history
  • Loading branch information
tyxla committed Jun 20, 2023
1 parent 115e7b1 commit 4f3e418
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 7 deletions.
1 change: 0 additions & 1 deletion package-lock.json

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

1 change: 0 additions & 1 deletion packages/blocks/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
"fast-deep-equal": "^3.1.3",
"hpq": "^1.3.0",
"is-plain-object": "^5.0.0",
"lodash": "^4.17.21",
"memize": "^2.1.0",
"rememo": "^4.0.2",
"remove-accents": "^0.4.2",
Expand Down
6 changes: 3 additions & 3 deletions packages/blocks/src/store/private-selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
* External dependencies
*/
import createSelector from 'rememo';
import { get } from 'lodash';

/**
* Internal dependencies
*/
import { getBlockType } from './selectors';
import { getValueFromObjectPath } from './utils';
import { __EXPERIMENTAL_STYLE_PROPERTY as STYLE_PROPERTY } from '../api/constants';

const ROOT_BLOCK_SUPPORTS = [
Expand Down Expand Up @@ -135,7 +135,7 @@ export const getSupportedStyles = createSelector(
if (
STYLE_PROPERTY[ styleName ].support[ 0 ] in
blockType.supports &&
get(
getValueFromObjectPath(
blockType.supports,
STYLE_PROPERTY[ styleName ].support
) !== false
Expand All @@ -146,7 +146,7 @@ export const getSupportedStyles = createSelector(
}

if (
get(
getValueFromObjectPath(
blockType.supports,
STYLE_PROPERTY[ styleName ].support,
false
Expand Down
12 changes: 10 additions & 2 deletions packages/blocks/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@
*/
import createSelector from 'rememo';
import removeAccents from 'remove-accents';
import { get } from 'lodash';

/**
* WordPress dependencies
*/
import { pipe } from '@wordpress/compose';

/**
* Internal dependencies
*/
import { getValueFromObjectPath } from './utils';

/** @typedef {import('../api/registration').WPBlockVariation} WPBlockVariation */
/** @typedef {import('../api/registration').WPBlockVariationScope} WPBlockVariationScope */
/** @typedef {import('./reducer').WPBlockCategory} WPBlockCategory */
Expand Down Expand Up @@ -607,7 +611,11 @@ export const getBlockSupport = (
return defaultSupports;
}

return get( blockType.supports, feature, defaultSupports );
return getValueFromObjectPath(
blockType.supports,
feature,
defaultSupports
);
};

/**
Expand Down
20 changes: 20 additions & 0 deletions packages/blocks/src/store/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Helper util to return a value from a certain path of the object.
* Path is specified as either:
* - a string of properties, separated by dots, for example: "x.y".
* - an array of properties, for example `[ 'x', 'y' ]`.
* You can also specify a default value in case the result is nullish.
*
* @param {Object} object Input object.
* @param {string|Array} path Path to the object property.
* @param {*} defaultValue Default value if the value at the specified path is nullish.
* @return {*} Value of the object property at the specified path.
*/
export const getValueFromObjectPath = ( object, path, defaultValue ) => {
const normalizedPath = Array.isArray( path ) ? path : path.split( '.' );
let value = object;
normalizedPath.forEach( ( fieldName ) => {
value = value?.[ fieldName ];
} );
return value ?? defaultValue;
};

0 comments on commit 4f3e418

Please sign in to comment.