Skip to content

Commit

Permalink
add idxs param to .filter(), to allow impl of prefix result caching
Browse files Browse the repository at this point in the history
  • Loading branch information
leeoniya committed Sep 25, 2022
1 parent 040701d commit 3a26d78
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 21 deletions.
18 changes: 13 additions & 5 deletions dist/uFuzzy.cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const OPTS = {
// inter-bounds mode
// 2 = strict (will only match 'man' on whitepace and punct boundaries: Mega Man, Mega_Man, mega.man)
// 1 = loose (plus allowance for alpha-num and case-change boundaries: MegaMan, 0007man)
// 0 = none (will match 'man' as any substring: megamaniac)
// 0 = any (will match 'man' as any substring: megamaniac)
interLft: 0,
interRgt: 0,

Expand Down Expand Up @@ -124,14 +124,22 @@ function uFuzzy(opts) {
return [new RegExp(reTpl, 'i'), parts];
};

const filter = (haystack, needle) => {
const filter = (haystack, needle, idxs) => {

let out = [];
let [query] = prepQuery(needle);

for (let i = 0; i < haystack.length; i++) {
let item = haystack[i];
query.test(item) && out.push(i);
if (idxs != null) {
for (let i = 0; i < idxs.length; i++) {
let item = haystack[idxs[i]];
query.test(item) && out.push(i);
}
}
else {
for (let i = 0; i < haystack.length; i++) {
let item = haystack[i];
query.test(item) && out.push(i);
}
}

return out;
Expand Down
18 changes: 13 additions & 5 deletions dist/uFuzzy.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const OPTS = {
// inter-bounds mode
// 2 = strict (will only match 'man' on whitepace and punct boundaries: Mega Man, Mega_Man, mega.man)
// 1 = loose (plus allowance for alpha-num and case-change boundaries: MegaMan, 0007man)
// 0 = none (will match 'man' as any substring: megamaniac)
// 0 = any (will match 'man' as any substring: megamaniac)
interLft: 0,
interRgt: 0,

Expand Down Expand Up @@ -122,14 +122,22 @@ function uFuzzy(opts) {
return [new RegExp(reTpl, 'i'), parts];
};

const filter = (haystack, needle) => {
const filter = (haystack, needle, idxs) => {

let out = [];
let [query] = prepQuery(needle);

for (let i = 0; i < haystack.length; i++) {
let item = haystack[i];
query.test(item) && out.push(i);
if (idxs != null) {
for (let i = 0; i < idxs.length; i++) {
let item = haystack[idxs[i]];
query.test(item) && out.push(i);
}
}
else {
for (let i = 0; i < haystack.length; i++) {
let item = haystack[i];
query.test(item) && out.push(i);
}
}

return out;
Expand Down
18 changes: 13 additions & 5 deletions dist/uFuzzy.iife.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ var uFuzzy = (function () {
// inter-bounds mode
// 2 = strict (will only match 'man' on whitepace and punct boundaries: Mega Man, Mega_Man, mega.man)
// 1 = loose (plus allowance for alpha-num and case-change boundaries: MegaMan, 0007man)
// 0 = none (will match 'man' as any substring: megamaniac)
// 0 = any (will match 'man' as any substring: megamaniac)
interLft: 0,
interRgt: 0,

Expand Down Expand Up @@ -125,14 +125,22 @@ var uFuzzy = (function () {
return [new RegExp(reTpl, 'i'), parts];
};

const filter = (haystack, needle) => {
const filter = (haystack, needle, idxs) => {

let out = [];
let [query] = prepQuery(needle);

for (let i = 0; i < haystack.length; i++) {
let item = haystack[i];
query.test(item) && out.push(i);
if (idxs != null) {
for (let i = 0; i < idxs.length; i++) {
let item = haystack[idxs[i]];
query.test(item) && out.push(i);
}
}
else {
for (let i = 0; i < haystack.length; i++) {
let item = haystack[i];
query.test(item) && out.push(i);
}
}

return out;
Expand Down
2 changes: 1 addition & 1 deletion dist/uFuzzy.iife.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 13 additions & 5 deletions src/uFuzzy.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const OPTS = {
// inter-bounds mode
// 2 = strict (will only match 'man' on whitepace and punct boundaries: Mega Man, Mega_Man, mega.man)
// 1 = loose (plus allowance for alpha-num and case-change boundaries: MegaMan, 0007man)
// 0 = none (will match 'man' as any substring: megamaniac)
// 0 = any (will match 'man' as any substring: megamaniac)
interLft: 0,
interRgt: 0,

Expand Down Expand Up @@ -115,15 +115,23 @@ export default function uFuzzy(opts) {
return [new RegExp(reTpl, 'i'), parts];
};

const filter = (haystack, needle) => {
const filter = (haystack, needle, idxs) => {
DEBUG && console.time('filter');

let out = [];
let [query] = prepQuery(needle);

for (let i = 0; i < haystack.length; i++) {
let item = haystack[i];
query.test(item) && out.push(i);
if (idxs != null) {
for (let i = 0; i < idxs.length; i++) {
let item = haystack[idxs[i]];
query.test(item) && out.push(i);
}
}
else {
for (let i = 0; i < haystack.length; i++) {
let item = haystack[i];
query.test(item) && out.push(i);
}
}

DEBUG && console.timeEnd('filter');
Expand Down

0 comments on commit 3a26d78

Please sign in to comment.