Skip to content

Commit

Permalink
Improve ignoring terms and more postprocessing on autocomplete term
Browse files Browse the repository at this point in the history
  • Loading branch information
SeinopSys committed Nov 15, 2020
1 parent 2db5b77 commit 82156a5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
5 changes: 4 additions & 1 deletion assets/js/autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ function getTermPosition(input) {
* @param {HTMLInputElement} input
* @param {string} [term] term to force, pass undefined or omit to clear
*/
function setAutocompleteTerm(input, term) {
function setAutocompleteTerm(input, term = '') {
input.dataset.acTerm = term;
}

Expand All @@ -170,9 +170,12 @@ function setAutocompleteTerm(input, term) {
*/
function postprocessTerm(term) {
let processedTerm = term;
// Replaces underscores with spaces where applicable
if (!termsAllowingUnderscores.test(term)) {
processedTerm = processedTerm.replace(/_/g, ' ');
}
// Remove spaces before/after namespace
processedTerm = processedTerm.replace(/^([^:\s]+)\s*:\s*(.*)$/g, '$1:$2');
return processedTerm;
}

Expand Down
28 changes: 19 additions & 9 deletions assets/js/search_autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,32 @@ const LITERAL_FIELDS = [
'duration',
'created_at',
'updated_at',
'first_seen_at'
'first_seen_at',
'faved_by',
'orig_sha512_hash',
'sha512_hash',
'uploader',
'source_url',
'original_format',
'mime_type',
'description',
'gallery_id',
'favourited_by_users',
'favourited_by_user_ids',
];

/**
* @type {RegExp[]}
* A regular expression that matches any terms that should not be autocompleted
*/
const ignoredTerms = [
new RegExp(`^(?:${LITERAL_FIELDS.join('|')})(?:\\.[^:]*)?:`),
];
const ignoredTermRegex = new RegExp(`^(?:${LITERAL_FIELDS.join('|')})(?:\\.[gl]te|:)`);

/**
* @param {string} value
* Checks if autocompletion should ignore the specified term
* @param {string} term
* @returns {boolean}
*/
function isIgnoredTerm(value) {
return ignoredTerms.some(regex => regex.test(value));
function isIgnoredTerm(term) {
return ignoredTermRegex.test(term);
}

/**
Expand All @@ -54,7 +64,7 @@ function isIgnoredTerm(value) {
*/

/**
* Extract the search term form an input string and the cursor position
* Extract the current search term form an input string based on the cursor position
* @param {string} input
* @param {number} cursorPos
* @return {ParserToken}
Expand Down

0 comments on commit 82156a5

Please sign in to comment.