Last active
June 25, 2022 19:40
-
-
Save RenatoPacheco/0860481b0d340a08f732053a922ec3a5 to your computer and use it in GitHub Desktop.
Method to convert a search string into search parameters.
This file contains hidden or 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
function getParameters(searchText) { | |
const result = { | |
text: null, | |
parameters: {}, | |
}; | |
let match; | |
let key; | |
let value; | |
let list = []; | |
const find = / ([a-z]+):\"([^\"]+)\" /gim; | |
if (searchText) { | |
searchText = ' ' + searchText.replace(/\s+/gim, ' ') + ' '; | |
do { | |
match = find.exec(searchText); | |
if (match) { | |
key = match[1].toLowerCase(); | |
value = match[2]; | |
if (!result.parameters[key]) { | |
result.parameters[key] = []; | |
} | |
list = value.split('|'); | |
for (let i = 0; i < list.length; i++) { | |
result.parameters[key].push( | |
list[i].replace(/\s+/gim, ' ').replace(/^\s+|\s+$/gim, '') | |
); | |
} | |
} | |
} while (match); | |
searchText = searchText.replace(find, ' '); | |
searchText = searchText.replace(/\s+/gim, ' '); | |
searchText = searchText.replace(/^\s+|\s+$/gim, ''); | |
result.text = searchText; | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment