@@ -4,6 +4,7 @@ const path = require('path')
44const getFunctionArguments = require ( 'fn-args' )
55const deepClone = require ( 'lodash.clonedeep' )
66const { convertColorToRGBA, isColorProperty } = require ( './colorUtils' )
7+ const Fuse = require ( 'fuse.js' )
78
89function 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