Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
Change applyCheckoutFilter signature
Browse files Browse the repository at this point in the history
  • Loading branch information
Aljullu committed Feb 8, 2021
1 parent 9dc1690 commit 73cb8e4
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ const TotalsFooterItem = ( { currency, values } ) => {
const label = __experimentalApplyCheckoutFilter( {
filterName: 'totalLabel',
defaultValue: __( 'Total', 'woo-gutenberg-products-block' ),
args: {
arg: {
extensions,
},
// Only accept strings.
validate: ( value ) => typeof value === 'string',
validation: ( value ) => typeof value === 'string',
} );

return (
Expand Down
12 changes: 6 additions & 6 deletions packages/checkout/registry/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,26 +35,26 @@ const getCheckoutFilters = ( filterName ) => {
* @param {Object} o Object of arguments.
* @param {string} o.filterName Name of the filter to apply.
* @param {any} o.defaultValue Default value to filter.
* @param {any} [o.args] Argument to pass to registered functions. If
* @param {any} [o.arg] Argument to pass to registered functions. If
* several arguments need to be passed, use an
* object.
* @param {Function} [o.validate] Function that needs to return true when the
* @param {Function} [o.validation] Function that needs to return true when the
* filtered value is passed in order for the
* filter to be applied.
* @return {any} Filtered value.
*/
export const __experimentalApplyCheckoutFilter = ( {
filterName,
defaultValue,
args = {},
validate = () => true,
arg = null,
validation = () => true,
} ) => {
const filters = getCheckoutFilters( filterName );
let value = defaultValue;
filters.forEach( ( filter ) => {
try {
const newValue = filter( value, args );
value = validate( newValue ) ? newValue : value;
const newValue = filter( value, arg );
value = validation( newValue ) ? newValue : value;
} catch ( e ) {
// eslint-disable-next-line no-console
console.log( e );
Expand Down
4 changes: 2 additions & 2 deletions packages/checkout/registry/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe( 'Checkout registry', () => {
const newValue = __experimentalApplyCheckoutFilter( {
filterName,
defaultValue: value,
args: {
arg: {
punctuationSign: '!',
},
} );
Expand All @@ -44,7 +44,7 @@ describe( 'Checkout registry', () => {
const newValue = __experimentalApplyCheckoutFilter( {
filterName,
defaultValue: value,
validate: ( val ) => ! val.includes( 'HELLO' ),
validation: ( val ) => ! val.includes( 'HELLO' ),
} );

expect( newValue ).toBe( value );
Expand Down

0 comments on commit 73cb8e4

Please sign in to comment.