Skip to content

Commit e5635aa

Browse files
committed
handle exclusions-only needles. close #25.
1 parent 3a1da66 commit e5635aa

File tree

5 files changed

+85
-21
lines changed

5 files changed

+85
-21
lines changed

dist/uFuzzy.cjs.js

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const cmp = new Intl.Collator('en').compare;
1313

1414
const inf = Infinity;
1515

16-
const NEGS_RE = /(?:\s|^)-[a-z\d]+/ig;
16+
const NEGS_RE = /(?:\s+|^)-[a-z\d]+/ig;
1717

1818
const OPTS = {
1919
// term segmentation & punct/whitespace merging
@@ -630,10 +630,28 @@ function uFuzzy(opts) {
630630

631631
needle = needle.replace(NEGS_RE, m => {
632632
negs.push(m.trim().slice(1));
633-
return ' ';
633+
return '';
634634
});
635635

636+
let negsRe;
637+
638+
if (negs.length > 0) {
639+
negsRe = new RegExp(negs.join('|'), 'i');
640+
641+
if (needle.trim() == '') {
642+
let idxs = [];
643+
644+
for (let i = 0; i < haystack.length; i++) {
645+
if (!negsRe.test(haystack[i]))
646+
idxs.push(i);
647+
}
648+
649+
return [idxs, null, null];
650+
}
651+
}
652+
636653
// console.log(negs);
654+
// console.log(needle);
637655

638656
if (outOfOrder) {
639657
// since uFuzzy is an AND-based search, we can iteratively pre-reduce the haystack by searching
@@ -691,10 +709,8 @@ function uFuzzy(opts) {
691709
let retInfo = null;
692710
let retOrder = null;
693711

694-
if (negs.length > 0) {
695-
let negsRe = new RegExp(negs.join('|'), 'i');
712+
if (negs.length > 0)
696713
matches = matches.map(idxs => idxs.filter(idx => !negsRe.test(haystack[idx])));
697-
}
698714

699715
let matchCount = matches.reduce((acc, idxs) => acc + idxs.length, 0);
700716

dist/uFuzzy.esm.js

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const cmp = new Intl.Collator('en').compare;
1111

1212
const inf = Infinity;
1313

14-
const NEGS_RE = /(?:\s|^)-[a-z\d]+/ig;
14+
const NEGS_RE = /(?:\s+|^)-[a-z\d]+/ig;
1515

1616
const OPTS = {
1717
// term segmentation & punct/whitespace merging
@@ -628,10 +628,28 @@ function uFuzzy(opts) {
628628

629629
needle = needle.replace(NEGS_RE, m => {
630630
negs.push(m.trim().slice(1));
631-
return ' ';
631+
return '';
632632
});
633633

634+
let negsRe;
635+
636+
if (negs.length > 0) {
637+
negsRe = new RegExp(negs.join('|'), 'i');
638+
639+
if (needle.trim() == '') {
640+
let idxs = [];
641+
642+
for (let i = 0; i < haystack.length; i++) {
643+
if (!negsRe.test(haystack[i]))
644+
idxs.push(i);
645+
}
646+
647+
return [idxs, null, null];
648+
}
649+
}
650+
634651
// console.log(negs);
652+
// console.log(needle);
635653

636654
if (outOfOrder) {
637655
// since uFuzzy is an AND-based search, we can iteratively pre-reduce the haystack by searching
@@ -689,10 +707,8 @@ function uFuzzy(opts) {
689707
let retInfo = null;
690708
let retOrder = null;
691709

692-
if (negs.length > 0) {
693-
let negsRe = new RegExp(negs.join('|'), 'i');
710+
if (negs.length > 0)
694711
matches = matches.map(idxs => idxs.filter(idx => !negsRe.test(haystack[idx])));
695-
}
696712

697713
let matchCount = matches.reduce((acc, idxs) => acc + idxs.length, 0);
698714

dist/uFuzzy.iife.js

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ var uFuzzy = (function () {
1414

1515
const inf = Infinity;
1616

17-
const NEGS_RE = /(?:\s|^)-[a-z\d]+/ig;
17+
const NEGS_RE = /(?:\s+|^)-[a-z\d]+/ig;
1818

1919
const OPTS = {
2020
// term segmentation & punct/whitespace merging
@@ -631,10 +631,28 @@ var uFuzzy = (function () {
631631

632632
needle = needle.replace(NEGS_RE, m => {
633633
negs.push(m.trim().slice(1));
634-
return ' ';
634+
return '';
635635
});
636636

637+
let negsRe;
638+
639+
if (negs.length > 0) {
640+
negsRe = new RegExp(negs.join('|'), 'i');
641+
642+
if (needle.trim() == '') {
643+
let idxs = [];
644+
645+
for (let i = 0; i < haystack.length; i++) {
646+
if (!negsRe.test(haystack[i]))
647+
idxs.push(i);
648+
}
649+
650+
return [idxs, null, null];
651+
}
652+
}
653+
637654
// console.log(negs);
655+
// console.log(needle);
638656

639657
if (outOfOrder) {
640658
// since uFuzzy is an AND-based search, we can iteratively pre-reduce the haystack by searching
@@ -692,10 +710,8 @@ var uFuzzy = (function () {
692710
let retInfo = null;
693711
let retOrder = null;
694712

695-
if (negs.length > 0) {
696-
let negsRe = new RegExp(negs.join('|'), 'i');
713+
if (negs.length > 0)
697714
matches = matches.map(idxs => idxs.filter(idx => !negsRe.test(haystack[idx])));
698-
}
699715

700716
let matchCount = matches.reduce((acc, idxs) => acc + idxs.length, 0);
701717

dist/uFuzzy.iife.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/uFuzzy.js

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const cmp = new Intl.Collator('en').compare;
44

55
const inf = Infinity;
66

7-
const NEGS_RE = /(?:\s|^)-[a-z\d]+/ig;
7+
const NEGS_RE = /(?:\s+|^)-[a-z\d]+/ig;
88

99
const OPTS = {
1010
// term segmentation & punct/whitespace merging
@@ -627,10 +627,28 @@ export default function uFuzzy(opts) {
627627

628628
needle = needle.replace(NEGS_RE, m => {
629629
negs.push(m.trim().slice(1));
630-
return ' ';
630+
return '';
631631
});
632632

633+
let negsRe;
634+
635+
if (negs.length > 0) {
636+
negsRe = new RegExp(negs.join('|'), 'i');
637+
638+
if (needle.trim() == '') {
639+
let idxs = [];
640+
641+
for (let i = 0; i < haystack.length; i++) {
642+
if (!negsRe.test(haystack[i]))
643+
idxs.push(i);
644+
}
645+
646+
return [idxs, null, null];
647+
}
648+
}
649+
633650
// console.log(negs);
651+
// console.log(needle);
634652

635653
if (outOfOrder) {
636654
// since uFuzzy is an AND-based search, we can iteratively pre-reduce the haystack by searching
@@ -688,10 +706,8 @@ export default function uFuzzy(opts) {
688706
let retInfo = null;
689707
let retOrder = null;
690708

691-
if (negs.length > 0) {
692-
let negsRe = new RegExp(negs.join('|'), 'i');
709+
if (negs.length > 0)
693710
matches = matches.map(idxs => idxs.filter(idx => !negsRe.test(haystack[idx])));
694-
}
695711

696712
let matchCount = matches.reduce((acc, idxs) => acc + idxs.length, 0);
697713

0 commit comments

Comments
 (0)