From a24396654c47f665e7cde680ec5bf0b600dd8dc6 Mon Sep 17 00:00:00 2001 From: Marin Atanasov <8436925+tyxla@users.noreply.github.com> Date: Wed, 22 Jun 2022 11:27:06 +0300 Subject: [PATCH] Lodash: Remove completely from annotations package (#41856) --- package-lock.json | 1 - packages/annotations/package.json | 1 - packages/annotations/src/store/reducer.js | 22 ++++++++++++++++----- packages/annotations/src/store/selectors.js | 5 +---- 4 files changed, 18 insertions(+), 11 deletions(-) diff --git a/package-lock.json b/package-lock.json index d1a4cda22a4971..b8de200d80336a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -16398,7 +16398,6 @@ "@wordpress/hooks": "file:packages/hooks", "@wordpress/i18n": "file:packages/i18n", "@wordpress/rich-text": "file:packages/rich-text", - "lodash": "^4.17.21", "rememo": "^4.0.0", "uuid": "^8.3.0" } diff --git a/packages/annotations/package.json b/packages/annotations/package.json index 1c3da2845dfd81..4e5048e9e11ad4 100644 --- a/packages/annotations/package.json +++ b/packages/annotations/package.json @@ -30,7 +30,6 @@ "@wordpress/hooks": "file:../hooks", "@wordpress/i18n": "file:../i18n", "@wordpress/rich-text": "file:../rich-text", - "lodash": "^4.17.21", "rememo": "^4.0.0", "uuid": "^8.3.0" }, diff --git a/packages/annotations/src/store/reducer.js b/packages/annotations/src/store/reducer.js index 9209fa6f2230d8..dd111c2d61cbee 100644 --- a/packages/annotations/src/store/reducer.js +++ b/packages/annotations/src/store/reducer.js @@ -1,8 +1,3 @@ -/** - * External dependencies - */ -import { mapValues } from 'lodash'; - /** * Filters an array based on the predicate, but keeps the reference the same if * the array hasn't changed. @@ -20,6 +15,23 @@ function filterWithReference( collection, predicate ) { : filteredCollection; } +/** + * 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 ), + } ), + {} + ); + /** * Verifies whether the given annotations is a valid annotation. * diff --git a/packages/annotations/src/store/selectors.js b/packages/annotations/src/store/selectors.js index 299d637f41c1e1..8ec24e0541e6fa 100644 --- a/packages/annotations/src/store/selectors.js +++ b/packages/annotations/src/store/selectors.js @@ -2,7 +2,6 @@ * External dependencies */ import createSelector from 'rememo'; -import { flatMap } from 'lodash'; /** * Shared reference to an empty array for cases where it is important to avoid @@ -79,7 +78,5 @@ export const __experimentalGetAnnotationsForRichText = createSelector( * @return {Array} All annotations currently applied. */ export function __experimentalGetAnnotations( state ) { - return flatMap( state, ( annotations ) => { - return annotations; - } ); + return Object.values( state ).flat(); }