Skip to content

Commit

Permalink
Merge pull request #2 from Tubitv/tubi/feat/return-score-for-each-match
Browse files Browse the repository at this point in the history
include score in each match
  • Loading branch information
springuper authored Dec 5, 2022
2 parents 206442c + 4a7a637 commit 29d9190
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 7 deletions.
4 changes: 3 additions & 1 deletion src/core/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ export const MatchOptions = {
// set will include the indices of the matched characters.
// These can consequently be used for highlighting purposes.
includeMatches: false,
// When true, every match will include its score
includeMatchScore: false,
// 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: false,
Expand All @@ -16,7 +18,7 @@ export const BasicOptions = {
// When `true`, the algorithm continues searching to the end of the input even if a perfect
// match is found before the end of the same input.
isCaseSensitive: false,
// When true, the matching function will continue to the end of a search pattern even if
// When true, each record in the result set will incude its match score
includeScore: false,
// List of properties that will be searched. This also supports nested properties.
keys: [],
Expand Down
7 changes: 5 additions & 2 deletions src/core/format.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import Config from './config'
import { transformMatches, transformScore } from '../transform'
import { transformMatches, transformMatchScore, transformScore } from '../transform'

export default function format(
results,
docs,
{
includeMatches = Config.includeMatches,
includeMatchScore = false,
includeScore = Config.includeScore
} = {}
) {
const transformers = []

if (includeMatches) transformers.push(transformMatches)
if (includeMatches) {
transformers.push(includeMatchScore ? transformMatchScore : transformMatches)
}
if (includeScore) transformers.push(transformScore)

return results.map((result) => {
Expand Down
4 changes: 3 additions & 1 deletion src/core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export default class Fuse {
search(query, { limit = -1, keys = [] } = {}) {
const {
includeMatches,
includeMatchScore,
includeScore,
shouldSort,
sortFn,
Expand All @@ -101,7 +102,8 @@ export default class Fuse {

return format(results, this._docs, {
includeMatches,
includeScore
includeScore,
includeMatchScore
})
}

Expand Down
2 changes: 1 addition & 1 deletion src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,8 +302,8 @@ declare namespace Fuse {
}

export type FuseSearchOptions = {
limit?: number
keys?: string[]
limit?: number
}

export type FuseResult<T> = {
Expand Down
3 changes: 2 additions & 1 deletion src/transform/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import transformMatches from './transformMatches'
import transformMatchScore from './transformMatchScore'
import transformScore from './transformScore'

export { transformMatches, transformScore }
export { transformMatches, transformMatchScore, transformScore }
34 changes: 34 additions & 0 deletions src/transform/transformMatchScore.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { isDefined } from '../helpers/types'

export default function transformMatchScore(result, data) {
const matches = result.matches
data.matches = []

if (!isDefined(matches)) {
return
}

matches.forEach((match) => {
if (!isDefined(match.indices) || !match.indices.length) {
return
}

const { indices, value, score } = match

let obj = {
indices,
value,
score
}

if (match.key) {
obj.key = match.key.src
}

if (match.idx > -1) {
obj.refIndex = match.idx
}

data.matches.push(obj)
})
}
44 changes: 43 additions & 1 deletion test/fuzzy-search.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -772,6 +772,48 @@ describe('Searching with findAllMatches', () => {
})
})

describe('Searching with includeMatchScore', () => {
const customBookList = [
{
title: "Old Man's War fiction",
author: 'John X',
tags: ['war']
},
{
title: 'Right Ho Jeeves',
author: 'P.D. Mans',
tags: ['fiction', 'war']
},
{
title: 'The life of Jane',
author: 'John Smith',
tags: ['john', 'smith']
},
{
title: 'John Smith',
author: 'Steve Pearson',
tags: ['steve', 'pearson']
}
]
let fuse

beforeEach(
() =>
(fuse = new Fuse(customBookList, {
keys: ['title', 'author'],
includeMatches: true,
includeMatchScore: true,
findAllMatches: true
}))
)

test('Each match in every result item should have a score property', () => {
const result = fuse.search('life')
expect(result[0].matches[0]).toHaveProperty('score')
})
})


describe('Searching with minCharLength', () => {
const customList = ['t te tes test tes te t']
let fuse
Expand Down Expand Up @@ -1067,7 +1109,7 @@ describe('Searching taking into account field length', () => {
})
})

describe.only('Searching with custom keys', () => {
describe('Searching with custom keys', () => {
const customBookList = [
{
title: "Old Man's War",
Expand Down

0 comments on commit 29d9190

Please sign in to comment.