Skip to content

Commit

Permalink
move to a reduce function
Browse files Browse the repository at this point in the history
  • Loading branch information
scruffian committed May 2, 2024
1 parent 3bf4405 commit 4b398ae
Showing 1 changed file with 12 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -153,20 +153,17 @@ export default function useThemeStyleVariationsByProperty( {
? cloneDeep( baseVariation )
: null;

let processedStyleVariations = variations
// Remove variations that are empty once the property is filtered out.
.filter( ( variation ) => {
const variationFilteredByProperty = filterObjectByProperty(
cloneDeep( variation ),
property
);
return Object.keys( variationFilteredByProperty ).length > 0;
} )
.map( ( variation ) => {
let processedStyleVariations = variations.reduce(
( accumulator, variation ) => {
const variationFilteredByProperty = filterObjectByProperty(
cloneDeep( variation ),
property
);
// Remove variations that are empty once the property is filtered out.
if ( Object.keys( variationFilteredByProperty ).length === 0 ) {
return accumulator;
}

let result = {
...variationFilteredByProperty,
title: variation?.title,
Expand All @@ -183,8 +180,11 @@ export default function useThemeStyleVariationsByProperty( {
result
);
}
return result;
} );
accumulator.push( result );
return accumulator;
},
[] // Initial accumulator value.
);

if ( 'function' === typeof filter ) {
processedStyleVariations =
Expand Down

0 comments on commit 4b398ae

Please sign in to comment.