Skip to content

Commit 80c6a34

Browse files
committed
Add the utils file and move a few functions into there.
1 parent 6bc3cfc commit 80c6a34

File tree

2 files changed

+46
-23
lines changed

2 files changed

+46
-23
lines changed

src/components/exclude-taxonomies.js

+2-23
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
/**
2-
* WordPress dependencies
3-
*/
41
/**
52
* WordPress dependencies
63
*/
@@ -10,27 +7,9 @@ import { useSelect } from '@wordpress/data';
107
import { store as coreDataStore } from '@wordpress/core-data';
118

129
/**
13-
* A helper to retrieve the correct items to display or save in the token field
14-
*
15-
* @param {Array} subSet
16-
* @param {Array} fullSet
17-
* @param {string} lookupProperty
18-
* @param {string} returnProperty
19-
* @return {Array} The correct items to display or save in the token field
10+
* Internal dependencies
2011
*/
21-
function prepDataFromTokenField(
22-
subSet,
23-
fullSet,
24-
lookupProperty,
25-
returnProperty
26-
) {
27-
const subsetFullObjects = fullSet.filter( ( item ) =>
28-
subSet.includes( item[ lookupProperty ] )
29-
);
30-
return subsetFullObjects.map(
31-
( { [ returnProperty ]: returnVal } ) => returnVal
32-
);
33-
}
12+
import { prepDataFromTokenField } from '../utils';
3413

3514
export const ExcludeTaxonomies = ( { attributes, setAttributes } ) => {
3615
const {

src/utils/index.js

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* Helper function to update taxonomy queries.
3+
*
4+
* @param {Array} queries The current queries.
5+
* @param {string} queryId The query ID to update.
6+
* @param {string} item The key to update.
7+
* @param {string} value The value to update.
8+
*
9+
* @return {Array} The updated queries.
10+
*/
11+
export const updateTaxonomyQuery = ( queries, queryId, item, value ) => {
12+
return queries.map( ( query ) => {
13+
if ( query.id === queryId ) {
14+
return {
15+
...query,
16+
[ item ]: value,
17+
};
18+
}
19+
return query;
20+
} );
21+
};
22+
23+
/**
24+
* A helper to retrieve the correct items to display or save in the token field
25+
*
26+
* @param {Array} subSet
27+
* @param {Array} fullSet
28+
* @param {string} lookupProperty
29+
* @param {string} returnProperty
30+
* @return {Array} The correct items to display or save in the token field
31+
*/
32+
export const prepDataFromTokenField = (
33+
subSet,
34+
fullSet,
35+
lookupProperty,
36+
returnProperty
37+
) => {
38+
const subsetFullObjects = fullSet.filter( ( item ) =>
39+
subSet.includes( item[ lookupProperty ] )
40+
);
41+
return subsetFullObjects.map(
42+
( { [ returnProperty ]: returnVal } ) => returnVal
43+
);
44+
};

0 commit comments

Comments
 (0)