Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions dist/format.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { AutocompleteResponse, Suggestion } from "./types";
export declare function lookupFilter(suggestion: Suggestion, _originalQuery: string, queryLowerCase: string): boolean;
import type { AutocompleteResponse, ResolvedOptions, Suggestion } from "./types";
export declare function lookupFilter(suggestion: Suggestion, _originalQuery: string, queryLowerCase: string, options?: ResolvedOptions): boolean;
export declare function transformResult(response: string | AutocompleteResponse): AutocompleteResponse;
export declare function formatResult(suggestion: Suggestion, currentValue: string): string;
export declare function formatResult(suggestion: Suggestion, currentValue: string, _index?: number, options?: ResolvedOptions): string;
export declare function formatGroup(_suggestion: Suggestion, category: string): string;
62 changes: 57 additions & 5 deletions dist/jquery.autocomplete.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,27 +39,76 @@ var keys = {
};

// src/format.ts
function lookupFilter(suggestion, _originalQuery, queryLowerCase) {
function lookupFilter(suggestion, _originalQuery, queryLowerCase, options) {
if (options?.ignoreDiacritics) {
return foldText(suggestion.value).text.indexOf(foldText(queryLowerCase).text) !== -1;
}
return suggestion.value.toLowerCase().indexOf(queryLowerCase) !== -1;
}
function transformResult(response) {
return typeof response === "string" ? JSON.parse(response) : response;
}
function formatResult(suggestion, currentValue) {
function formatResult(suggestion, currentValue, _index, options) {
if (!currentValue) {
const span = document.createElement("span");
span.textContent = suggestion.value;
return span.innerHTML;
}
if (options?.ignoreDiacritics) {
return highlightFolded(suggestion.value, currentValue);
}
const pattern = "(" + utils.escapeRegExChars(currentValue) + ")";
return suggestion.value.replace(new RegExp(pattern, "gi"), "<strong>$1</strong>").replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/&lt;(\/?strong)&gt;/g, "<$1>");
return escapeHtml(
suggestion.value.replace(new RegExp(pattern, "gi"), "<strong>$1</strong>")
).replace(/&lt;(\/?strong)&gt;/g, "<$1>");
}
function formatGroup(_suggestion, category) {
const div = document.createElement("div");
div.className = "autocomplete-group";
div.textContent = category;
return div.outerHTML;
}
function escapeHtml(value) {
return value.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;");
}
function foldText(value) {
const folded = { text: "", starts: [], ends: [] };
let offset = 0;
for (const char of value) {
const end = offset + char.length;
const piece = char.normalize("NFD").replace(/\p{M}/gu, "").normalize("NFC").toLowerCase();
for (let k = folded.ends.length - 1; !piece && k >= 0 && folded.ends[k] === offset; k--) {
folded.ends[k] = end;
}
for (let i = 0; i < piece.length; i++) {
folded.starts.push(offset);
folded.ends.push(end);
}
folded.text += piece;
offset = end;
}
return folded;
}
function highlightFolded(value, currentValue) {
const query = foldText(currentValue).text;
if (!query) {
return escapeHtml(value);
}
const folded = foldText(value);
let html = "";
let last = 0;
let from = 0;
let hit;
while ((hit = folded.text.indexOf(query, from)) !== -1) {
const start = Math.max(folded.starts[hit], last);
const end = folded.ends[hit + query.length - 1];
html += escapeHtml(value.slice(last, start));
html += "<strong>" + escapeHtml(value.slice(start, end)) + "</strong>";
last = end;
from = hit + query.length;
}
return html + escapeHtml(value.slice(last));
}

// src/defaults.ts
var noop = () => {
Expand Down Expand Up @@ -88,6 +137,7 @@ var defaults = {
triggerSelectOnValidInput: true,
preventBadQueries: true,
lookupFilter,
ignoreDiacritics: false,
paramName: "query",
transformResult,
showNoSuggestionNotice: false,
Expand Down Expand Up @@ -387,7 +437,9 @@ var _Autocomplete = class _Autocomplete {
const filter = options.lookupFilter;
const limit = parseInt(options.lookupLimit, 10);
const lookup = options.lookup;
const matched = lookup.filter((suggestion) => filter(suggestion, query, queryLowerCase));
const matched = lookup.filter(
(suggestion) => filter(suggestion, query, queryLowerCase, options)
);
return {
suggestions: limit && matched.length > limit ? matched.slice(0, limit) : matched
};
Expand Down Expand Up @@ -510,7 +562,7 @@ var _Autocomplete = class _Autocomplete {
};
const html = this.suggestions.map((suggestion, i) => {
const group = groupBy ? formatGroupFn(suggestion) : "";
return `${group}<div class="${className}" data-index="${i}">${formatResultFn(suggestion, value, i)}</div>`;
return `${group}<div class="${className}" data-index="${i}">${formatResultFn(suggestion, value, i, options)}</div>`;
}).join("");
this.adjustContainerWidth();
this.$noSuggestionsContainer.detach();
Expand Down
62 changes: 57 additions & 5 deletions dist/jquery.autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,27 +47,76 @@
};

// src/format.ts
function lookupFilter(suggestion, _originalQuery, queryLowerCase) {
function lookupFilter(suggestion, _originalQuery, queryLowerCase, options) {
if (options?.ignoreDiacritics) {
return foldText(suggestion.value).text.indexOf(foldText(queryLowerCase).text) !== -1;
}
return suggestion.value.toLowerCase().indexOf(queryLowerCase) !== -1;
}
function transformResult(response) {
return typeof response === "string" ? JSON.parse(response) : response;
}
function formatResult(suggestion, currentValue) {
function formatResult(suggestion, currentValue, _index, options) {
if (!currentValue) {
const span = document.createElement("span");
span.textContent = suggestion.value;
return span.innerHTML;
}
if (options?.ignoreDiacritics) {
return highlightFolded(suggestion.value, currentValue);
}
const pattern = "(" + utils.escapeRegExChars(currentValue) + ")";
return suggestion.value.replace(new RegExp(pattern, "gi"), "<strong>$1</strong>").replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/&lt;(\/?strong)&gt;/g, "<$1>");
return escapeHtml(
suggestion.value.replace(new RegExp(pattern, "gi"), "<strong>$1</strong>")
).replace(/&lt;(\/?strong)&gt;/g, "<$1>");
}
function formatGroup(_suggestion, category) {
const div = document.createElement("div");
div.className = "autocomplete-group";
div.textContent = category;
return div.outerHTML;
}
function escapeHtml(value) {
return value.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;");
}
function foldText(value) {
const folded = { text: "", starts: [], ends: [] };
let offset = 0;
for (const char of value) {
const end = offset + char.length;
const piece = char.normalize("NFD").replace(/\p{M}/gu, "").normalize("NFC").toLowerCase();
for (let k = folded.ends.length - 1; !piece && k >= 0 && folded.ends[k] === offset; k--) {
folded.ends[k] = end;
}
for (let i = 0; i < piece.length; i++) {
folded.starts.push(offset);
folded.ends.push(end);
}
folded.text += piece;
offset = end;
}
return folded;
}
function highlightFolded(value, currentValue) {
const query = foldText(currentValue).text;
if (!query) {
return escapeHtml(value);
}
const folded = foldText(value);
let html = "";
let last = 0;
let from = 0;
let hit;
while ((hit = folded.text.indexOf(query, from)) !== -1) {
const start = Math.max(folded.starts[hit], last);
const end = folded.ends[hit + query.length - 1];
html += escapeHtml(value.slice(last, start));
html += "<strong>" + escapeHtml(value.slice(start, end)) + "</strong>";
last = end;
from = hit + query.length;
}
return html + escapeHtml(value.slice(last));
}

// src/defaults.ts
var noop = () => {
Expand Down Expand Up @@ -96,6 +145,7 @@
triggerSelectOnValidInput: true,
preventBadQueries: true,
lookupFilter,
ignoreDiacritics: false,
paramName: "query",
transformResult,
showNoSuggestionNotice: false,
Expand Down Expand Up @@ -395,7 +445,9 @@
const filter = options.lookupFilter;
const limit = parseInt(options.lookupLimit, 10);
const lookup = options.lookup;
const matched = lookup.filter((suggestion) => filter(suggestion, query, queryLowerCase));
const matched = lookup.filter(
(suggestion) => filter(suggestion, query, queryLowerCase, options)
);
return {
suggestions: limit && matched.length > limit ? matched.slice(0, limit) : matched
};
Expand Down Expand Up @@ -518,7 +570,7 @@
};
const html = this.suggestions.map((suggestion, i) => {
const group = groupBy ? formatGroupFn(suggestion) : "";
return `${group}<div class="${className}" data-index="${i}">${formatResultFn(suggestion, value, i)}</div>`;
return `${group}<div class="${className}" data-index="${i}">${formatResultFn(suggestion, value, i, options)}</div>`;
}).join("");
this.adjustContainerWidth();
this.$noSuggestionsContainer.detach();
Expand Down
Loading
Loading