Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reuse fusejs #4729

Merged
merged 1 commit into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions lib/listener/emptyRun.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)

Expand All @@ -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?')
Expand Down
10 changes: 3 additions & 7 deletions lib/pause.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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
Expand Down Expand Up @@ -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]
Expand Down
47 changes: 47 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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<string>} [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<Object>} - 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)
}
Loading