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 _.castArray() #45098

Merged
merged 1 commit into from
Oct 20, 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 @@ -81,6 +81,7 @@ module.exports = {
importNames: [
'camelCase',
'capitalize',
'castArray',
'chunk',
'clamp',
'cloneDeep',
Expand Down
5 changes: 3 additions & 2 deletions packages/block-editor/src/components/block-compare/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* External dependencies
*/
import classnames from 'classnames';
import { castArray } from 'lodash';
// diff doesn't tree-shake correctly, so we import from the individual
// module here, to avoid including too much of the library
import { diffChars } from 'diff/lib/diff/character';
Expand Down Expand Up @@ -44,7 +43,9 @@ function BlockCompare( {

function getConvertedContent( convertedBlock ) {
// The convertor may return an array of items or a single item.
const newBlocks = castArray( convertedBlock );
const newBlocks = Array.isArray( convertedBlock )
? convertedBlock
: [ convertedBlock ];

// Get converted block details.
const newContent = newBlocks.map( ( item ) =>
Expand Down
11 changes: 8 additions & 3 deletions packages/block-editor/src/components/block-preview/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/**
* External dependencies
*/
import { castArray } from 'lodash';
import classnames from 'classnames';

/**
Expand Down Expand Up @@ -36,7 +35,10 @@ export function BlockPreview( {
() => ( { ...originalSettings, __unstableIsPreviewMode: true } ),
[ originalSettings ]
);
const renderedBlocks = useMemo( () => castArray( blocks ), [ blocks ] );
const renderedBlocks = useMemo(
() => ( Array.isArray( blocks ) ? blocks : [ blocks ] ),
[ blocks ]
);
if ( ! blocks || blocks.length === 0 ) {
return null;
}
Expand Down Expand Up @@ -99,7 +101,10 @@ export function useBlockPreview( {
);
const disabledRef = useDisabled();
const ref = useMergeRefs( [ props.ref, disabledRef ] );
const renderedBlocks = useMemo( () => castArray( blocks ), [ blocks ] );
const renderedBlocks = useMemo(
() => ( Array.isArray( blocks ) ? blocks : [ blocks ] ),
[ blocks ]
);

const children = (
<BlockEditorProvider value={ renderedBlocks } settings={ settings }>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* External dependencies
*/
import { castArray } from 'lodash';

/**
* WordPress dependencies
*/
Expand Down Expand Up @@ -58,7 +53,9 @@ export function BlockSettingsDropdown( {
__unstableDisplayLocation,
...props
} ) {
const blockClientIds = castArray( clientIds );
const blockClientIds = Array.isArray( clientIds )
? clientIds
: [ clientIds ];
const count = blockClientIds.length;
const firstBlockClientId = blockClientIds[ 0 ];
const {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* External dependencies
*/
import { castArray } from 'lodash';

/**
* WordPress dependencies
*/
Expand Down Expand Up @@ -52,7 +47,7 @@ export const BlockSwitcherDropdownMenu = ( { clientIds, blocks } ) => {
const { getBlockStyles, getBlockType } = select( blocksStore );
const { canRemoveBlocks } = select( blockEditorStore );
const rootClientId = getBlockRootClientId(
castArray( clientIds )[ 0 ]
Array.isArray( clientIds ) ? clientIds[ 0 ] : clientIds
);
const [ { name: firstBlockName } ] = blocks;
const _isSingleBlockSelected = blocks.length === 1;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* External dependencies
*/
import { castArray } from 'lodash';

/**
* WordPress dependencies
*/
Expand Down Expand Up @@ -118,14 +113,11 @@ function useInsertionPoint( {
meta
);
}
const blockLength = Array.isArray( blocks ) ? blocks.length : 1;
const message = sprintf(
// translators: %d: the name of the block that has been added
_n(
'%d block added.',
'%d blocks added.',
castArray( blocks ).length
),
castArray( blocks ).length
_n( '%d block added.', '%d blocks added.', blockLength ),
blockLength
);
speak( message );

Expand Down
7 changes: 1 addition & 6 deletions packages/block-editor/src/store/array.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* External dependencies
*/
import { castArray } from 'lodash';

/**
* Insert one or multiple elements into a given position of an array.
*
Expand All @@ -15,7 +10,7 @@ import { castArray } from 'lodash';
export function insertAt( array, elements, index ) {
return [
...array.slice( 0, index ),
...castArray( elements ),
...( Array.isArray( elements ) ? elements : [ elements ] ),
...array.slice( index ),
];
}
Expand Down
11 changes: 6 additions & 5 deletions packages/block-editor/src/store/selectors.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { castArray, map, reduce, some, find, filter, orderBy } from 'lodash';
import { map, reduce, some, find, filter, orderBy } from 'lodash';
import createSelector from 'rememo';

/**
Expand Down Expand Up @@ -319,12 +319,13 @@ export const __experimentalGetGlobalBlocksByName = createSelector(
*/
export const getBlocksByClientId = createSelector(
( state, clientIds ) =>
map( castArray( clientIds ), ( clientId ) =>
getBlock( state, clientId )
map(
Array.isArray( clientIds ) ? clientIds : [ clientIds ],
( clientId ) => getBlock( state, clientId )
),
( state, clientIds ) =>
map(
castArray( clientIds ),
Array.isArray( clientIds ) ? clientIds : [ clientIds ],
( clientId ) => state.blocks.tree[ clientId ]
)
);
Expand Down Expand Up @@ -2065,7 +2066,7 @@ export const getInserterItems = createSelector(
*/
export const getBlockTransformItems = createSelector(
( state, blocks, rootClientId = null ) => {
const normalizedBlocks = castArray( blocks );
const normalizedBlocks = Array.isArray( blocks ) ? blocks : [ blocks ];
const [ sourceBlock ] = normalizedBlocks;
const buildBlockTypeTransformItem = buildBlockTypeItem( state, {
buildScope: 'transform',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* External dependencies
*/
import { castArray } from 'lodash';

/**
* WordPress dependencies
*/
Expand Down Expand Up @@ -61,7 +56,9 @@ export default function useOutdentListItem( clientId ) {
return [
canOutdent,
useCallback( ( clientIds = getSelectedBlockClientIds() ) => {
clientIds = castArray( clientIds );
if ( ! Array.isArray( clientIds ) ) {
clientIds = [ clientIds ];
}

if ( ! clientIds.length ) return;

Expand Down
9 changes: 3 additions & 6 deletions packages/blocks/src/api/children.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* External dependencies
*/
import { castArray } from 'lodash';

/**
* WordPress dependencies
*/
Expand Down Expand Up @@ -75,7 +70,9 @@ export function concat( ...blockNodes ) {

const result = [];
for ( let i = 0; i < blockNodes.length; i++ ) {
const blockNode = castArray( blockNodes[ i ] );
const blockNode = Array.isArray( blockNodes[ i ] )
? blockNodes[ i ]
: [ blockNodes[ i ] ];
for ( let j = 0; j < blockNode.length; j++ ) {
const child = blockNode[ j ];
const canConcatToPreviousString =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* External dependencies
*/
import { castArray } from 'lodash';

/**
* Internal dependencies
*/
Expand Down Expand Up @@ -102,10 +97,15 @@ export function applyBlockDeprecatedVersions( block, rawBlock, blockType ) {
// inner blocks.
const { migrate } = deprecatedBlockType;
if ( migrate ) {
let migrated = migrate( migratedAttributes, block.innerBlocks );
if ( ! Array.isArray( migrated ) ) {
migrated = [ migrated ];
}

[
migratedAttributes = parsedAttributes,
migratedInnerBlocks = block.innerBlocks,
] = castArray( migrate( migratedAttributes, block.innerBlocks ) );
] = migrated;
}

block = {
Expand Down
7 changes: 5 additions & 2 deletions packages/blocks/src/api/parser/get-block-attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* External dependencies
*/
import { parse as hpqParse } from 'hpq';
import { mapValues, castArray } from 'lodash';
import { mapValues } from 'lodash';
import memoize from 'memize';

/**
Expand Down Expand Up @@ -165,7 +165,10 @@ export function getBlockAttribute(
* @return {boolean} Whether value is valid.
*/
export function isValidByType( value, type ) {
return type === undefined || isOfTypes( value, castArray( type ) );
return (
type === undefined ||
isOfTypes( value, Array.isArray( type ) ? type : [ type ] )
);
}

/**
Expand Down
16 changes: 9 additions & 7 deletions packages/blocks/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* External dependencies
*/
import { isPlainObject } from 'is-plain-object';
import { castArray, pick, some } from 'lodash';
import { pick, some } from 'lodash';

/**
* WordPress dependencies
Expand Down Expand Up @@ -163,7 +163,7 @@ const processBlockType = ( blockType, { select } ) => {
export function addBlockTypes( blockTypes ) {
return {
type: 'ADD_BLOCK_TYPES',
blockTypes: castArray( blockTypes ),
blockTypes: Array.isArray( blockTypes ) ? blockTypes : [ blockTypes ],
};
}

Expand Down Expand Up @@ -242,7 +242,7 @@ export const __experimentalReapplyBlockTypeFilters =
export function removeBlockTypes( names ) {
return {
type: 'REMOVE_BLOCK_TYPES',
names: castArray( names ),
names: Array.isArray( names ) ? names : [ names ],
};
}

Expand All @@ -260,7 +260,7 @@ export function removeBlockTypes( names ) {
export function addBlockStyles( blockName, styles ) {
return {
type: 'ADD_BLOCK_STYLES',
styles: castArray( styles ),
styles: Array.isArray( styles ) ? styles : [ styles ],
blockName,
};
}
Expand All @@ -279,7 +279,7 @@ export function addBlockStyles( blockName, styles ) {
export function removeBlockStyles( blockName, styleNames ) {
return {
type: 'REMOVE_BLOCK_STYLES',
styleNames: castArray( styleNames ),
styleNames: Array.isArray( styleNames ) ? styleNames : [ styleNames ],
blockName,
};
}
Expand All @@ -298,7 +298,7 @@ export function removeBlockStyles( blockName, styleNames ) {
export function addBlockVariations( blockName, variations ) {
return {
type: 'ADD_BLOCK_VARIATIONS',
variations: castArray( variations ),
variations: Array.isArray( variations ) ? variations : [ variations ],
blockName,
};
}
Expand All @@ -317,7 +317,9 @@ export function addBlockVariations( blockName, variations ) {
export function removeBlockVariations( blockName, variationNames ) {
return {
type: 'REMOVE_BLOCK_VARIATIONS',
variationNames: castArray( variationNames ),
variationNames: Array.isArray( variationNames )
? variationNames
: [ variationNames ],
blockName,
};
}
Expand Down
13 changes: 8 additions & 5 deletions packages/core-data/src/actions.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { castArray, isEqual, find } from 'lodash';
import { isEqual, find } from 'lodash';
import { v4 as uuid } from 'uuid';

/**
Expand Down Expand Up @@ -33,7 +33,7 @@ import { STORE_NAME } from './name';
export function receiveUserQuery( queryID, users ) {
return {
type: 'RECEIVE_USER_QUERY',
users: castArray( users ),
users: Array.isArray( users ) ? users : [ users ],
queryID,
};
}
Expand Down Expand Up @@ -91,8 +91,11 @@ export function receiveEntityRecords(
// Auto drafts should not have titles, but some plugins rely on them so we can't filter this
// on the server.
if ( kind === 'postType' ) {
records = castArray( records ).map( ( record ) =>
record.status === 'auto-draft' ? { ...record, title: '' } : record
records = ( Array.isArray( records ) ? records : [ records ] ).map(
( record ) =>
record.status === 'auto-draft'
? { ...record, title: '' }
: record
);
}
let action;
Expand Down Expand Up @@ -820,6 +823,6 @@ export function receiveAutosaves( postId, autosaves ) {
return {
type: 'RECEIVE_AUTOSAVES',
postId,
autosaves: castArray( autosaves ),
autosaves: Array.isArray( autosaves ) ? autosaves : [ autosaves ],
};
}
9 changes: 2 additions & 7 deletions packages/core-data/src/queried-data/actions.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* External dependencies
*/
import { castArray } from 'lodash';

/**
* Returns an action object used in signalling that items have been received.
*
Expand All @@ -14,7 +9,7 @@ import { castArray } from 'lodash';
export function receiveItems( items, edits ) {
return {
type: 'RECEIVE_ITEMS',
items: castArray( items ),
items: Array.isArray( items ) ? items : [ items ],
persistedEdits: edits,
};
}
Expand All @@ -32,7 +27,7 @@ export function receiveItems( items, edits ) {
export function removeItems( kind, name, records, invalidateCache = false ) {
return {
type: 'REMOVE_ITEMS',
itemIds: castArray( records ),
itemIds: Array.isArray( records ) ? records : [ records ],
kind,
name,
invalidateCache,
Expand Down
Loading