Skip to content

Commit 2eb0d40

Browse files
committed
Improved category matching with updated regular expression
1 parent f4c5a1e commit 2eb0d40

File tree

1 file changed

+37
-19
lines changed

1 file changed

+37
-19
lines changed

src/js/jquery.storelocator.js

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -960,6 +960,39 @@
960960
}
961961
},
962962

963+
/**
964+
* Run the matching between regular expression filters and string value
965+
*
966+
* @param filter {array} One or multiple filters to apply
967+
* @param val {string} Value to compare
968+
* @param inclusive {boolean} Inclusive (default) or exclusive
969+
*
970+
* @returns {boolean}
971+
*/
972+
filterMatching: function(filter, val, inclusive) {
973+
this.writeDebug('inclusiveFilter',arguments);
974+
inclusive = (typeof inclusive !== 'undefined') ? inclusive : true;
975+
var applyFilters;
976+
977+
// Undefined check.
978+
if (typeof val === 'undefined') {
979+
return false;
980+
}
981+
982+
// Modify the join depending on inclusive (AND) vs exclusive (OR).
983+
if ( true === inclusive ) {
984+
applyFilters = filter.join('');
985+
} else {
986+
applyFilters = filter.join('|');
987+
}
988+
989+
if ((new RegExp(applyFilters, 'i').test(val.replace(/([.*+?^=!:${}()|\[\]\/\\]|&\s+)/g, '')))) {
990+
return true;
991+
}
992+
993+
return false;
994+
},
995+
963996
/**
964997
* Filter the data with Regex
965998
*
@@ -969,38 +1002,23 @@
9691002
*/
9701003
filterData: function (data, filters) {
9711004
this.writeDebug('filterData',arguments);
972-
var filterTest = true;
1005+
var filterTest = false;
9731006

9741007
for (var k in filters) {
9751008
if (filters.hasOwnProperty(k)) {
9761009

9771010
// Exclusive filtering
9781011
if (this.settings.exclusiveFiltering === true || (this.settings.exclusiveTax !== null && Array.isArray(this.settings.exclusiveTax) && this.settings.exclusiveTax.indexOf(k) !== -1)) {
979-
var filterTests = filters[k];
980-
var exclusiveTest = [];
981-
982-
if (typeof data[k] !== 'undefined') {
983-
for (var l = 0; l < filterTests.length; l++) {
984-
exclusiveTest[l] = new RegExp(filterTests[l], 'i').test(data[k].replace(/([.*+?^=!:${}()|\[\]\/\\]|&\s+)/g, ''));
985-
}
986-
}
987-
988-
if (exclusiveTest.indexOf(true) === -1) {
989-
filterTest = false;
990-
}
1012+
filterTest = this.filterMatching(filters[k], data[k], false );
9911013
}
9921014
// Inclusive filtering
9931015
else {
994-
if (typeof data[k] === 'undefined' || !(new RegExp(filters[k].join(''), 'i').test(data[k].replace(/([.*+?^=!:${}()|\[\]\/\\]|&\s+)/g, '')))) {
995-
filterTest = false;
996-
}
1016+
filterTest = this.filterMatching(filters[k], data[k]);
9971017
}
9981018
}
9991019
}
10001020

1001-
if (filterTest) {
1002-
return true;
1003-
}
1021+
return filterTest;
10041022
},
10051023

10061024
/**

0 commit comments

Comments
 (0)