From 0ca91cd9a6fd86d797db392e3434797ddc86455b Mon Sep 17 00:00:00 2001 From: Marin Atanasov Date: Fri, 12 Aug 2022 14:21:30 +0300 Subject: [PATCH] Lodash: Refactor away from _.uniqBy() --- .eslintrc.js | 1 + bin/plugin/commands/changelog.js | 21 ++-- packages/blocks/src/store/reducer.js | 96 ++++++++----------- .../post-taxonomies/flat-term-selector.js | 12 ++- 4 files changed, 65 insertions(+), 65 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index b5af773920cf9..03ba0dd71c87c 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -140,6 +140,7 @@ module.exports = { 'toString', 'trim', 'truncate', + 'uniqBy', 'uniqueId', 'uniqWith', 'values', diff --git a/bin/plugin/commands/changelog.js b/bin/plugin/commands/changelog.js index 382cbbc0bc42e..6e189352201ef 100644 --- a/bin/plugin/commands/changelog.js +++ b/bin/plugin/commands/changelog.js @@ -1,14 +1,7 @@ /** * External dependencies */ -const { - groupBy, - escapeRegExp, - uniq, - flow, - sortBy, - uniqBy, -} = require( 'lodash' ); +const { groupBy, escapeRegExp, uniq, flow, sortBy } = require( 'lodash' ); const Octokit = require( '@octokit/rest' ); const { sprintf } = require( 'sprintf-js' ); const semver = require( 'semver' ); @@ -838,7 +831,17 @@ function sortByUsername( items ) { * @return {IssuesListForRepoResponseItem[]} The list of pull requests unique per user. */ function getUniqueByUsername( items ) { - return uniqBy( items, ( item ) => item.user.login ); + /** + * @type {IssuesListForRepoResponseItem[]} List of pull requests. + */ + const EMPTY_PR_LIST = []; + + return items.reduce( ( acc, item ) => { + if ( ! acc.some( ( i ) => i.user.login === item.user.login ) ) { + acc.push( item ); + } + return acc; + }, EMPTY_PR_LIST ); } /** diff --git a/packages/blocks/src/store/reducer.js b/packages/blocks/src/store/reducer.js index c9582926318e0..b0f5167c12fdb 100644 --- a/packages/blocks/src/store/reducer.js +++ b/packages/blocks/src/store/reducer.js @@ -1,16 +1,7 @@ /** * External dependencies */ -import { - filter, - find, - get, - isEmpty, - map, - mapValues, - omit, - uniqBy, -} from 'lodash'; +import { filter, find, get, isEmpty, map, mapValues, omit } from 'lodash'; /** * WordPress dependencies @@ -51,6 +42,16 @@ function keyBlockTypesByName( types ) { ); } +// Filter items to ensure they're unique by their name. +function getUniqueItemsByName( items ) { + return items.reduce( ( acc, currentItem ) => { + if ( ! acc.some( ( item ) => item.name === currentItem.name ) ) { + acc.push( currentItem ); + } + return acc; + }, [] ); +} + /** * Reducer managing the unprocessed block types in a form passed when registering the by block. * It's for internal use only. It allows recomputing the processed block types on-demand after block type filters @@ -113,34 +114,27 @@ export function blockStyles( state = {}, action ) { ...state, ...mapValues( keyBlockTypesByName( action.blockTypes ), - ( blockType ) => { - return uniqBy( - [ - ...get( blockType, [ 'styles' ], [] ).map( - ( style ) => ( { - ...style, - source: 'block', - } ) - ), - ...get( state, [ blockType.name ], [] ).filter( - ( { source } ) => 'block' !== source - ), - ], - ( style ) => style.name - ); - } + ( blockType ) => + getUniqueItemsByName( [ + ...get( blockType, [ 'styles' ], [] ).map( + ( style ) => ( { + ...style, + source: 'block', + } ) + ), + ...get( state, [ blockType.name ], [] ).filter( + ( { source } ) => 'block' !== source + ), + ] ) ), }; case 'ADD_BLOCK_STYLES': return { ...state, - [ action.blockName ]: uniqBy( - [ - ...get( state, [ action.blockName ], [] ), - ...action.styles, - ], - ( style ) => style.name - ), + [ action.blockName ]: getUniqueItemsByName( [ + ...get( state, [ action.blockName ], [] ), + ...action.styles, + ] ), }; case 'REMOVE_BLOCK_STYLES': return { @@ -171,33 +165,27 @@ export function blockVariations( state = {}, action ) { ...mapValues( keyBlockTypesByName( action.blockTypes ), ( blockType ) => { - return uniqBy( - [ - ...get( blockType, [ 'variations' ], [] ).map( - ( variation ) => ( { - ...variation, - source: 'block', - } ) - ), - ...get( state, [ blockType.name ], [] ).filter( - ( { source } ) => 'block' !== source - ), - ], - ( variation ) => variation.name - ); + return getUniqueItemsByName( [ + ...get( blockType, [ 'variations' ], [] ).map( + ( variation ) => ( { + ...variation, + source: 'block', + } ) + ), + ...get( state, [ blockType.name ], [] ).filter( + ( { source } ) => 'block' !== source + ), + ] ); } ), }; case 'ADD_BLOCK_VARIATIONS': return { ...state, - [ action.blockName ]: uniqBy( - [ - ...get( state, [ action.blockName ], [] ), - ...action.variations, - ], - ( variation ) => variation.name - ), + [ action.blockName ]: getUniqueItemsByName( [ + ...get( state, [ action.blockName ], [] ), + ...action.variations, + ] ), }; case 'REMOVE_BLOCK_VARIATIONS': return { diff --git a/packages/editor/src/components/post-taxonomies/flat-term-selector.js b/packages/editor/src/components/post-taxonomies/flat-term-selector.js index af8e546ea7b3a..d1d46b2ac03a3 100644 --- a/packages/editor/src/components/post-taxonomies/flat-term-selector.js +++ b/packages/editor/src/components/post-taxonomies/flat-term-selector.js @@ -1,7 +1,7 @@ /** * External dependencies */ -import { escape as escapeString, find, get, uniqBy } from 'lodash'; +import { escape as escapeString, find, get } from 'lodash'; /** * WordPress dependencies @@ -202,7 +202,15 @@ export function FlatTermSelector( { slug } ) { ...( terms ?? [] ), ...( searchResults ?? [] ), ]; - const uniqueTerms = uniqBy( termNames, ( term ) => term.toLowerCase() ); + const uniqueTerms = termNames.reduce( ( acc, name ) => { + if ( + ! acc.some( ( n ) => n.toLowerCase() === name.toLowerCase() ) + ) { + acc.push( name ); + } + return acc; + }, [] ); + const newTermNames = uniqueTerms.filter( ( termName ) => ! find( availableTerms, ( term ) =>