Skip to content

Instantly share code, notes, and snippets.

@RenatoPacheco
Last active June 25, 2022 19:40
Show Gist options
  • Save RenatoPacheco/0860481b0d340a08f732053a922ec3a5 to your computer and use it in GitHub Desktop.
Save RenatoPacheco/0860481b0d340a08f732053a922ec3a5 to your computer and use it in GitHub Desktop.
Method to convert a search string into search parameters.
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