Skip to content

Commit

Permalink
Add the utils file and move a few functions into there.
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanwelcher committed Nov 28, 2024
1 parent 6bc3cfc commit 80c6a34
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 23 deletions.
25 changes: 2 additions & 23 deletions src/components/exclude-taxonomies.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
/**
* WordPress dependencies
*/
/**
* WordPress dependencies
*/
Expand All @@ -10,27 +7,9 @@ import { useSelect } from '@wordpress/data';
import { store as coreDataStore } from '@wordpress/core-data';

/**
* A helper to retrieve the correct items to display or save in the token field
*
* @param {Array} subSet
* @param {Array} fullSet
* @param {string} lookupProperty
* @param {string} returnProperty
* @return {Array} The correct items to display or save in the token field
* Internal dependencies
*/
function prepDataFromTokenField(
subSet,
fullSet,
lookupProperty,
returnProperty
) {
const subsetFullObjects = fullSet.filter( ( item ) =>
subSet.includes( item[ lookupProperty ] )
);
return subsetFullObjects.map(
( { [ returnProperty ]: returnVal } ) => returnVal
);
}
import { prepDataFromTokenField } from '../utils';

export const ExcludeTaxonomies = ( { attributes, setAttributes } ) => {
const {
Expand Down
44 changes: 44 additions & 0 deletions src/utils/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* Helper function to update taxonomy queries.
*
* @param {Array} queries The current queries.
* @param {string} queryId The query ID to update.
* @param {string} item The key to update.
* @param {string} value The value to update.
*
* @return {Array} The updated queries.
*/
export const updateTaxonomyQuery = ( queries, queryId, item, value ) => {
return queries.map( ( query ) => {
if ( query.id === queryId ) {
return {
...query,
[ item ]: value,
};
}
return query;
} );
};

/**
* A helper to retrieve the correct items to display or save in the token field
*
* @param {Array} subSet
* @param {Array} fullSet
* @param {string} lookupProperty
* @param {string} returnProperty
* @return {Array} The correct items to display or save in the token field
*/
export const prepDataFromTokenField = (
subSet,
fullSet,
lookupProperty,
returnProperty
) => {
const subsetFullObjects = fullSet.filter( ( item ) =>
subSet.includes( item[ lookupProperty ] )
);
return subsetFullObjects.map(
( { [ returnProperty ]: returnVal } ) => returnVal
);
};

0 comments on commit 80c6a34

Please sign in to comment.