Skip to content

Commit 350283f

Browse files
committed
feat(extended): add ability to search actual exact string
This involves renaming of existing exact to be "include".
1 parent 6c76f52 commit 350283f

8 files changed

Lines changed: 770 additions & 83 deletions

File tree

dist/fuse.js

Lines changed: 61 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,21 +1158,11 @@
11581158
_createClass(ExactMatch, [{
11591159
key: "search",
11601160
value: function search(text) {
1161-
var location = 0;
1162-
var index;
1163-
var indices = [];
1164-
var patternLen = this.pattern.length; // Get all exact matches
1165-
1166-
while ((index = text.indexOf(this.pattern, location)) > -1) {
1167-
location = index + patternLen;
1168-
indices.push([index, location - 1]);
1169-
}
1170-
1171-
var isMatch = !!indices.length;
1161+
var isMatch = text === this.pattern;
11721162
return {
11731163
isMatch: isMatch,
1174-
score: isMatch ? 1 : 0,
1175-
indices: indices
1164+
score: isMatch ? 0 : 1,
1165+
indices: [0, this.pattern.length - 1]
11761166
};
11771167
}
11781168
}], [{
@@ -1183,12 +1173,12 @@
11831173
}, {
11841174
key: "multiRegex",
11851175
get: function get() {
1186-
return /^'"(.*)"$/;
1176+
return /^="(.*)"$/;
11871177
}
11881178
}, {
11891179
key: "singleRegex",
11901180
get: function get() {
1191-
return /^'(.*)$/;
1181+
return /^=(.*)$/;
11921182
}
11931183
}]);
11941184

@@ -1465,7 +1455,58 @@
14651455
return FuzzyMatch;
14661456
}(BaseMatch);
14671457

1468-
var searchers = [ExactMatch, PrefixExactMatch, InversePrefixExactMatch, InverseSuffixExactMatch, SuffixExactMatch, InverseExactMatch, FuzzyMatch];
1458+
var IncludeMatch = /*#__PURE__*/function (_BaseMatch) {
1459+
_inherits(IncludeMatch, _BaseMatch);
1460+
1461+
var _super = _createSuper(IncludeMatch);
1462+
1463+
function IncludeMatch(pattern) {
1464+
_classCallCheck(this, IncludeMatch);
1465+
1466+
return _super.call(this, pattern);
1467+
}
1468+
1469+
_createClass(IncludeMatch, [{
1470+
key: "search",
1471+
value: function search(text) {
1472+
var location = 0;
1473+
var index;
1474+
var indices = [];
1475+
var patternLen = this.pattern.length; // Get all exact matches
1476+
1477+
while ((index = text.indexOf(this.pattern, location)) > -1) {
1478+
location = index + patternLen;
1479+
indices.push([index, location - 1]);
1480+
}
1481+
1482+
var isMatch = !!indices.length;
1483+
return {
1484+
isMatch: isMatch,
1485+
score: isMatch ? 1 : 0,
1486+
indices: indices
1487+
};
1488+
}
1489+
}], [{
1490+
key: "type",
1491+
get: function get() {
1492+
return 'include';
1493+
}
1494+
}, {
1495+
key: "multiRegex",
1496+
get: function get() {
1497+
return /^'"(.*)"$/;
1498+
}
1499+
}, {
1500+
key: "singleRegex",
1501+
get: function get() {
1502+
return /^'(.*)$/;
1503+
}
1504+
}]);
1505+
1506+
return IncludeMatch;
1507+
}(BaseMatch);
1508+
1509+
var searchers = [ExactMatch, IncludeMatch, PrefixExactMatch, InversePrefixExactMatch, InverseSuffixExactMatch, SuffixExactMatch, InverseExactMatch, FuzzyMatch];
14691510
var searchersLen = searchers.length; // Regex to split by spaces, but keep anything in quotes together
14701511

14711512
var SPACE_RE = / +(?=([^\"]*\"[^\"]*\")*[^\"]*$)/;
@@ -1522,7 +1563,7 @@
15221563

15231564
// to a singl match
15241565

1525-
var MultiMatchSet = new Set([FuzzyMatch.type, ExactMatch.type]);
1566+
var MultiMatchSet = new Set([FuzzyMatch.type, IncludeMatch.type]);
15261567
/**
15271568
* Command-like searching
15281569
* ======================
@@ -1534,8 +1575,9 @@
15341575
*
15351576
* | Token | Match type | Description |
15361577
* | ----------- | -------------------------- | -------------------------------------- |
1537-
* | `jscript` | fuzzy-match | Items that match `jscript` |
1538-
* | `'python` | exact-match | Items that include `python` |
1578+
* | `jscript` | fuzzy-match | Items that fuzzy match `jscript` |
1579+
* | `=scheme` | exact-match | Items that are `scheme` |
1580+
* | `'python` | include-match | Items that include `python` |
15391581
* | `!ruby` | inverse-exact-match | Items that do not include `ruby` |
15401582
* | `^java` | prefix-exact-match | Items that start with `java` |
15411583
* | `!^earlang` | inverse-prefix-exact-match | Items that do not start with `earlang` |

docs/examples.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,8 +278,9 @@ White space acts as an **AND** operator, while a single pipe (`|`) character act
278278

279279
| Token | Match type | Description |
280280
| ----------- | -------------------------- | -------------------------------------- |
281-
| `jscript` | fuzzy-match | Items that match `jscript` |
282-
| `'python` | exact-match | Items that include `python` |
281+
| `jscript` | fuzzy-match | Items that fuzzy match `jscript` |
282+
| `=scheme` | exact-match | Items that are `scheme` |
283+
| `'python` | include-match | Items that include `python` |
283284
| `!ruby` | inverse-exact-match | Items that do not include `ruby` |
284285
| `^java` | prefix-exact-match | Items that start with `java` |
285286
| `!^earlang` | inverse-prefix-exact-match | Items that do not start with `earlang` |

src/search/extended/ExactMatch.js

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Token: 'file
22
// Match type: exact-match
3-
// Description: Items that include `file`
3+
// Description: Items that are `file`
44

55
import BaseMatch from './BaseMatch'
66

@@ -12,30 +12,18 @@ export default class ExactMatch extends BaseMatch {
1212
return 'exact'
1313
}
1414
static get multiRegex() {
15-
return /^'"(.*)"$/
15+
return /^="(.*)"$/
1616
}
1717
static get singleRegex() {
18-
return /^'(.*)$/
18+
return /^=(.*)$/
1919
}
2020
search(text) {
21-
let location = 0
22-
let index
23-
24-
const indices = []
25-
const patternLen = this.pattern.length
26-
27-
// Get all exact matches
28-
while ((index = text.indexOf(this.pattern, location)) > -1) {
29-
location = index + patternLen
30-
indices.push([index, location - 1])
31-
}
32-
33-
const isMatch = !!indices.length
21+
const isMatch = text === this.pattern
3422

3523
return {
3624
isMatch,
37-
score: isMatch ? 1 : 0,
38-
indices
25+
score: isMatch ? 0 : 1,
26+
indices: [0, this.pattern.length - 1]
3927
}
4028
}
4129
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Token: 'file
2+
// Match type: exact-match
3+
// Description: Items that include `file`
4+
5+
import BaseMatch from './BaseMatch'
6+
7+
export default class IncludeMatch extends BaseMatch {
8+
constructor(pattern) {
9+
super(pattern)
10+
}
11+
static get type() {
12+
return 'include'
13+
}
14+
static get multiRegex() {
15+
return /^'"(.*)"$/
16+
}
17+
static get singleRegex() {
18+
return /^'(.*)$/
19+
}
20+
search(text) {
21+
let location = 0
22+
let index
23+
24+
const indices = []
25+
const patternLen = this.pattern.length
26+
27+
// Get all exact matches
28+
while ((index = text.indexOf(this.pattern, location)) > -1) {
29+
location = index + patternLen
30+
indices.push([index, location - 1])
31+
}
32+
33+
const isMatch = !!indices.length
34+
35+
return {
36+
isMatch,
37+
score: isMatch ? 1 : 0,
38+
indices
39+
}
40+
}
41+
}

src/search/extended/index.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import parseQuery from './parseQuery'
22
import FuzzyMatch from './FuzzyMatch'
3-
import ExactMatch from './ExactMatch'
3+
import IncludeMatch from './IncludeMatch'
44
import Config from '../../core/config'
55

66
// These extended matchers can return an array of matches, as opposed
77
// to a singl match
8-
const MultiMatchSet = new Set([FuzzyMatch.type, ExactMatch.type])
8+
const MultiMatchSet = new Set([FuzzyMatch.type, IncludeMatch.type])
99

1010
/**
1111
* Command-like searching
@@ -18,8 +18,9 @@ const MultiMatchSet = new Set([FuzzyMatch.type, ExactMatch.type])
1818
*
1919
* | Token | Match type | Description |
2020
* | ----------- | -------------------------- | -------------------------------------- |
21-
* | `jscript` | fuzzy-match | Items that match `jscript` |
22-
* | `'python` | exact-match | Items that include `python` |
21+
* | `jscript` | fuzzy-match | Items that fuzzy match `jscript` |
22+
* | `=scheme` | exact-match | Items that are `scheme` |
23+
* | `'python` | include-match | Items that include `python` |
2324
* | `!ruby` | inverse-exact-match | Items that do not include `ruby` |
2425
* | `^java` | prefix-exact-match | Items that start with `java` |
2526
* | `!^earlang` | inverse-prefix-exact-match | Items that do not start with `earlang` |

src/search/extended/parseQuery.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ import InversePrefixExactMatch from './InversePrefixExactMatch'
55
import SuffixExactMatch from './SuffixExactMatch'
66
import InverseSuffixExactMatch from './InverseSuffixExactMatch'
77
import FuzzyMatch from './FuzzyMatch'
8+
import IncludeMatch from './IncludeMatch'
89

910
// ❗Order is important. DO NOT CHANGE.
1011
const searchers = [
1112
ExactMatch,
13+
IncludeMatch,
1214
PrefixExactMatch,
1315
InversePrefixExactMatch,
1416
InverseSuffixExactMatch,

0 commit comments

Comments
 (0)