2017-09-10 17:25:29 +05:30
|
|
|
import './filtered_search_token_keys';
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
export default class FilteredSearchTokenizer {
|
2017-09-10 17:25:29 +05:30
|
|
|
static processTokens(input, allowedKeys) {
|
2017-08-17 22:00:37 +05:30
|
|
|
// Regex extracts `(token):(symbol)(value)`
|
|
|
|
// Values that start with a double quote must end in a double quote (same for single)
|
2018-12-13 13:39:08 +05:30
|
|
|
const tokenRegex = new RegExp(
|
|
|
|
`(${allowedKeys.join('|')}):([~%@]?)(?:('[^']*'{0,1})|("[^"]*"{0,1})|(\\S+))`,
|
|
|
|
'g',
|
|
|
|
);
|
2017-08-17 22:00:37 +05:30
|
|
|
const tokens = [];
|
|
|
|
const tokenIndexes = []; // stores key+value for simple search
|
|
|
|
let lastToken = null;
|
2018-12-13 13:39:08 +05:30
|
|
|
const searchToken =
|
|
|
|
input
|
|
|
|
.replace(tokenRegex, (match, key, symbol, v1, v2, v3) => {
|
|
|
|
let tokenValue = v1 || v2 || v3;
|
|
|
|
let tokenSymbol = symbol;
|
|
|
|
let tokenIndex = '';
|
|
|
|
|
|
|
|
if (tokenValue === '~' || tokenValue === '%' || tokenValue === '@') {
|
|
|
|
tokenSymbol = tokenValue;
|
|
|
|
tokenValue = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
tokenIndex = `${key}:${tokenValue}`;
|
|
|
|
|
|
|
|
// Prevent adding duplicates
|
|
|
|
if (tokenIndexes.indexOf(tokenIndex) === -1) {
|
|
|
|
tokenIndexes.push(tokenIndex);
|
|
|
|
|
|
|
|
tokens.push({
|
|
|
|
key,
|
|
|
|
value: tokenValue || '',
|
|
|
|
symbol: tokenSymbol || '',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return '';
|
|
|
|
})
|
|
|
|
.replace(/\s{2,}/g, ' ')
|
|
|
|
.trim() || '';
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
if (tokens.length > 0) {
|
|
|
|
const last = tokens[tokens.length - 1];
|
|
|
|
const lastString = `${last.key}:${last.symbol}${last.value}`;
|
2018-12-13 13:39:08 +05:30
|
|
|
lastToken =
|
|
|
|
input.lastIndexOf(lastString) === input.length - lastString.length ? last : searchToken;
|
2017-08-17 22:00:37 +05:30
|
|
|
} else {
|
|
|
|
lastToken = searchToken;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
tokens,
|
|
|
|
lastToken,
|
|
|
|
searchToken,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|