Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent crashing when no items provided #141

Merged
merged 1 commit into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,10 @@ export const index = function (items, fields) {

let i = 1;

fields.forEach((field) => {
facets['data'][field] = Object.create(null);
});

items && items.map((item) => {
if (!item['_id']) {
item['_id'] = i;
Expand All @@ -258,15 +262,10 @@ export const index = function (items, fields) {

items && items.map((item) => {
fields.forEach((field) => {
//if (!item || !item[field]) {
if (!item) {
return;
}

if (!facets['data'][field]) {
facets['data'][field] = Object.create(null);
}

if (Array.isArray(item[field])) {
item[field].forEach((v) => {
if (!item[field]) {
Expand Down
21 changes: 20 additions & 1 deletion tests/searchSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,25 @@ describe('search', function () {
done();
});

it('searches no items with filters and query', function test(done) {
const itemsjs = itemsJS([], configuration);

const result = itemsjs.search({
filters: {
tags: ['a'],
category: ['drama'],
},
query: 'comedy',
});

assert.equal(result.data.items.length, 0);
assert.equal(result.data.aggregations.in_cinema.buckets.length, 0);
assert.equal(result.data.aggregations.category.buckets.length, 0);
assert.equal(result.data.aggregations.year.buckets.length, 0);

done();
});

it('searches with two filters', function test(done) {
const itemsjs = itemsJS(items, configuration);

Expand Down Expand Up @@ -244,7 +263,7 @@ describe('search', function () {
} catch (err) {
assert.equal(
err.message,
'"query" and "filter" options are not working once native search is disabled',
'"query" and "filter" options are not working once native search is disabled'
);
}

Expand Down
Loading