Skip to content

Commit 2616c68

Browse files
committed
Make functions idempotent
1 parent 1ad87f8 commit 2616c68

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

src/settings.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ var settings = function() {
6363

6464
this.addCustomFieldFilter = function(fieldName, value) {
6565
var filter = encodeURIComponent(fieldName + '=' + value);
66-
this.settings.customFieldFilters.push(filter);
66+
if (this.settings.customFieldFilters.indexOf(filter) === -1) {
67+
this.settings.customFieldFilters.push(filter);
68+
}
6769
}
6870

6971
this.removeCustomFieldFilter = function(fieldName, value) {
@@ -114,7 +116,9 @@ var settings = function() {
114116
}
115117

116118
this.addFacetField = function(field) {
117-
this.settings.facetFields.push(field);
119+
if (this.settings.facetFields.indexOf(field) === -1) {
120+
this.settings.facetFields.push(field);
121+
}
118122
}
119123

120124
this.setNumberOfFacets = function(numFacets) {

test/settings.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ describe('settings', function() {
5757
assert.equal(JSON.stringify(s.getSettings().customFieldFilters), expect);
5858
});
5959

60+
it('adding custom fields filter should be idempotent', function() {
61+
s.addCustomFieldFilter('city', 'helsinki');
62+
s.addCustomFieldFilter('city', 'helsinki');
63+
var expect = '["city%3Dhelsinki"]';
64+
assert.equal(JSON.stringify(s.getSettings().customFieldFilters), expect);
65+
});
66+
6067

6168
});
6269
});

0 commit comments

Comments
 (0)