Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
krisk committed Jun 17, 2020
1 parent da3e894 commit 9136c6e
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 109 deletions.
2 changes: 1 addition & 1 deletion dist/fuse.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Type definitions for Fuse.js v6.2.0-beta.1
// Type definitions for Fuse.js v6.2.0-beta.0
// TypeScript v3.9.5

export default Fuse
Expand Down
26 changes: 6 additions & 20 deletions dist/fuse.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Fuse.js v6.2.0-beta.1 - Lightweight fuzzy-search (http://fusejs.io)
* Fuse.js v6.2.0-beta.0 - Lightweight fuzzy-search (http://fusejs.io)
*
* Copyright (c) 2020 Kiro Risk (http://kiro.me)
* All Rights Reserved. Apache Software License 2.0
Expand Down Expand Up @@ -422,10 +422,7 @@
// When `true`, the calculation for the relevance score (used for sorting) will
// ignore the field-length norm.
// More info: https://fusejs.io/concepts/scoring-theory.html#field-length-norm
ignoreFieldNorm: false,
// When `true`, if the search query is empty, return the whole list instead
// of an empty array.
matchEmptyQuery: false
ignoreFieldNorm: false
};
var Config = _objectSpread2({}, BasicOptions, {}, MatchOptions, {}, FuzzyOptions, {}, AdvancedOptions);

Expand Down Expand Up @@ -633,7 +630,8 @@
var myIndex = new FuseIndex({
getFn: getFn
});
myIndex.setKeys(keys);
var keyStore = new KeyStore(keys);
myIndex.setKeys(keyStore.keys());
myIndex.setSources(docs);
myIndex.create();
return myIndex;
Expand Down Expand Up @@ -1827,19 +1825,7 @@
includeScore = _this$options.includeScore,
shouldSort = _this$options.shouldSort,
sortFn = _this$options.sortFn,
ignoreFieldNorm = _this$options.ignoreFieldNorm,
matchEmptyQuery = _this$options.matchEmptyQuery;

if (matchEmptyQuery && (!isDefined(query) || isBlank(query))) {
return this._docs.map(function (doc, idx) {
return {
item: doc,
score: 1,
refIndex: idx
};
});
}

ignoreFieldNorm = _this$options.ignoreFieldNorm;
var results = isString(query) ? isString(this._docs[0]) ? this._searchStringList(query) : this._searchObjectList(query) : this._searchLogical(query);
computeScore$1(results, this._keyStore, {
ignoreFieldNorm: ignoreFieldNorm
Expand Down Expand Up @@ -2122,7 +2108,7 @@
});
}

Fuse.version = '6.2.0-beta.1';
Fuse.version = '6.2.0-beta.0';
Fuse.createIndex = createIndex;
Fuse.parseIndex = parseIndex;
Fuse.config = Config;
Expand Down
7 changes: 0 additions & 7 deletions docs/api/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,3 @@ When `true`, the calculation for the relevance score (used for sorting) will ign
:::tip
The only time it might make sense `ignoreFieldNorm` to `false` is when it does not matter how many terms there are, but only that the query term exists.
:::

### `matchEmptyQuery` <Badge text="experimental" type="warning"/>

- Type: `boolean`
- Default: `false`

When `true`, if the search query is empty, return the whole list instead of an empty array.
5 changes: 1 addition & 4 deletions src/core/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,7 @@ export const AdvancedOptions = {
// When `true`, the calculation for the relevance score (used for sorting) will
// ignore the field-length norm.
// More info: https://fusejs.io/concepts/scoring-theory.html#field-length-norm
ignoreFieldNorm: false,
// When `true`, if the search query is empty, return the whole list instead
// of an empty array.
matchEmptyQuery: false
ignoreFieldNorm: false
}

export default {
Expand Down
19 changes: 2 additions & 17 deletions src/core/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
import {
isArray,
isBlank,
isDefined,
isString,
isNumber
} from '../helpers/types'
import { isArray, isDefined, isString, isNumber } from '../helpers/types'
import KeyStore from '../tools/KeyStore'
import FuseIndex, { createIndex } from '../tools/FuseIndex'
import { transformMatches, transformScore } from '../transform'
Expand Down Expand Up @@ -83,18 +77,9 @@ export default class Fuse {
includeScore,
shouldSort,
sortFn,
ignoreFieldNorm,
matchEmptyQuery
ignoreFieldNorm
} = this.options

if (matchEmptyQuery && (!isDefined(query) || isBlank(query))) {
return this._docs.map((doc, idx) => ({
item: doc,
score: 1,
refIndex: idx
}))
}

let results = isString(query)
? isString(this._docs[0])
? this._searchStringList(query)
Expand Down
8 changes: 5 additions & 3 deletions src/tools/FuseIndex.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { isArray, isDefined, isString, isBlank } from '../helpers/types'
import Config from '../core/config'
import normGenerator from './norm'
import KeyStore from './KeyStore'

export default class FuseIndex {
constructor({ getFn = Config.getFn } = {}) {
Expand Down Expand Up @@ -139,16 +140,17 @@ export default class FuseIndex {
}

export function createIndex(keys, docs, { getFn = Config.getFn } = {}) {
let myIndex = new FuseIndex({ getFn })
myIndex.setKeys(keys)
const myIndex = new FuseIndex({ getFn })
const keyStore = new KeyStore(keys)
myIndex.setKeys(keyStore.keys())
myIndex.setSources(docs)
myIndex.create()
return myIndex
}

export function parseIndex(data, { getFn = Config.getFn } = {}) {
const { keys, records } = data
let myIndex = new FuseIndex({ getFn })
const myIndex = new FuseIndex({ getFn })
myIndex.setKeys(keys)
myIndex.setIndexRecords(records)
return myIndex
Expand Down
29 changes: 0 additions & 29 deletions test/__snapshots__/fuzzy-search.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -74,32 +74,3 @@ Array [
},
]
`;

exports[`Returning all results when query is empty The entry with the shorter field length appears first 1`] = `
Array [
Object {
"item": Object {
"ISBN": "0312696957",
"author": "Steve Hamilton",
"tags": Array [
"fiction war hello no way",
],
"title": "The Lock war Artist nonficon",
},
"refIndex": 0,
"score": 1,
},
Object {
"item": Object {
"ISBN": "0765348276",
"author": "John Scalzi",
"tags": Array [
"fiction no",
],
"title": "Old Man's War",
},
"refIndex": 1,
"score": 1,
},
]
`;
28 changes: 0 additions & 28 deletions test/fuzzy-search.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1005,34 +1005,6 @@ describe('Searching taking into account field length', () => {
})
})

describe('Returning all results when query is empty', () => {
const list = [
{
ISBN: '0312696957',
title: 'The Lock war Artist nonficon',
author: 'Steve Hamilton',
tags: ['fiction war hello no way']
},
{
ISBN: '0765348276',
title: "Old Man's War",
author: 'John Scalzi',
tags: ['fiction no']
}
]

test('The entry with the shorter field length appears first', () => {
const fuse = new Fuse(list, {
keys: ['title'],
matchEmptyQuery: true
})

let result = fuse.search('')

expect(result).toMatchSnapshot()
})
})

describe('Ignore location and field length norm', () => {
const list = [
'beforeEach',
Expand Down
9 changes: 9 additions & 0 deletions test/indexing.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ describe('Searching', () => {
expect(myIndex.keys).toBeDefined()
})

test('createIndex: ensure keys can be created with objects', () => {
let myIndex = Fuse.createIndex(
[{ name: 'title' }, { name: 'author.firstName' }],
Books
)
expect(myIndex.records).toBeDefined()
expect(myIndex.keys).toBeDefined()
})

test('parseIndex: ensure index can be exported and Fuse can be initialized', () => {
const myIndex = Fuse.createIndex(options.keys, Books)
expect(myIndex.size()).toBe(Books.length)
Expand Down

0 comments on commit 9136c6e

Please sign in to comment.