Skip to content

Commit

Permalink
STSMACOM-787 Advanced search handle case when name of match operator …
Browse files Browse the repository at this point in the history
…is a part of value
  • Loading branch information
BogdanDenis committed Nov 1, 2023
1 parent a2190d5 commit 10f861d
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 7 deletions.
13 changes: 6 additions & 7 deletions lib/SearchAndSort/advancedSearchQueryToRows.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,15 @@ const advancedSearchQueryToRows = (queryValue) => {
// first row doesn't have a bool operator
if (index !== 0) {
bool = match.substr(0, match.indexOf(' '));
query = match.substr(bool.length);
query = match.substr(bool.length).trim();
}

const splitIndexAndQueryRegex = /([^=]+)(exactPhrase|containsAll|startsWith|containsAny)(.+)/g;
const matchOperatorStartIndex = query.indexOf(' ') + 1;
const matchOperatorEndIndex = query.substr(matchOperatorStartIndex).indexOf(' ') + matchOperatorStartIndex + 1;

const rowParts = [...query.matchAll(splitIndexAndQueryRegex)]?.[0] || [];
// eslint-disable-next-line no-unused-vars
const [, option, _match, value] = rowParts
.map(i => i.trim())
.map(i => i.replaceAll('"', ''));
const option = query.substr(0, matchOperatorStartIndex).trim().replaceAll('"', '');
const _match = query.substring(matchOperatorStartIndex, matchOperatorEndIndex).trim().replaceAll('"', '');
const value = query.substring(matchOperatorEndIndex).trim().replaceAll('"', '');

return {
query: value,
Expand Down
41 changes: 41 additions & 0 deletions lib/SearchAndSort/tests/advancedSearchQueryToRows-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import React from 'react';
import {
describe,
it,
} from 'mocha';
import { expect } from 'chai';

import advancedSearchQueryToRows from '../advancedSearchQueryToRows';

describe.only('advancedSearchQueryToRows', () => {

Check failure on line 10 in lib/SearchAndSort/tests/advancedSearchQueryToRows-test.js

View workflow job for this annotation

GitHub Actions / build-npm

describe.only not permitted
describe('when query contains multiple rows', () => {
it('should parse query correctly', () => {
const query = 'keyword exactPhrase value1 or keyword exactPhrase value2';

expect(advancedSearchQueryToRows(query)).to.have.deep.members([{
query: 'value1',
bool: '',
searchOption: 'keyword',
match: 'exactPhrase',
}, {
query: 'value2',
bool: 'or',
searchOption: 'keyword',
match: 'exactPhrase',
}]);
});
});

describe('when query contains match option as part of value', () => {
it('should parse query correctly', () => {
const query = 'keyword containsAny containsAny1';

expect(advancedSearchQueryToRows(query)).to.deep.include({
query: 'containsAny1',
bool: '',
searchOption: 'keyword',
match: 'containsAny',
});
});
});
});

0 comments on commit 10f861d

Please sign in to comment.