Skip to content

Commit 73f0a32

Browse files
committed
Accept boolean conjunction in runtime facets (bump 2.4.3)
1 parent 22bd120 commit 73f0a32

File tree

4 files changed

+50
-6
lines changed

4 files changed

+50
-6
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ const result = itemsjs.search({
241241
tags: {
242242
selected: ['1980s', 'historical'],
243243
options: {
244-
conjunction: 'OR', // AND/OR for this facet only
244+
conjunction: 'OR', // AND/OR for this facet only (also accepts boolean true/false)
245245
size: 30, // how many buckets to return
246246
sortBy: 'count', // 'count' | 'key'
247247
sortDir: 'desc', // 'asc' | 'desc'

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "itemsjs",
3-
"version": "2.4.2",
3+
"version": "2.4.3",
44
"description": "Created to perform fast search on small json dataset (up to 1000 elements).",
55
"type": "module",
66
"scripts": {

src/utils/config.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,16 @@ export const parse_boolean_query = function (query) {
6868
});
6969
};
7070

71+
const normalizeConjunction = (value) => {
72+
if (typeof value === 'boolean') {
73+
return value ? 'AND' : 'OR';
74+
}
75+
if (typeof value === 'string') {
76+
return value.toUpperCase() === 'OR' ? 'OR' : 'AND';
77+
}
78+
return 'AND';
79+
};
80+
7181
/**
7282
* Builds a boolean query string from runtime facet selections.
7383
* Respects per-facet conjunction (AND/OR). Unknown facets are ignored.
@@ -90,8 +100,9 @@ export const buildFiltersQueryFromFacets = function (facets, configuration) {
90100
return;
91101
}
92102

93-
const conjunction =
94-
facets[facetName]?.options?.conjunction === 'OR' ? 'OR' : 'AND';
103+
const conjunction = normalizeConjunction(
104+
facets[facetName]?.options?.conjunction,
105+
);
95106

96107
const parts = selected.map((val) => {
97108
const stringVal = String(val);
@@ -144,8 +155,9 @@ export const normalizeRuntimeFacetConfig = function (facets, configuration) {
144155
if (options) {
145156
const mapped = {};
146157

147-
if (options.conjunction) {
148-
mapped.conjunction = options.conjunction !== 'OR';
158+
if (options.conjunction !== undefined) {
159+
const conj = normalizeConjunction(options.conjunction);
160+
mapped.conjunction = conj === 'AND';
149161
}
150162

151163
if (typeof options.size === 'number') {

tests/searchSpec.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,38 @@ describe('search', function () {
351351
done();
352352
});
353353

354+
it('accepts boolean conjunction in runtime facets (false => OR, true => AND)', function test(done) {
355+
const itemsjs = itemsJS(items, configuration);
356+
357+
// false => OR
358+
const orResult = itemsjs.search({
359+
facets: {
360+
tags: {
361+
selected: ['c', 'e'],
362+
options: {
363+
conjunction: false,
364+
},
365+
},
366+
},
367+
});
368+
assert.equal(orResult.pagination.total, 4);
369+
370+
// true => AND should return 0 for disjoint values
371+
const andResult = itemsjs.search({
372+
facets: {
373+
tags: {
374+
selected: ['c', 'e'],
375+
options: {
376+
conjunction: true,
377+
},
378+
},
379+
},
380+
});
381+
assert.equal(andResult.pagination.total, 0);
382+
383+
done();
384+
});
385+
354386
it('makes search with non existing filter value with conjunction true should return no results', function test(done) {
355387
const itemsjs = itemsJS(items, configuration);
356388

0 commit comments

Comments
 (0)