Skip to content

Commit

Permalink
fix(search): guard against whitespace only pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
nikku committed Nov 21, 2024
1 parent bf03eb3 commit e80568c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/features/search/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ export default function search(items, pattern, options) {
keys
} = options;

pattern = pattern.trim().toLowerCase();

if (!pattern) {
throw new Error('<pattern> must not be empty');
}

const words = pattern.trim().toLowerCase().split(/\s+/);

return items.flatMap((item) => {
Expand Down
16 changes: 16 additions & 0 deletions test/spec/features/search/searchSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,22 @@ describe('features/search', function() {
expect(results).to.have.length(1);
}));


it('should error on whitespace pattern', inject(function(search) {

// given
const fn = () => {
search([], ' ', {
keys: [
'title'
]
});
};

// then
expect(fn).to.throw(/<pattern> must not be empty/);
}));

});


Expand Down

0 comments on commit e80568c

Please sign in to comment.