-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
STSMACOM-793: Use the default match and search option in Advanced sea…
…rch when they are not entered. (#1421)
- Loading branch information
1 parent
4e3c874
commit d3dc0f4
Showing
5 changed files
with
1,158 additions
and
1,121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
95
lib/SearchAndSort/tests/advancedSearchQueryToRows-test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.