diff --git a/dist/fuse.d.ts b/dist/fuse.d.ts index e521581b4..d88008cfd 100644 --- a/dist/fuse.d.ts +++ b/dist/fuse.d.ts @@ -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 diff --git a/dist/fuse.js b/dist/fuse.js index 1c8df94a7..ddbdbbc11 100644 --- a/dist/fuse.js +++ b/dist/fuse.js @@ -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 @@ -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); @@ -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; @@ -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 @@ -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; diff --git a/docs/api/options.md b/docs/api/options.md index f15e7d20c..bd17468c5 100644 --- a/docs/api/options.md +++ b/docs/api/options.md @@ -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` - -- Type: `boolean` -- Default: `false` - -When `true`, if the search query is empty, return the whole list instead of an empty array. diff --git a/src/core/config.js b/src/core/config.js index 847b44efa..495d626ee 100644 --- a/src/core/config.js +++ b/src/core/config.js @@ -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 { diff --git a/src/core/index.js b/src/core/index.js index 70988020b..ef1b1abd7 100644 --- a/src/core/index.js +++ b/src/core/index.js @@ -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' @@ -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) diff --git a/src/tools/FuseIndex.js b/src/tools/FuseIndex.js index 6da6f5106..84d593538 100644 --- a/src/tools/FuseIndex.js +++ b/src/tools/FuseIndex.js @@ -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 } = {}) { @@ -139,8 +140,9 @@ 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 @@ -148,7 +150,7 @@ export function createIndex(keys, docs, { getFn = Config.getFn } = {}) { 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 diff --git a/test/__snapshots__/fuzzy-search.test.js.snap b/test/__snapshots__/fuzzy-search.test.js.snap index 4690d0904..dd6dc0816 100644 --- a/test/__snapshots__/fuzzy-search.test.js.snap +++ b/test/__snapshots__/fuzzy-search.test.js.snap @@ -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, - }, -] -`; diff --git a/test/fuzzy-search.test.js b/test/fuzzy-search.test.js index fd9f976c2..5dda52d1a 100644 --- a/test/fuzzy-search.test.js +++ b/test/fuzzy-search.test.js @@ -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', diff --git a/test/indexing.test.js b/test/indexing.test.js index 773160eb7..9c5d431f5 100644 --- a/test/indexing.test.js +++ b/test/indexing.test.js @@ -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)