From 1504b0c1a3907ffd588fb509c70b462e67b4bffc Mon Sep 17 00:00:00 2001 From: kobenguyent Date: Thu, 9 Jan 2025 12:57:11 +0100 Subject: [PATCH] reuse fusejs --- lib/listener/emptyRun.js | 7 ++---- lib/pause.js | 10 +++------ lib/utils.js | 47 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 12 deletions(-) diff --git a/lib/listener/emptyRun.js b/lib/listener/emptyRun.js index 2fa8af341..04339624c 100644 --- a/lib/listener/emptyRun.js +++ b/lib/listener/emptyRun.js @@ -2,6 +2,7 @@ const figures = require('figures') const Container = require('../container') const event = require('../event') const output = require('../output') +const { searchWithFusejs } = require('../utils') module.exports = function () { let isEmptyRun = true @@ -15,8 +16,6 @@ module.exports = function () { const mocha = Container.mocha() if (mocha.options.grep) { - const Fuse = require('fuse.js') - output.print() output.print('No tests found by pattern: ' + mocha.options.grep) @@ -27,14 +26,12 @@ module.exports = function () { }) }) - const fuse = new Fuse(allTests, { + const results = searchWithFusejs(allTests, mocha.options.grep.toString(), { includeScore: true, threshold: 0.6, caseSensitive: false, }) - const results = fuse.search(mocha.options.grep.toString()) - if (results.length > 0) { output.print() output.print('Maybe you wanted to run one of these tests?') diff --git a/lib/pause.js b/lib/pause.js index d551a15de..0bb2d0475 100644 --- a/lib/pause.js +++ b/lib/pause.js @@ -2,7 +2,6 @@ const colors = require('chalk') const readline = require('readline') const ora = require('ora-classic') const debug = require('debug')('codeceptjs:pause') -const Fuse = require('fuse.js') const container = require('./container') const history = require('./history') @@ -11,7 +10,7 @@ const aiAssistant = require('./ai') const recorder = require('./recorder') const event = require('./event') const output = require('./output') -const { methodsOfObject } = require('./utils') +const { methodsOfObject, searchWithFusejs } = require('./utils') // npm install colors let rl @@ -218,15 +217,12 @@ function completer(line) { return [completions, line] } - // Initialize Fuse with completions - const fuse = new Fuse(completions, { + // Search using Fuse.js + const searchResults = searchWithFusejs(completions, line, { threshold: 0.3, distance: 100, minMatchCharLength: 1, }) - - // Search using Fuse.js - const searchResults = fuse.search(line) const hits = searchResults.map(result => result.item) return [hits, line] diff --git a/lib/utils.js b/lib/utils.js index 5698f1464..3c28b2696 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -4,6 +4,7 @@ const path = require('path') const getFunctionArguments = require('fn-args') const deepClone = require('lodash.clonedeep') const { convertColorToRGBA, isColorProperty } = require('./colorUtils') +const Fuse = require('fuse.js') function deepMerge(target, source) { const merge = require('lodash.merge') @@ -484,3 +485,49 @@ module.exports.humanizeFunction = function (fn) { return simplified } + +/** + * Searches through a given data source using the Fuse.js library for fuzzy searching. + * + * @function searchWithFusejs + * @param {Array|Object} source - The data source to search through. This can be an array of objects or strings. + * @param {string} searchString - The search query string to match against the source. + * @param {Object} [opts] - Optional configuration object for Fuse.js. + * @param {boolean} [opts.includeScore=true] - Whether to include the score of the match in the results. + * @param {number} [opts.threshold=0.6] - Determines the match threshold; lower values mean stricter matching. + * @param {boolean} [opts.caseSensitive=false] - Whether the search should be case-sensitive. + * @param {number} [opts.distance=100] - Determines how far apart the search term is allowed to be from the target. + * @param {number} [opts.maxPatternLength=32] - The maximum length of the search pattern. Patterns longer than this are ignored. + * @param {boolean} [opts.ignoreLocation=false] - Whether the location of the match is ignored when scoring. + * @param {boolean} [opts.ignoreFieldNorm=false] - When true, the field's length is not considered when scoring. + * @param {Array} [opts.keys=[]] - List of keys to search in the objects of the source array. + * @param {boolean} [opts.shouldSort=true] - Whether the results should be sorted by score. + * @param {string} [opts.sortFn] - A custom sorting function for sorting results. + * @param {number} [opts.minMatchCharLength=1] - The minimum number of characters that must match. + * @param {boolean} [opts.useExtendedSearch=false] - Enables extended search capabilities. + * + * @returns {Array} - An array of search results. Each result contains an item and, if `includeScore` is true, a score. + * + * @example + * const data = [ + * { title: "Old Man's War", author: "John Scalzi" }, + * { title: "The Lock Artist", author: "Steve Hamilton" }, + * ]; + * + * const options = { + * keys: ['title', 'author'], + * includeScore: true, + * threshold: 0.4, + * caseSensitive: false, + * distance: 50, + * ignoreLocation: true, + * }; + * + * const results = searchWithFusejs(data, 'lock', options); + * console.log(results); + */ +module.exports.searchWithFusejs = function (source, searchString, opts) { + const fuse = new Fuse(source, opts) + + return fuse.search(searchString) +}