Skip to content
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 _.uniqBy() #43182

Merged
merged 1 commit into from
Aug 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ module.exports = {
'toString',
'trim',
'truncate',
'uniqBy',
'uniqueId',
'uniqWith',
'values',
Expand Down
21 changes: 12 additions & 9 deletions bin/plugin/commands/changelog.js
Original file line number Diff line number Diff line change
@@ -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' );
Expand Down Expand Up @@ -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 );
}

/**
Expand Down
96 changes: 42 additions & 54 deletions packages/blocks/src/store/reducer.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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 ) =>
Expand Down