Skip to content

Commit eee48ea

Browse files
authored
reuse fusejs (#4729)
1 parent f1e2eb8 commit eee48ea

File tree

3 files changed

+52
-12
lines changed

3 files changed

+52
-12
lines changed

lib/listener/emptyRun.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const figures = require('figures')
22
const Container = require('../container')
33
const event = require('../event')
44
const output = require('../output')
5+
const { searchWithFusejs } = require('../utils')
56

67
module.exports = function () {
78
let isEmptyRun = true
@@ -15,8 +16,6 @@ module.exports = function () {
1516
const mocha = Container.mocha()
1617

1718
if (mocha.options.grep) {
18-
const Fuse = require('fuse.js')
19-
2019
output.print()
2120
output.print('No tests found by pattern: ' + mocha.options.grep)
2221

@@ -27,14 +26,12 @@ module.exports = function () {
2726
})
2827
})
2928

30-
const fuse = new Fuse(allTests, {
29+
const results = searchWithFusejs(allTests, mocha.options.grep.toString(), {
3130
includeScore: true,
3231
threshold: 0.6,
3332
caseSensitive: false,
3433
})
3534

36-
const results = fuse.search(mocha.options.grep.toString())
37-
3835
if (results.length > 0) {
3936
output.print()
4037
output.print('Maybe you wanted to run one of these tests?')

lib/pause.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ const colors = require('chalk')
22
const readline = require('readline')
33
const ora = require('ora-classic')
44
const debug = require('debug')('codeceptjs:pause')
5-
const Fuse = require('fuse.js')
65

76
const container = require('./container')
87
const history = require('./history')
@@ -11,7 +10,7 @@ const aiAssistant = require('./ai')
1110
const recorder = require('./recorder')
1211
const event = require('./event')
1312
const output = require('./output')
14-
const { methodsOfObject } = require('./utils')
13+
const { methodsOfObject, searchWithFusejs } = require('./utils')
1514

1615
// npm install colors
1716
let rl
@@ -218,15 +217,12 @@ function completer(line) {
218217
return [completions, line]
219218
}
220219

221-
// Initialize Fuse with completions
222-
const fuse = new Fuse(completions, {
220+
// Search using Fuse.js
221+
const searchResults = searchWithFusejs(completions, line, {
223222
threshold: 0.3,
224223
distance: 100,
225224
minMatchCharLength: 1,
226225
})
227-
228-
// Search using Fuse.js
229-
const searchResults = fuse.search(line)
230226
const hits = searchResults.map(result => result.item)
231227

232228
return [hits, line]

lib/utils.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const path = require('path')
44
const getFunctionArguments = require('fn-args')
55
const deepClone = require('lodash.clonedeep')
66
const { convertColorToRGBA, isColorProperty } = require('./colorUtils')
7+
const Fuse = require('fuse.js')
78

89
function deepMerge(target, source) {
910
const merge = require('lodash.merge')
@@ -484,3 +485,49 @@ module.exports.humanizeFunction = function (fn) {
484485

485486
return simplified
486487
}
488+
489+
/**
490+
* Searches through a given data source using the Fuse.js library for fuzzy searching.
491+
*
492+
* @function searchWithFusejs
493+
* @param {Array|Object} source - The data source to search through. This can be an array of objects or strings.
494+
* @param {string} searchString - The search query string to match against the source.
495+
* @param {Object} [opts] - Optional configuration object for Fuse.js.
496+
* @param {boolean} [opts.includeScore=true] - Whether to include the score of the match in the results.
497+
* @param {number} [opts.threshold=0.6] - Determines the match threshold; lower values mean stricter matching.
498+
* @param {boolean} [opts.caseSensitive=false] - Whether the search should be case-sensitive.
499+
* @param {number} [opts.distance=100] - Determines how far apart the search term is allowed to be from the target.
500+
* @param {number} [opts.maxPatternLength=32] - The maximum length of the search pattern. Patterns longer than this are ignored.
501+
* @param {boolean} [opts.ignoreLocation=false] - Whether the location of the match is ignored when scoring.
502+
* @param {boolean} [opts.ignoreFieldNorm=false] - When true, the field's length is not considered when scoring.
503+
* @param {Array<string>} [opts.keys=[]] - List of keys to search in the objects of the source array.
504+
* @param {boolean} [opts.shouldSort=true] - Whether the results should be sorted by score.
505+
* @param {string} [opts.sortFn] - A custom sorting function for sorting results.
506+
* @param {number} [opts.minMatchCharLength=1] - The minimum number of characters that must match.
507+
* @param {boolean} [opts.useExtendedSearch=false] - Enables extended search capabilities.
508+
*
509+
* @returns {Array<Object>} - An array of search results. Each result contains an item and, if `includeScore` is true, a score.
510+
*
511+
* @example
512+
* const data = [
513+
* { title: "Old Man's War", author: "John Scalzi" },
514+
* { title: "The Lock Artist", author: "Steve Hamilton" },
515+
* ];
516+
*
517+
* const options = {
518+
* keys: ['title', 'author'],
519+
* includeScore: true,
520+
* threshold: 0.4,
521+
* caseSensitive: false,
522+
* distance: 50,
523+
* ignoreLocation: true,
524+
* };
525+
*
526+
* const results = searchWithFusejs(data, 'lock', options);
527+
* console.log(results);
528+
*/
529+
module.exports.searchWithFusejs = function (source, searchString, opts) {
530+
const fuse = new Fuse(source, opts)
531+
532+
return fuse.search(searchString)
533+
}

0 commit comments

Comments
 (0)