Skip to content

Commit

Permalink
STSMACOM-793: Use the default match and search option in Advanced sea…
Browse files Browse the repository at this point in the history
…rch when they are not entered. (#1421)
  • Loading branch information
Dmytro-Melnyshyn authored Jan 5, 2024
1 parent 4e3c874 commit d3dc0f4
Show file tree
Hide file tree
Showing 5 changed files with 1,158 additions and 1,121 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* Refactor CSS away from postcss-color-function. Refs STSMACOM-791.
* `<EditCustomFieldsSettings>` now passes the `entityType` when making PUT requests to `/custom-fields`. Refs FCFIELDS-44.
* Added `tenant` prop to `<ControlledVocab>`. Refs STSMACOM-794.
* Use the default match and search option in Advanced search when they are not entered. Refs STSMACOM-793.

## [9.0.0](https://github.com/folio-org/stripes-smart-components/tree/v9.0.0) (2023-10-11)
[Full Changelog](https://github.com/folio-org/stripes-smart-components/compare/v8.0.0...v9.0.0)
Expand Down
81 changes: 43 additions & 38 deletions lib/SearchAndSort/advancedSearchQueryToRows.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,43 @@
const advancedSearchQueryToRows = (queryValue) => {
if (!queryValue) {
return [];
}

const splitIntoRowsRegex = /(?=\sor\s|\sand\s|\snot\s)/g;

// split will return array of strings:
// ['keyword==test', 'or issn=123', ...]
const matches = queryValue.split(splitIntoRowsRegex).map(i => i.trim());

return matches.map((match, index) => {
let bool = '';
let query = match;

// first row doesn't have a bool operator
if (index !== 0) {
bool = match.substr(0, match.indexOf(' '));
query = match.substr(bool.length).trim();
}

const matchOperatorStartIndex = query.indexOf(' ') + 1;
const matchOperatorEndIndex = query.substr(matchOperatorStartIndex).indexOf(' ') + matchOperatorStartIndex + 1;

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,
bool,
searchOption: option,
match: _match,
};
});
};

export default advancedSearchQueryToRows;
import {
MATCH_OPTIONS,
DEFAULT_SEARCH_OPTION,
} from '@folio/stripes-components/lib/AdvancedSearch';

const advancedSearchQueryToRows = (queryValue) => {
if (!queryValue) {
return [];
}

const splitIntoRowsRegex = /(?=\sor\s|\sand\s|\snot\s)/g;

// split will return array of strings:
// ['keyword==test', 'or issn=123', ...]
const matches = queryValue.split(splitIntoRowsRegex).map(i => i.trim());

return matches.map((match, index) => {
let bool = '';
let query = match;

// first row doesn't have a bool operator
if (index !== 0) {
bool = match.substr(0, match.indexOf(' '));
query = match.substr(bool.length).trim();
}

const matchOperatorStartIndex = query.indexOf(' ') + 1;
const matchOperatorEndIndex = query.substr(matchOperatorStartIndex).indexOf(' ') + matchOperatorStartIndex + 1;

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,
bool,
searchOption: option || DEFAULT_SEARCH_OPTION,
match: _match || MATCH_OPTIONS.CONTAINS_ALL,
};
});
};

export default advancedSearchQueryToRows;
95 changes: 54 additions & 41 deletions lib/SearchAndSort/tests/advancedSearchQueryToRows-test.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,54 @@
import React from 'react';
import {
describe,
it,
} from 'mocha';
import { expect } from 'chai';

import advancedSearchQueryToRows from '../advancedSearchQueryToRows';

describe('advancedSearchQueryToRows', () => {
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',
});
});
});
});
import React from 'react';
import {
describe,
it,
} from 'mocha';
import { expect } from 'chai';

import advancedSearchQueryToRows from '../advancedSearchQueryToRows';

describe('advancedSearchQueryToRows', () => {
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',
});
});
});

describe('when query without match and search option', () => {
it('should use default value for match and search option', () => {
const query = 'test';

expect(advancedSearchQueryToRows(query)).to.deep.include({
query,
bool: '',
searchOption: 'keyword',
match: 'containsAll',
});
});
});
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
"stylelint-junit-formatter": "^0.2.2"
},
"dependencies": {
"@rehooks/local-storage": "^2.4.4",
"@rehooks/local-storage": "2.4.4",
"classnames": "^2.2.6",
"final-form": "^4.18.2",
"final-form-arrays": "^3.0.2",
Expand Down
Loading

0 comments on commit d3dc0f4

Please sign in to comment.