mirror of
https://github.com/philomena-dev/philomena.git
synced 2025-02-13 09:24:22 +01:00
Merge pull request #416 from koloml/dont-show-aliases-starting-with-prefix
Don't display aliases in suggestions when both alias and tag name are matched by the prefix
This commit is contained in:
commit
a6faa53999
3 changed files with 55 additions and 15 deletions
|
@ -8,7 +8,7 @@ import store from './utils/store';
|
||||||
import { TermContext } from './query/lex';
|
import { TermContext } from './query/lex';
|
||||||
import { $$ } from './utils/dom';
|
import { $$ } from './utils/dom';
|
||||||
import {
|
import {
|
||||||
formatLocalAutocompleteResult,
|
createLocalAutocompleteResultFormatter,
|
||||||
fetchLocalAutocomplete,
|
fetchLocalAutocomplete,
|
||||||
fetchSuggestions,
|
fetchSuggestions,
|
||||||
SuggestionsPopup,
|
SuggestionsPopup,
|
||||||
|
@ -196,9 +196,11 @@ function listenAutocomplete() {
|
||||||
originalTerm = `${inputField.value}`.toLowerCase();
|
originalTerm = `${inputField.value}`.toLowerCase();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const matchedTerm = trimPrefixes(originalTerm);
|
||||||
|
|
||||||
const suggestions = localAc
|
const suggestions = localAc
|
||||||
.matchPrefix(trimPrefixes(originalTerm), suggestionsCount)
|
.matchPrefix(matchedTerm, suggestionsCount)
|
||||||
.map(formatLocalAutocompleteResult);
|
.map(createLocalAutocompleteResultFormatter(matchedTerm));
|
||||||
|
|
||||||
if (suggestions.length) {
|
if (suggestions.length) {
|
||||||
popup.renderSuggestions(suggestions).showForField(targetedInput);
|
popup.renderSuggestions(suggestions).showForField(targetedInput);
|
||||||
|
|
|
@ -2,7 +2,7 @@ import { fetchMock } from '../../../test/fetch-mock.ts';
|
||||||
import {
|
import {
|
||||||
fetchLocalAutocomplete,
|
fetchLocalAutocomplete,
|
||||||
fetchSuggestions,
|
fetchSuggestions,
|
||||||
formatLocalAutocompleteResult,
|
createLocalAutocompleteResultFormatter,
|
||||||
purgeSuggestionsCache,
|
purgeSuggestionsCache,
|
||||||
SuggestionsPopup,
|
SuggestionsPopup,
|
||||||
TermSuggestion,
|
TermSuggestion,
|
||||||
|
@ -334,12 +334,13 @@ describe('Suggestions', () => {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('formatLocalAutocompleteResult', () => {
|
describe('createLocalAutocompleteResultFormatter', () => {
|
||||||
it('should format suggested tags as tag name and the count', () => {
|
it('should format suggested tags as tag name and the count', () => {
|
||||||
const tagName = 'safe';
|
const tagName = 'safe';
|
||||||
const tagCount = getRandomIntBetween(5, 10);
|
const tagCount = getRandomIntBetween(5, 10);
|
||||||
|
|
||||||
const resultObject = formatLocalAutocompleteResult({
|
const formatter = createLocalAutocompleteResultFormatter();
|
||||||
|
const resultObject = formatter({
|
||||||
name: tagName,
|
name: tagName,
|
||||||
aliasName: tagName,
|
aliasName: tagName,
|
||||||
imageCount: tagCount,
|
imageCount: tagCount,
|
||||||
|
@ -354,7 +355,8 @@ describe('Suggestions', () => {
|
||||||
const tagAlias = 'rating:safe';
|
const tagAlias = 'rating:safe';
|
||||||
const tagCount = getRandomIntBetween(5, 10);
|
const tagCount = getRandomIntBetween(5, 10);
|
||||||
|
|
||||||
const resultObject = formatLocalAutocompleteResult({
|
const formatter = createLocalAutocompleteResultFormatter();
|
||||||
|
const resultObject = formatter({
|
||||||
name: tagName,
|
name: tagName,
|
||||||
aliasName: tagAlias,
|
aliasName: tagAlias,
|
||||||
imageCount: tagCount,
|
imageCount: tagCount,
|
||||||
|
@ -363,5 +365,39 @@ describe('Suggestions', () => {
|
||||||
expect(resultObject.label).toBe(`${tagAlias} ⇒ ${tagName} (${tagCount})`);
|
expect(resultObject.label).toBe(`${tagAlias} ⇒ ${tagName} (${tagCount})`);
|
||||||
expect(resultObject.value).toBe(tagName);
|
expect(resultObject.value).toBe(tagName);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should not display aliases when tag is starting with the same matched', () => {
|
||||||
|
const tagName = 'chest fluff';
|
||||||
|
const tagAlias = 'chest floof';
|
||||||
|
const tagCount = getRandomIntBetween(5, 10);
|
||||||
|
|
||||||
|
const prefix = 'ch';
|
||||||
|
|
||||||
|
const formatter = createLocalAutocompleteResultFormatter(prefix);
|
||||||
|
const resultObject = formatter({
|
||||||
|
name: tagName,
|
||||||
|
aliasName: tagAlias,
|
||||||
|
imageCount: tagCount,
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(resultObject.label).toBe(`${tagName} (${tagCount})`);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should display aliases if matched prefix is different from the tag name', () => {
|
||||||
|
const tagName = 'queen chrysalis';
|
||||||
|
const tagAlias = 'chrysalis';
|
||||||
|
const tagCount = getRandomIntBetween(5, 10);
|
||||||
|
|
||||||
|
const prefix = 'ch';
|
||||||
|
|
||||||
|
const formatter = createLocalAutocompleteResultFormatter(prefix);
|
||||||
|
const resultObject = formatter({
|
||||||
|
name: tagName,
|
||||||
|
aliasName: tagAlias,
|
||||||
|
imageCount: tagCount,
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(resultObject.label).toBe(`${tagAlias} ⇒ ${tagName} (${tagCount})`);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -176,10 +176,11 @@ export async function fetchLocalAutocomplete(): Promise<LocalAutocompleter> {
|
||||||
.then(buf => new LocalAutocompleter(buf));
|
.then(buf => new LocalAutocompleter(buf));
|
||||||
}
|
}
|
||||||
|
|
||||||
export function formatLocalAutocompleteResult(result: Result): TermSuggestion {
|
export function createLocalAutocompleteResultFormatter(matchedPrefix?: string): (result: Result) => TermSuggestion {
|
||||||
|
return result => {
|
||||||
let tagName = result.name;
|
let tagName = result.name;
|
||||||
|
|
||||||
if (tagName !== result.aliasName) {
|
if (tagName !== result.aliasName && (!matchedPrefix || !tagName.startsWith(matchedPrefix))) {
|
||||||
tagName = `${result.aliasName} ⇒ ${tagName}`;
|
tagName = `${result.aliasName} ⇒ ${tagName}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -187,4 +188,5 @@ export function formatLocalAutocompleteResult(result: Result): TermSuggestion {
|
||||||
value: result.name,
|
value: result.name,
|
||||||
label: `${tagName} (${result.imageCount})`,
|
label: `${tagName} (${result.imageCount})`,
|
||||||
};
|
};
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue