Skip to content

Commit

Permalink
feat: allow passing getFn for a specific key
Browse files Browse the repository at this point in the history
Closes #627
  • Loading branch information
fakenickels authored and krisk committed May 3, 2022
1 parent 9d7fc44 commit 1d445b9
Show file tree
Hide file tree
Showing 14 changed files with 94 additions and 38 deletions.
10 changes: 6 additions & 4 deletions dist/fuse.basic.common.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Fuse.js v6.5.3 - Lightweight fuzzy-search (http://fusejs.io)
*
* Copyright (c) 2021 Kiro Risk (http://kiro.me)
* Copyright (c) 2022 Kiro Risk (http://kiro.me)
* All Rights Reserved. Apache Software License 2.0
*
* http://www.apache.org/licenses/LICENSE-2.0
Expand Down Expand Up @@ -230,6 +230,7 @@ function createKey(key) {
var id = null;
var src = null;
var weight = 1;
var getFn = null;

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

path = createKeyPath(name);
id = createKeyId(name);
getFn = key.getFn;
}

return {
path: path,
id: id,
weight: weight,
src: src
src: src,
getFn: getFn
};
}
function createKeyPath(key) {
Expand Down Expand Up @@ -519,8 +522,7 @@ var FuseIndex = /*#__PURE__*/function () {
}; // Iterate over every key (i.e, path), and fetch the value at that key

this.keys.forEach(function (key, keyIndex) {
// console.log(key)
var value = _this3.getFn(doc, key.path);
var value = key.getFn ? key.getFn(doc) : _this3.getFn(doc, key.path);

if (!isDefined(value)) {
return;
Expand Down
9 changes: 5 additions & 4 deletions dist/fuse.basic.esm.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Fuse.js v6.5.3 - Lightweight fuzzy-search (http://fusejs.io)
*
* Copyright (c) 2021 Kiro Risk (http://kiro.me)
* Copyright (c) 2022 Kiro Risk (http://kiro.me)
* All Rights Reserved. Apache Software License 2.0
*
* http://www.apache.org/licenses/LICENSE-2.0
Expand Down Expand Up @@ -130,6 +130,7 @@ function createKey(key) {
let id = null;
let src = null;
let weight = 1;
let getFn = null;

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

path = createKeyPath(name);
id = createKeyId(name);
getFn = key.getFn;
}

return { path, id, weight, src }
return { path, id, weight, src, getFn }
}

function createKeyPath(key) {
Expand Down Expand Up @@ -398,8 +400,7 @@ class FuseIndex {

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

if (!isDefined(value)) {
return
Expand Down
4 changes: 2 additions & 2 deletions dist/fuse.basic.esm.min.js

Large diffs are not rendered by default.

10 changes: 6 additions & 4 deletions dist/fuse.basic.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Fuse.js v6.5.3 - Lightweight fuzzy-search (http://fusejs.io)
*
* Copyright (c) 2021 Kiro Risk (http://kiro.me)
* Copyright (c) 2022 Kiro Risk (http://kiro.me)
* All Rights Reserved. Apache Software License 2.0
*
* http://www.apache.org/licenses/LICENSE-2.0
Expand Down Expand Up @@ -234,6 +234,7 @@
var id = null;
var src = null;
var weight = 1;
var getFn = null;

if (isString(key) || isArray(key)) {
src = key;
Expand All @@ -257,13 +258,15 @@

path = createKeyPath(name);
id = createKeyId(name);
getFn = key.getFn;
}

return {
path: path,
id: id,
weight: weight,
src: src
src: src,
getFn: getFn
};
}
function createKeyPath(key) {
Expand Down Expand Up @@ -523,8 +526,7 @@
}; // Iterate over every key (i.e, path), and fetch the value at that key

this.keys.forEach(function (key, keyIndex) {
// console.log(key)
var value = _this3.getFn(doc, key.path);
var value = key.getFn ? key.getFn(doc) : _this3.getFn(doc, key.path);

if (!isDefined(value)) {
return;
Expand Down
4 changes: 2 additions & 2 deletions dist/fuse.basic.min.js

Large diffs are not rendered by default.

12 changes: 7 additions & 5 deletions dist/fuse.common.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Fuse.js v6.5.3 - Lightweight fuzzy-search (http://fusejs.io)
*
* Copyright (c) 2021 Kiro Risk (http://kiro.me)
* Copyright (c) 2022 Kiro Risk (http://kiro.me)
* All Rights Reserved. Apache Software License 2.0
*
* http://www.apache.org/licenses/LICENSE-2.0
Expand Down Expand Up @@ -313,6 +313,7 @@ function createKey(key) {
var id = null;
var src = null;
var weight = 1;
var getFn = null;

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

path = createKeyPath(name);
id = createKeyId(name);
getFn = key.getFn;
}

return {
path: path,
id: id,
weight: weight,
src: src
src: src,
getFn: getFn
};
}
function createKeyPath(key) {
Expand Down Expand Up @@ -602,8 +605,7 @@ var FuseIndex = /*#__PURE__*/function () {
}; // Iterate over every key (i.e, path), and fetch the value at that key

this.keys.forEach(function (key, keyIndex) {
// console.log(key)
var value = _this3.getFn(doc, key.path);
var value = key.getFn ? key.getFn(doc) : _this3.getFn(doc, key.path);

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

var SPACE_RE = / +(?=([^\"]*\"[^\"]*\")*[^\"]*$)/;
var SPACE_RE = / +(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)/;
var OR_TOKEN = '|'; // Return a 2D array representation of the query, for simpler parsing.
// Example:
// "^core go$ | rb$ | py$ xy$" => [["^core", "go$"], ["rb$"], ["py$", "xy$"]]
Expand Down
17 changes: 17 additions & 0 deletions dist/fuse.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,20 +242,37 @@ declare namespace Fuse {
export type FuseOptionKey = FuseOptionKeyObject | string | string[]

export interface IFuseOptions<T> {
/** Indicates whether comparisons should be case sensitive. */
isCaseSensitive?: boolean
/** 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`. */
distance?: number
/** 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. */
findAllMatches?: boolean
/** The function to use to retrieve an object's value at the provided path. The default will also search nested paths. */
getFn?: FuseGetFunction<T>
/** When `true`, search will ignore `location` and `distance`, so it won't matter where in the string the pattern appears. */
ignoreLocation?: boolean
/** When `true`, the calculation for the relevance score (used for sorting) will ignore the `field-length norm`. */
ignoreFieldNorm?: boolean
/** 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. */
fieldNormWeight: number
/** 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. */
includeMatches?: boolean
/** 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. */
includeScore?: boolean
/** List of keys that will be searched. This supports nested paths, weighted search, searching in arrays of `strings` and `objects`. */
keys?: Array<FuseOptionKey>
/** Determines approximately where in the text is the pattern expected to be found. */
location?: number
/** 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`). */
minMatchCharLength?: number
/** Whether to sort the result list, by score. */
shouldSort?: boolean
/** The function to use to sort all the results. The default will sort by ascending relevance score, ascending index. */
sortFn?: FuseSortFunction
/** 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. */
threshold?: number
/** When `true`, it enables the use of unix-like search commands. See [example](/examples.html#extended-search). */
useExtendedSearch?: boolean
}

Expand Down
11 changes: 6 additions & 5 deletions dist/fuse.esm.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Fuse.js v6.5.3 - Lightweight fuzzy-search (http://fusejs.io)
*
* Copyright (c) 2021 Kiro Risk (http://kiro.me)
* Copyright (c) 2022 Kiro Risk (http://kiro.me)
* All Rights Reserved. Apache Software License 2.0
*
* http://www.apache.org/licenses/LICENSE-2.0
Expand Down Expand Up @@ -128,6 +128,7 @@ function createKey(key) {
let id = null;
let src = null;
let weight = 1;
let getFn = null;

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

path = createKeyPath(name);
id = createKeyId(name);
getFn = key.getFn;
}

return { path, id, weight, src }
return { path, id, weight, src, getFn }
}

function createKeyPath(key) {
Expand Down Expand Up @@ -396,8 +398,7 @@ class FuseIndex {

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

if (!isDefined(value)) {
return
Expand Down Expand Up @@ -1127,7 +1128,7 @@ const searchers = [
const searchersLen = searchers.length;

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

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

0 comments on commit 1d445b9

Please sign in to comment.