Skip to content

Commit

Permalink
fix: use loose type comparison for list__in and list__not_in (#1326)
Browse files Browse the repository at this point in the history
  • Loading branch information
dkoo committed Jul 12, 2024
1 parent e4825e9 commit 1872c96
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
23 changes: 23 additions & 0 deletions src/criteria/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,18 @@ describe( 'criteria matching', () => {
expect( criteria.matches( { value: '' } ) ).toEqual( false );
expect( criteria.matches( { value: [] } ) ).toEqual( false );
} );
it( 'should match "list__in" matching function with loose type comparison', () => {
setMatchingAttribute( criteriaId, () => 2 );
setMatchingFunction( criteriaId, 'list__in' );
const criteria = getCriteria( criteriaId );
expect( criteria.matches( { value: [ '1', '2' ] } ) ).toEqual( true );
expect( criteria.matches( { value: [ 1, 2 ] } ) ).toEqual( true );
expect( criteria.matches( { value: '1, 2' } ) ).toEqual( true );
expect( criteria.matches( { value: '2' } ) ).toEqual( true );
expect( criteria.matches( { value: [ 1 ] } ) ).toEqual( false );
expect( criteria.matches( { value: '1' } ) ).toEqual( false );
expect( criteria.matches( { value: 1 } ) ).toEqual( false );
} );
it( 'should match "list__not_in" matching function', () => {
setMatchingAttribute( criteriaId, () => 'bar' );
setMatchingFunction( criteriaId, 'list__not_in' );
Expand All @@ -128,6 +140,17 @@ describe( 'criteria matching', () => {
expect( criteria.matches( { value: '' } ) ).toEqual( true );
expect( criteria.matches( { value: [] } ) ).toEqual( true );
} );
it( 'should match "list__not_in" matching function with loose type comparison', () => {
setMatchingAttribute( criteriaId, () => 2 );
setMatchingFunction( criteriaId, 'list__not_in' );
const criteria = getCriteria( criteriaId );
expect( criteria.matches( { value: [ '1', '2' ] } ) ).toEqual( false );
expect( criteria.matches( { value: [ 1, 2 ] } ) ).toEqual( false );
expect( criteria.matches( { value: '2' } ) ).toEqual( false );
expect( criteria.matches( { value: [ 1 ] } ) ).toEqual( true );
expect( criteria.matches( { value: '1' } ) ).toEqual( true );
expect( criteria.matches( { value: 1 } ) ).toEqual( true );
} );
it( 'should match custom matching function', () => {
setMatchingFunction( criteriaId, segmentConfig => {
return segmentConfig.value === 'foo';
Expand Down
10 changes: 6 additions & 4 deletions src/criteria/matching-functions.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-disable eqeqeq */

/**
* Common matching functions that can be used by criteria.
*/
Expand All @@ -22,9 +24,9 @@ export default {
return false;
}
if ( Array.isArray( criteria.value ) ) {
return criteria.value.some( value => list.includes( value ) );
return criteria.value.some( value => list.some( configValue => configValue == value ) );
}
if ( ! criteria.value || ! list.includes( criteria.value ) ) {
if ( ! criteria.value || ! list.some( configValue => configValue == criteria.value ) ) {
return false;
}
return true;
Expand All @@ -45,9 +47,9 @@ export default {
return true;
}
if ( Array.isArray( criteria.value ) ) {
return ! criteria.value.some( value => list.includes( value ) );
return ! criteria.value.some( value => list.some( configValue => configValue == value ) );
}
if ( ! criteria.value || ! list.includes( criteria.value ) ) {
if ( ! criteria.value || ! list.some( configValue => configValue == criteria.value ) ) {
return true;
}
return false;
Expand Down

0 comments on commit 1872c96

Please sign in to comment.