Skip to content

Commit

Permalink
feat(search): always provide tokens
Browse files Browse the repository at this point in the history
We ensure that for every entry that is a match we _always_ provide
tokens. This ensures that a user can rely on these tokens being there,
i.e. to render the results.

Related to bpmn-io/bpmn-js#2235
  • Loading branch information
nikku committed Nov 4, 2024
1 parent 147fd00 commit cd70b64
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 8 deletions.
19 changes: 11 additions & 8 deletions lib/features/search/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,12 @@ export default function search(items, pattern, options) {
pattern = pattern.trim();

return items.flatMap((item) => {
const tokens = getTokens(item, pattern, keys);
const {
__matches: matches,
...tokens
} = getTokens(item, pattern, keys);

if (!Object.keys(tokens).length) {
if (!matches) {
return [];
}

Expand All @@ -64,12 +67,12 @@ function getTokens(item, pattern, keys) {

const tokens = getMatchingTokens(string, pattern);

if (hasMatch(tokens)) {
results[ key ] = tokens;
}

return results;
}, {});
return {
...results,
[ key ]: tokens,
__matches: results.__matches || hasMatch(tokens)
};
}, { });
}

/**
Expand Down
60 changes: 60 additions & 0 deletions test/spec/features/search/searchSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,66 @@ describe('search', function() {
}));




describe('result', function() {

it('should provide <item>', inject(function(search) {

// given
const items = [
{
title: 'foo',
description: 'woop'
},
{
title: 'foobar'
}
];

// when
const result = search(items, 'foo', {
keys: [
'title',
'description'
]
});

// then
expect(result[0].item).to.equal(items[0]);
expect(result[1].item).to.equal(items[1]);
}));


it('should provide <tokens>', inject(function(search) {

// given
const items = [
{
title: 'foo',
description: 'woop'
},
{
title: 'foobar'
}
];

// when
const result = search(items, 'foo', {
keys: [
'title',
'description'
]
});

// then
expect(result[0].tokens).to.have.keys([ 'title', 'description' ]);
expect(result[1].tokens).to.have.keys([ 'title', 'description' ]);

expect(result[1].tokens.description).to.be.empty;
}));

});
it('should search complex', inject(function(search) {

// given
Expand Down

0 comments on commit cd70b64

Please sign in to comment.