Skip to content

Adding a full option to Input.search for searching the whole text #226

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
17 changes: 9 additions & 8 deletions src/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ export function search(data, {
datalist,
disabled,
required = true, // if true, the value is everything if nothing is selected
width
width,
full = false // If false will only search at the beginning of the options
} = {}) {
let value = [];
data = arrayify(data);
Expand All @@ -47,7 +48,7 @@ export function search(data, {
</form>`;
form.addEventListener("submit", preventDefault);
function oninput() {
value = input.value || required ? data.filter(filter(input.value)) : [];
value = input.value || required ? data.filter(filter(input.value, full)) : [];
if (columns !== undefined) value.columns = columns;
output.value = format(value.length);
}
Expand All @@ -70,8 +71,8 @@ export function search(data, {
});
}

export function searchFilter(query) {
const filters = `${query}`.split(/\s+/g).filter(t => t).map(termFilter);
export function searchFilter(query, full = false) {
const filters = `${query}`.split(/\s+/g).filter(t => t).map(getTermFilter(full));
return d => {
if (d == null) return false;
if (typeof d === "object") {
Expand All @@ -94,9 +95,9 @@ export function searchFilter(query) {
};
}

function columnFilter(columns) {
function columnFilter(columns, full = false) {
return query => {
const filters = `${query}`.split(/\s+/g).filter(t => t).map(termFilter);
const filters = `${query}`.split(/\s+/g).filter(t => t).map(getTermFilter(full));
return d => {
out: for (const filter of filters) {
for (const column of columns) {
Expand All @@ -117,8 +118,8 @@ function* valuesof(d) {
}
}

function termFilter(term) {
return new RegExp(`(?:^|[^\\p{L}-])${escapeRegExp(term)}`, "iu");
function getTermFilter(full = false) {
return (term) => new RegExp(`(?:^${full ? ".*" : "" }|[^\\p{L}-])${escapeRegExp(term)}`, "iu");
}

function escapeRegExp(text) {
Expand Down
8 changes: 8 additions & 0 deletions test/search-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,12 @@ it("Inputs.search(data) handles word boundaries", () => {
it("Inputs.search(data) supports Cyrillic symbols", () => {
const s = Inputs.search(["red", "дом", "чудо", "Река Дон"], {query: "до"});
assert.deepStrictEqual(s.value, ["дом", "Река Дон"]);
});

it("Inputs.search(data) supports full search", () => {
const s = Inputs.search(["red", "blue", "The Real", "Mr.Real", "un-real"], {query: "re", full:true});
assert.deepStrictEqual(s.value, ["red", "The Real", "Mr.Real", "un-real"]);

const s2 = Inputs.search(["red", "дом", "чудо", "Река Дон"], {query: "до", full:true});
assert.deepStrictEqual(s2.value, ["дом", "чудо", "Река Дон"]);
});
Loading