Skip to content

Commit 1d445b9

Browse files
fakenickelskrisk
authored andcommitted
feat: allow passing getFn for a specific key
Closes #627
1 parent 9d7fc44 commit 1d445b9

14 files changed

Lines changed: 94 additions & 38 deletions

dist/fuse.basic.common.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* Fuse.js v6.5.3 - Lightweight fuzzy-search (http://fusejs.io)
33
*
4-
* Copyright (c) 2021 Kiro Risk (http://kiro.me)
4+
* Copyright (c) 2022 Kiro Risk (http://kiro.me)
55
* All Rights Reserved. Apache Software License 2.0
66
*
77
* http://www.apache.org/licenses/LICENSE-2.0
@@ -230,6 +230,7 @@ function createKey(key) {
230230
var id = null;
231231
var src = null;
232232
var weight = 1;
233+
var getFn = null;
233234

234235
if (isString(key) || isArray(key)) {
235236
src = key;
@@ -253,13 +254,15 @@ function createKey(key) {
253254

254255
path = createKeyPath(name);
255256
id = createKeyId(name);
257+
getFn = key.getFn;
256258
}
257259

258260
return {
259261
path: path,
260262
id: id,
261263
weight: weight,
262-
src: src
264+
src: src,
265+
getFn: getFn
263266
};
264267
}
265268
function createKeyPath(key) {
@@ -519,8 +522,7 @@ var FuseIndex = /*#__PURE__*/function () {
519522
}; // Iterate over every key (i.e, path), and fetch the value at that key
520523

521524
this.keys.forEach(function (key, keyIndex) {
522-
// console.log(key)
523-
var value = _this3.getFn(doc, key.path);
525+
var value = key.getFn ? key.getFn(doc) : _this3.getFn(doc, key.path);
524526

525527
if (!isDefined(value)) {
526528
return;

dist/fuse.basic.esm.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* Fuse.js v6.5.3 - Lightweight fuzzy-search (http://fusejs.io)
33
*
4-
* Copyright (c) 2021 Kiro Risk (http://kiro.me)
4+
* Copyright (c) 2022 Kiro Risk (http://kiro.me)
55
* All Rights Reserved. Apache Software License 2.0
66
*
77
* http://www.apache.org/licenses/LICENSE-2.0
@@ -130,6 +130,7 @@ function createKey(key) {
130130
let id = null;
131131
let src = null;
132132
let weight = 1;
133+
let getFn = null;
133134

134135
if (isString(key) || isArray(key)) {
135136
src = key;
@@ -153,9 +154,10 @@ function createKey(key) {
153154

154155
path = createKeyPath(name);
155156
id = createKeyId(name);
157+
getFn = key.getFn;
156158
}
157159

158-
return { path, id, weight, src }
160+
return { path, id, weight, src, getFn }
159161
}
160162

161163
function createKeyPath(key) {
@@ -398,8 +400,7 @@ class FuseIndex {
398400

399401
// Iterate over every key (i.e, path), and fetch the value at that key
400402
this.keys.forEach((key, keyIndex) => {
401-
// console.log(key)
402-
let value = this.getFn(doc, key.path);
403+
let value = key.getFn ? key.getFn(doc) : this.getFn(doc, key.path);
403404

404405
if (!isDefined(value)) {
405406
return

dist/fuse.basic.esm.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/fuse.basic.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* Fuse.js v6.5.3 - Lightweight fuzzy-search (http://fusejs.io)
33
*
4-
* Copyright (c) 2021 Kiro Risk (http://kiro.me)
4+
* Copyright (c) 2022 Kiro Risk (http://kiro.me)
55
* All Rights Reserved. Apache Software License 2.0
66
*
77
* http://www.apache.org/licenses/LICENSE-2.0
@@ -234,6 +234,7 @@
234234
var id = null;
235235
var src = null;
236236
var weight = 1;
237+
var getFn = null;
237238

238239
if (isString(key) || isArray(key)) {
239240
src = key;
@@ -257,13 +258,15 @@
257258

258259
path = createKeyPath(name);
259260
id = createKeyId(name);
261+
getFn = key.getFn;
260262
}
261263

262264
return {
263265
path: path,
264266
id: id,
265267
weight: weight,
266-
src: src
268+
src: src,
269+
getFn: getFn
267270
};
268271
}
269272
function createKeyPath(key) {
@@ -523,8 +526,7 @@
523526
}; // Iterate over every key (i.e, path), and fetch the value at that key
524527

525528
this.keys.forEach(function (key, keyIndex) {
526-
// console.log(key)
527-
var value = _this3.getFn(doc, key.path);
529+
var value = key.getFn ? key.getFn(doc) : _this3.getFn(doc, key.path);
528530

529531
if (!isDefined(value)) {
530532
return;

dist/fuse.basic.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/fuse.common.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* Fuse.js v6.5.3 - Lightweight fuzzy-search (http://fusejs.io)
33
*
4-
* Copyright (c) 2021 Kiro Risk (http://kiro.me)
4+
* Copyright (c) 2022 Kiro Risk (http://kiro.me)
55
* All Rights Reserved. Apache Software License 2.0
66
*
77
* http://www.apache.org/licenses/LICENSE-2.0
@@ -313,6 +313,7 @@ function createKey(key) {
313313
var id = null;
314314
var src = null;
315315
var weight = 1;
316+
var getFn = null;
316317

317318
if (isString(key) || isArray(key)) {
318319
src = key;
@@ -336,13 +337,15 @@ function createKey(key) {
336337

337338
path = createKeyPath(name);
338339
id = createKeyId(name);
340+
getFn = key.getFn;
339341
}
340342

341343
return {
342344
path: path,
343345
id: id,
344346
weight: weight,
345-
src: src
347+
src: src,
348+
getFn: getFn
346349
};
347350
}
348351
function createKeyPath(key) {
@@ -602,8 +605,7 @@ var FuseIndex = /*#__PURE__*/function () {
602605
}; // Iterate over every key (i.e, path), and fetch the value at that key
603606

604607
this.keys.forEach(function (key, keyIndex) {
605-
// console.log(key)
606-
var value = _this3.getFn(doc, key.path);
608+
var value = key.getFn ? key.getFn(doc) : _this3.getFn(doc, key.path);
607609

608610
if (!isDefined(value)) {
609611
return;
@@ -1500,7 +1502,7 @@ var IncludeMatch = /*#__PURE__*/function (_BaseMatch) {
15001502
var searchers = [ExactMatch, IncludeMatch, PrefixExactMatch, InversePrefixExactMatch, InverseSuffixExactMatch, SuffixExactMatch, InverseExactMatch, FuzzyMatch];
15011503
var searchersLen = searchers.length; // Regex to split by spaces, but keep anything in quotes together
15021504

1503-
var SPACE_RE = / +(?=([^\"]*\"[^\"]*\")*[^\"]*$)/;
1505+
var SPACE_RE = / +(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)/;
15041506
var OR_TOKEN = '|'; // Return a 2D array representation of the query, for simpler parsing.
15051507
// Example:
15061508
// "^core go$ | rb$ | py$ xy$" => [["^core", "go$"], ["rb$"], ["py$", "xy$"]]

dist/fuse.d.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,20 +242,37 @@ declare namespace Fuse {
242242
export type FuseOptionKey = FuseOptionKeyObject | string | string[]
243243

244244
export interface IFuseOptions<T> {
245+
/** Indicates whether comparisons should be case sensitive. */
245246
isCaseSensitive?: boolean
247+
/** Determines how close the match must be to the fuzzy location (specified by `location`). An exact letter match which is `distance` characters away from the fuzzy location would score as a complete mismatch. A `distance` of `0` requires the match be at the exact `location` specified. A distance of `1000` would require a perfect match to be within `800` characters of the `location` to be found using a `threshold` of `0.8`. */
246248
distance?: number
249+
/** When true, the matching function will continue to the end of a search pattern even if a perfect match has already been located in the string. */
247250
findAllMatches?: boolean
251+
/** The function to use to retrieve an object's value at the provided path. The default will also search nested paths. */
248252
getFn?: FuseGetFunction<T>
253+
/** When `true`, search will ignore `location` and `distance`, so it won't matter where in the string the pattern appears. */
249254
ignoreLocation?: boolean
255+
/** When `true`, the calculation for the relevance score (used for sorting) will ignore the `field-length norm`. */
250256
ignoreFieldNorm?: boolean
257+
/** Determines how much the `field-length norm` affects scoring. A value of `0` is equivalent to ignoring the field-length norm. A value of `0.5` will greatly reduce the effect of field-length norm, while a value of `2.0` will greatly increase it. */
258+
fieldNormWeight: number
259+
/** Whether the matches should be included in the result set. When `true`, each record in the result set will include the indices of the matched characters. These can consequently be used for highlighting purposes. */
251260
includeMatches?: boolean
261+
/** Whether the score should be included in the result set. A score of `0`indicates a perfect match, while a score of `1` indicates a complete mismatch. */
252262
includeScore?: boolean
263+
/** List of keys that will be searched. This supports nested paths, weighted search, searching in arrays of `strings` and `objects`. */
253264
keys?: Array<FuseOptionKey>
265+
/** Determines approximately where in the text is the pattern expected to be found. */
254266
location?: number
267+
/** Only the matches whose length exceeds this value will be returned. (For instance, if you want to ignore single character matches in the result, set it to `2`). */
255268
minMatchCharLength?: number
269+
/** Whether to sort the result list, by score. */
256270
shouldSort?: boolean
271+
/** The function to use to sort all the results. The default will sort by ascending relevance score, ascending index. */
257272
sortFn?: FuseSortFunction
273+
/** At what point does the match algorithm give up. A threshold of `0.0` requires a perfect match (of both letters and location), a threshold of `1.0` would match anything. */
258274
threshold?: number
275+
/** When `true`, it enables the use of unix-like search commands. See [example](/examples.html#extended-search). */
259276
useExtendedSearch?: boolean
260277
}
261278

dist/fuse.esm.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* Fuse.js v6.5.3 - Lightweight fuzzy-search (http://fusejs.io)
33
*
4-
* Copyright (c) 2021 Kiro Risk (http://kiro.me)
4+
* Copyright (c) 2022 Kiro Risk (http://kiro.me)
55
* All Rights Reserved. Apache Software License 2.0
66
*
77
* http://www.apache.org/licenses/LICENSE-2.0
@@ -128,6 +128,7 @@ function createKey(key) {
128128
let id = null;
129129
let src = null;
130130
let weight = 1;
131+
let getFn = null;
131132

132133
if (isString(key) || isArray(key)) {
133134
src = key;
@@ -151,9 +152,10 @@ function createKey(key) {
151152

152153
path = createKeyPath(name);
153154
id = createKeyId(name);
155+
getFn = key.getFn;
154156
}
155157

156-
return { path, id, weight, src }
158+
return { path, id, weight, src, getFn }
157159
}
158160

159161
function createKeyPath(key) {
@@ -396,8 +398,7 @@ class FuseIndex {
396398

397399
// Iterate over every key (i.e, path), and fetch the value at that key
398400
this.keys.forEach((key, keyIndex) => {
399-
// console.log(key)
400-
let value = this.getFn(doc, key.path);
401+
let value = key.getFn ? key.getFn(doc) : this.getFn(doc, key.path);
401402

402403
if (!isDefined(value)) {
403404
return
@@ -1127,7 +1128,7 @@ const searchers = [
11271128
const searchersLen = searchers.length;
11281129

11291130
// Regex to split by spaces, but keep anything in quotes together
1130-
const SPACE_RE = / +(?=([^\"]*\"[^\"]*\")*[^\"]*$)/;
1131+
const SPACE_RE = / +(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)/;
11311132
const OR_TOKEN = '|';
11321133

11331134
// Return a 2D array representation of the query, for simpler parsing.

0 commit comments

Comments
 (0)