From ebd7bb5119df3a8394b8aff5042f112822360218 Mon Sep 17 00:00:00 2001 From: Hugo van Rijswijk Date: Wed, 20 Nov 2024 18:28:38 +0100 Subject: [PATCH] Add ESM and rollup build Fixes #94 Adds a rollup config file to build ESM, CJS, and UMD bundles. Based on the source in src/fuzzysort.js. Checks in the new esm file and the updated cjs and umd files. Also updates the package.json to include `module` and `browser` fields for the ESM and UMD bundles. --- .gitignore | 2 + fuzzysort.js | 1156 ++++++++++++++++++++++----------------------- fuzzysort.min.js | 4 +- fuzzysort.mjs | 682 ++++++++++++++++++++++++++ package-lock.json | 531 +++++++++++++++++++++ package.json | 26 +- rollup.config.mjs | 24 + src/fuzzysort.js | 678 ++++++++++++++++++++++++++ test/test.html | 2 +- 9 files changed, 2515 insertions(+), 590 deletions(-) create mode 100644 .gitignore create mode 100644 fuzzysort.mjs create mode 100644 package-lock.json create mode 100644 rollup.config.mjs create mode 100644 src/fuzzysort.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..fc467d1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +node_modules +*.map diff --git a/fuzzysort.js b/fuzzysort.js index ff816bb..ae4baa4 100644 --- a/fuzzysort.js +++ b/fuzzysort.js @@ -1,690 +1,690 @@ -// https://github.com/farzher/fuzzysort v3.0.2 +// https://github.com/farzher/fuzzysort v3.1.0 +'use strict'; -// UMD (Universal Module Definition) for fuzzysort -;((root, UMD) => { - if(typeof define === 'function' && define.amd) define([], UMD) - else if(typeof module === 'object' && module.exports) module.exports = UMD() - else root['fuzzysort'] = UMD() -})(this, _ => { - 'use strict' +Object.defineProperty(exports, '__esModule', { value: true }); - var single = (search, target) => { - if(!search || !target) return NULL +const single = (search, target) => { + if(!search || !target) return NULL - var preparedSearch = getPreparedSearch(search) - if(!isPrepared(target)) target = getPrepared(target) + var preparedSearch = getPreparedSearch(search); + if(!isPrepared(target)) target = getPrepared(target); - var searchBitflags = preparedSearch.bitflags - if((searchBitflags & target._bitflags) !== searchBitflags) return NULL + var searchBitflags = preparedSearch.bitflags; + if((searchBitflags & target._bitflags) !== searchBitflags) return NULL - return algorithm(preparedSearch, target) - } + return algorithm(preparedSearch, target) +}; - var go = (search, targets, options) => { - if(!search) return options?.all ? all(targets, options) : noResults +const go = (search, targets, options) => { + if(!search) return options?.all ? all(targets, options) : noResults - var preparedSearch = getPreparedSearch(search) - var searchBitflags = preparedSearch.bitflags - var containsSpace = preparedSearch.containsSpace + var preparedSearch = getPreparedSearch(search); + var searchBitflags = preparedSearch.bitflags; + var containsSpace = preparedSearch.containsSpace; - var threshold = denormalizeScore( options?.threshold || 0 ) - var limit = options?.limit || INFINITY + var threshold = denormalizeScore( options?.threshold || 0 ); + var limit = options?.limit || INFINITY; - var resultsLen = 0; var limitedCount = 0 - var targetsLen = targets.length + var resultsLen = 0; var limitedCount = 0; + var targetsLen = targets.length; - function push_result(result) { - if(resultsLen < limit) { q.add(result); ++resultsLen } - else { - ++limitedCount - if(result._score > q.peek()._score) q.replaceTop(result) - } + function push_result(result) { + if(resultsLen < limit) { q.add(result); ++resultsLen; } + else { + ++limitedCount; + if(result._score > q.peek()._score) q.replaceTop(result); } + } - // This code is copy/pasted 3 times for performance reasons [options.key, options.keys, no keys] - - // options.key - if(options?.key) { - var key = options.key - for(var i = 0; i < targetsLen; ++i) { var obj = targets[i] - var target = getValue(obj, key) - if(!target) continue - if(!isPrepared(target)) target = getPrepared(target) + // This code is copy/pasted 3 times for performance reasons [options.key, options.keys, no keys] - if((searchBitflags & target._bitflags) !== searchBitflags) continue - var result = algorithm(preparedSearch, target) - if(result === NULL) continue - if(result._score < threshold) continue + // options.key + if(options?.key) { + var key = options.key; + for(var i = 0; i < targetsLen; ++i) { var obj = targets[i]; + var target = getValue(obj, key); + if(!target) continue + if(!isPrepared(target)) target = getPrepared(target); - result.obj = obj - push_result(result) - } + if((searchBitflags & target._bitflags) !== searchBitflags) continue + var result = algorithm(preparedSearch, target); + if(result === NULL) continue + if(result._score < threshold) continue - // options.keys - } else if(options?.keys) { - var keys = options.keys - var keysLen = keys.length + result.obj = obj; + push_result(result); + } - outer: for(var i = 0; i < targetsLen; ++i) { var obj = targets[i] + // options.keys + } else if(options?.keys) { + var keys = options.keys; + var keysLen = keys.length; - { // early out based on bitflags - var keysBitflags = 0 - for (var keyI = 0; keyI < keysLen; ++keyI) { - var key = keys[keyI] - var target = getValue(obj, key) - if(!target) { tmpTargets[keyI] = noTarget; continue } - if(!isPrepared(target)) target = getPrepared(target) - tmpTargets[keyI] = target + outer: for(var i = 0; i < targetsLen; ++i) { var obj = targets[i]; - keysBitflags |= target._bitflags - } + { // early out based on bitflags + var keysBitflags = 0; + for (var keyI = 0; keyI < keysLen; ++keyI) { + var key = keys[keyI]; + var target = getValue(obj, key); + if(!target) { tmpTargets[keyI] = noTarget; continue } + if(!isPrepared(target)) target = getPrepared(target); + tmpTargets[keyI] = target; - if((searchBitflags & keysBitflags) !== searchBitflags) continue + keysBitflags |= target._bitflags; } - if(containsSpace) for(let i=0; i -1000) { - if(keysSpacesBestScores[i] > NEGATIVE_INFINITY) { - var tmp = (keysSpacesBestScores[i] + allowPartialMatchScores[i]) / 4/*bonus score for having multiple matches*/ - if(tmp > keysSpacesBestScores[i]) keysSpacesBestScores[i] = tmp - } - } - if(allowPartialMatchScores[i] > keysSpacesBestScores[i]) keysSpacesBestScores[i] = allowPartialMatchScores[i] - } - } + if(containsSpace) for(let i=0; i -1000) { - if(score > NEGATIVE_INFINITY) { - var tmp = (score + result._score) / 4/*bonus score for having multiple matches*/ - if(tmp > score) score = tmp - } + // todo: this seems weird and wrong. like what if our first match wasn't good. this should just replace it instead of averaging with it + // if our second match isn't good we ignore it instead of averaging with it + if(containsSpace) for(let i=0; i -1000) { + if(keysSpacesBestScores[i] > NEGATIVE_INFINITY) { + var tmp = (keysSpacesBestScores[i] + allowPartialMatchScores[i]) / 4;/*bonus score for having multiple matches*/ + if(tmp > keysSpacesBestScores[i]) keysSpacesBestScores[i] = tmp; } - if(result._score > score) score = result._score } + if(allowPartialMatchScores[i] > keysSpacesBestScores[i]) keysSpacesBestScores[i] = allowPartialMatchScores[i]; } - - objResults.obj = obj - objResults._score = score - if(options?.scoreFn) { - score = options.scoreFn(objResults) - if(!score) continue - score = denormalizeScore(score) - objResults._score = score - } - - if(score < threshold) continue - push_result(objResults) } - // no keys - } else { - for(var i = 0; i < targetsLen; ++i) { var target = targets[i] - if(!target) continue - if(!isPrepared(target)) target = getPrepared(target) - - if((searchBitflags & target._bitflags) !== searchBitflags) continue - var result = algorithm(preparedSearch, target) - if(result === NULL) continue - if(result._score < threshold) continue - - push_result(result) + if(containsSpace) { + for(let i=0; i= 0; --i) results[i] = q.poll() - results.total = resultsLen + limitedCount - return results - } - - - // this is written as 1 function instead of 2 for minification. perf seems fine ... - // except when minified. the perf is very slow - var highlight = (result, open='', close='') => { - var callback = typeof open === 'function' ? open : undefined - - var target = result.target - var targetLen = target.length - var indexes = result.indexes - var highlighted = '' - var matchI = 0 - var indexesI = 0 - var opened = false - var parts = [] - - for(var i = 0; i < targetLen; ++i) { var char = target[i] - if(indexes[indexesI] === i) { - ++indexesI - if(!opened) { opened = true - if(callback) { - parts.push(highlighted); highlighted = '' - } else { - highlighted += open - } - } + var objResults = new KeysResult(keysLen); + for(let i=0; i < keysLen; i++) { objResults[i] = tmpResults[i]; } - if(indexesI === indexes.length) { - if(callback) { - highlighted += char - parts.push(callback(highlighted, matchI++)); highlighted = '' - parts.push(target.substr(i+1)) - } else { - highlighted += char + close + target.substr(i+1) - } - break - } + if(containsSpace) { + var score = 0; + for(let i=0; i -1000) { + if(score > NEGATIVE_INFINITY) { + var tmp = (score + result._score) / 4;/*bonus score for having multiple matches*/ + if(tmp > score) score = tmp; + } } + if(result._score > score) score = result._score; } } - highlighted += char - } - - return callback ? parts : highlighted - } - - - var prepare = (target) => { - if(typeof target === 'number') target = ''+target - else if(typeof target !== 'string') target = '' - var info = prepareLowerInfo(target) - return new_result(target, {_targetLower:info._lower, _targetLowerCodes:info.lowerCodes, _bitflags:info.bitflags}) - } - - var cleanup = () => { preparedCache.clear(); preparedSearchCache.clear() } - - - // Below this point is only internal code - // Below this point is only internal code - // Below this point is only internal code - // Below this point is only internal code - - - class Result { - get ['indexes']() { return this._indexes.slice(0, this._indexes.len).sort((a,b)=>a-b) } - set ['indexes'](indexes) { return this._indexes = indexes } - ['highlight'](open, close) { return highlight(this, open, close) } - get ['score']() { return normalizeScore(this._score) } - set ['score'](score) { this._score = denormalizeScore(score) } - } - - class KeysResult extends Array { - get ['score']() { return normalizeScore(this._score) } - set ['score'](score) { this._score = denormalizeScore(score) } - } - - var new_result = (target, options) => { - const result = new Result() - result['target'] = target - result['obj'] = options.obj ?? NULL - result._score = options._score ?? NEGATIVE_INFINITY - result._indexes = options._indexes ?? [] - result._targetLower = options._targetLower ?? '' - result._targetLowerCodes = options._targetLowerCodes ?? NULL - result._nextBeginningIndexes = options._nextBeginningIndexes ?? NULL - result._bitflags = options._bitflags ?? 0 - return result - } - - var normalizeScore = score => { - if(score === NEGATIVE_INFINITY) return 0 - if(score > 1) return score - return Math.E ** ( ((-score + 1)**.04307 - 1) * -2) - } - var denormalizeScore = normalizedScore => { - if(normalizedScore === 0) return NEGATIVE_INFINITY - if(normalizedScore > 1) return normalizedScore - return 1 - Math.pow((Math.log(normalizedScore) / -2 + 1), 1 / 0.04307) - } - - - var prepareSearch = (search) => { - if(typeof search === 'number') search = ''+search - else if(typeof search !== 'string') search = '' - search = search.trim() - var info = prepareLowerInfo(search) - - var spaceSearches = [] - if(info.containsSpace) { - var searches = search.split(/\s+/) - searches = [...new Set(searches)] // distinct - for(var i=0; i { - if(target.length > 999) return prepare(target) // don't cache huge targets - var targetPrepared = preparedCache.get(target) - if(targetPrepared !== undefined) return targetPrepared - targetPrepared = prepare(target) - preparedCache.set(target, targetPrepared) - return targetPrepared - } - var getPreparedSearch = (search) => { - if(search.length > 999) return prepareSearch(search) // don't cache huge searches - var searchPrepared = preparedSearchCache.get(search) - if(searchPrepared !== undefined) return searchPrepared - searchPrepared = prepareSearch(search) - preparedSearchCache.set(search, searchPrepared) - return searchPrepared + push_result(result); + } } - - var all = (targets, options) => { - var results = []; results.total = targets.length // this total can be wrong if some targets are skipped - - var limit = options?.limit || INFINITY - - if(options?.key) { - for(var i=0;i= limit) return results + if(resultsLen === 0) return noResults + var results = new Array(resultsLen); + for(var i = resultsLen - 1; i >= 0; --i) results[i] = q.poll(); + results.total = resultsLen + limitedCount; + return results +}; + + +// this is written as 1 function instead of 2 for minification. perf seems fine ... +// except when minified. the perf is very slow +const highlight = (result, open='', close='') => { + var callback = typeof open === 'function' ? open : undefined; + + var target = result.target; + var targetLen = target.length; + var indexes = result.indexes; + var highlighted = ''; + var matchI = 0; + var indexesI = 0; + var opened = false; + var parts = []; + + for(var i = 0; i < targetLen; ++i) { var char = target[i]; + if(indexes[indexesI] === i) { + ++indexesI; + if(!opened) { opened = true; + if(callback) { + parts.push(highlighted); highlighted = ''; + } else { + highlighted += open; + } } - } else if(options?.keys) { - for(var i=0;i= 0; --keyI) { - var target = getValue(obj, options.keys[keyI]) - if(!target) { objResults[keyI] = noTarget; continue } - if(!isPrepared(target)) target = getPrepared(target) - target._score = NEGATIVE_INFINITY - target._indexes.len = 0 - objResults[keyI] = target + + if(indexesI === indexes.length) { + if(callback) { + highlighted += char; + parts.push(callback(highlighted, matchI++)); highlighted = ''; + parts.push(target.substr(i+1)); + } else { + highlighted += char + close + target.substr(i+1); } - objResults.obj = obj - objResults._score = NEGATIVE_INFINITY - results.push(objResults); if(results.length >= limit) return results + break } } else { - for(var i=0;i= limit) return results + if(opened) { opened = false; + if(callback) { + parts.push(callback(highlighted, matchI++)); highlighted = ''; + } else { + highlighted += close; + } } } - - return results + highlighted += char; } + return callback ? parts : highlighted +}; + + +const prepare = (target) => { + if(typeof target === 'number') target = ''+target; + else if(typeof target !== 'string') target = ''; + var info = prepareLowerInfo(target); + return new_result(target, {_targetLower:info._lower, _targetLowerCodes:info.lowerCodes, _bitflags:info.bitflags}) +}; + +const cleanup = () => { preparedCache.clear(); preparedSearchCache.clear(); }; + + +// Below this point is only internal code +// Below this point is only internal code +// Below this point is only internal code +// Below this point is only internal code + + +class Result { + get ['indexes']() { return this._indexes.slice(0, this._indexes.len).sort((a,b)=>a-b) } + set ['indexes'](indexes) { return this._indexes = indexes } + ['highlight'](open, close) { return highlight(this, open, close) } + get ['score']() { return normalizeScore(this._score) } + set ['score'](score) { this._score = denormalizeScore(score); } +} + +class KeysResult extends Array { + get ['score']() { return normalizeScore(this._score) } + set ['score'](score) { this._score = denormalizeScore(score); } +} + +const new_result = (target, options) => { + const result = new Result(); + result['target'] = target; + result['obj'] = options.obj ?? NULL; + result._score = options._score ?? NEGATIVE_INFINITY; + result._indexes = options._indexes ?? []; + result._targetLower = options._targetLower ?? ''; + result._targetLowerCodes = options._targetLowerCodes ?? NULL; + result._nextBeginningIndexes = options._nextBeginningIndexes ?? NULL; + result._bitflags = options._bitflags ?? 0; + return result +}; + + +const normalizeScore = score => { + if(score === NEGATIVE_INFINITY) return 0 + if(score > 1) return score + return Math.E ** ( ((-score + 1)**.04307 - 1) * -2) +}; +const denormalizeScore = normalizedScore => { + if(normalizedScore === 0) return NEGATIVE_INFINITY + if(normalizedScore > 1) return normalizedScore + return 1 - Math.pow((Math.log(normalizedScore) / -2 + 1), 1 / 0.04307) +}; + + +const prepareSearch = (search) => { + if(typeof search === 'number') search = ''+search; + else if(typeof search !== 'string') search = ''; + search = search.trim(); + var info = prepareLowerInfo(search); + + var spaceSearches = []; + if(info.containsSpace) { + var searches = search.split(/\s+/); + searches = [...new Set(searches)]; // distinct + for(var i=0; i { - if(allowSpaces===false && preparedSearch.containsSpace) return algorithmSpaces(preparedSearch, prepared, allowPartialMatch) - - var searchLower = preparedSearch._lower - var searchLowerCodes = preparedSearch.lowerCodes - var searchLowerCode = searchLowerCodes[0] - var targetLowerCodes = prepared._targetLowerCodes - var searchLen = searchLowerCodes.length - var targetLen = targetLowerCodes.length - var searchI = 0 // where we at - var targetI = 0 // where you at - var matchesSimpleLen = 0 - - // very basic fuzzy match; to remove non-matching targets ASAP! - // walk through target. find sequential matches. - // if all chars aren't found then exit - for(;;) { - var isMatch = searchLowerCode === targetLowerCodes[targetI] - if(isMatch) { - matchesSimple[matchesSimpleLen++] = targetI - ++searchI; if(searchI === searchLen) break - searchLowerCode = searchLowerCodes[searchI] + return {lowerCodes: info.lowerCodes, _lower: info._lower, containsSpace: info.containsSpace, bitflags: info.bitflags, spaceSearches: spaceSearches} +}; + + + +const getPrepared = (target) => { + if(target.length > 999) return prepare(target) // don't cache huge targets + var targetPrepared = preparedCache.get(target); + if(targetPrepared !== undefined) return targetPrepared + targetPrepared = prepare(target); + preparedCache.set(target, targetPrepared); + return targetPrepared +}; +const getPreparedSearch = (search) => { + if(search.length > 999) return prepareSearch(search) // don't cache huge searches + var searchPrepared = preparedSearchCache.get(search); + if(searchPrepared !== undefined) return searchPrepared + searchPrepared = prepareSearch(search); + preparedSearchCache.set(search, searchPrepared); + return searchPrepared +}; + + +const all = (targets, options) => { + var results = []; results.total = targets.length; // this total can be wrong if some targets are skipped + + var limit = options?.limit || INFINITY; + + if(options?.key) { + for(var i=0;i= limit) return results + } + } else if(options?.keys) { + for(var i=0;i= 0; --keyI) { + var target = getValue(obj, options.keys[keyI]); + if(!target) { objResults[keyI] = noTarget; continue } + if(!isPrepared(target)) target = getPrepared(target); + target._score = NEGATIVE_INFINITY; + target._indexes.len = 0; + objResults[keyI] = target; } - ++targetI; if(targetI >= targetLen) return NULL // Failed to find searchI + objResults.obj = obj; + objResults._score = NEGATIVE_INFINITY; + results.push(objResults); if(results.length >= limit) return results } + } else { + for(var i=0;i= limit) return results + } + } + + return results +}; + + +const algorithm = (preparedSearch, prepared, allowSpaces=false, allowPartialMatch=false) => { + if(allowSpaces===false && preparedSearch.containsSpace) return algorithmSpaces(preparedSearch, prepared, allowPartialMatch) + + var searchLower = preparedSearch._lower; + var searchLowerCodes = preparedSearch.lowerCodes; + var searchLowerCode = searchLowerCodes[0]; + var targetLowerCodes = prepared._targetLowerCodes; + var searchLen = searchLowerCodes.length; + var targetLen = targetLowerCodes.length; + var searchI = 0; // where we at + var targetI = 0; // where you at + var matchesSimpleLen = 0; + + // very basic fuzzy match; to remove non-matching targets ASAP! + // walk through target. find sequential matches. + // if all chars aren't found then exit + for(;;) { + var isMatch = searchLowerCode === targetLowerCodes[targetI]; + if(isMatch) { + matchesSimple[matchesSimpleLen++] = targetI; + ++searchI; if(searchI === searchLen) break + searchLowerCode = searchLowerCodes[searchI]; + } + ++targetI; if(targetI >= targetLen) return NULL // Failed to find searchI + } - var searchI = 0 - var successStrict = false - var matchesStrictLen = 0 + var searchI = 0; + var successStrict = false; + var matchesStrictLen = 0; - var nextBeginningIndexes = prepared._nextBeginningIndexes - if(nextBeginningIndexes === NULL) nextBeginningIndexes = prepared._nextBeginningIndexes = prepareNextBeginningIndexes(prepared.target) - targetI = matchesSimple[0]===0 ? 0 : nextBeginningIndexes[matchesSimple[0]-1] + var nextBeginningIndexes = prepared._nextBeginningIndexes; + if(nextBeginningIndexes === NULL) nextBeginningIndexes = prepared._nextBeginningIndexes = prepareNextBeginningIndexes(prepared.target); + targetI = matchesSimple[0]===0 ? 0 : nextBeginningIndexes[matchesSimple[0]-1]; - // Our target string successfully matched all characters in sequence! - // Let's try a more advanced and strict test to improve the score - // only count it as a match if it's consecutive or a beginning character! - var backtrackCount = 0 - if(targetI !== targetLen) for(;;) { - if(targetI >= targetLen) { - // We failed to find a good spot for this search char, go back to the previous search char and force it forward - if(searchI <= 0) break // We failed to push chars forward for a better match + // Our target string successfully matched all characters in sequence! + // Let's try a more advanced and strict test to improve the score + // only count it as a match if it's consecutive or a beginning character! + var backtrackCount = 0; + if(targetI !== targetLen) for(;;) { + if(targetI >= targetLen) { + // We failed to find a good spot for this search char, go back to the previous search char and force it forward + if(searchI <= 0) break // We failed to push chars forward for a better match - ++backtrackCount; if(backtrackCount > 200) break // exponential backtracking is taking too long, just give up and return a bad match + ++backtrackCount; if(backtrackCount > 200) break // exponential backtracking is taking too long, just give up and return a bad match - --searchI - var lastMatch = matchesStrict[--matchesStrictLen] - targetI = nextBeginningIndexes[lastMatch] + --searchI; + var lastMatch = matchesStrict[--matchesStrictLen]; + targetI = nextBeginningIndexes[lastMatch]; + } else { + var isMatch = searchLowerCodes[searchI] === targetLowerCodes[targetI]; + if(isMatch) { + matchesStrict[matchesStrictLen++] = targetI; + ++searchI; if(searchI === searchLen) { successStrict = true; break } + ++targetI; } else { - var isMatch = searchLowerCodes[searchI] === targetLowerCodes[targetI] - if(isMatch) { - matchesStrict[matchesStrictLen++] = targetI - ++searchI; if(searchI === searchLen) { successStrict = true; break } - ++targetI - } else { - targetI = nextBeginningIndexes[targetI] - } + targetI = nextBeginningIndexes[targetI]; } } + } - // check if it's a substring match - var substringIndex = searchLen <= 1 ? -1 : prepared._targetLower.indexOf(searchLower, matchesSimple[0]) // perf: this is slow - var isSubstring = !!~substringIndex - var isSubstringBeginning = !isSubstring ? false : substringIndex===0 || prepared._nextBeginningIndexes[substringIndex-1] === substringIndex + // check if it's a substring match + var substringIndex = searchLen <= 1 ? -1 : prepared._targetLower.indexOf(searchLower, matchesSimple[0]); // perf: this is slow + var isSubstring = !!~substringIndex; + var isSubstringBeginning = !isSubstring ? false : substringIndex===0 || prepared._nextBeginningIndexes[substringIndex-1] === substringIndex; - // if it's a substring match but not at a beginning index, let's try to find a substring starting at a beginning index for a better score - if(isSubstring && !isSubstringBeginning) { - for(var i=0; i { - var score = 0 + var calculateScore = matches => { + var score = 0; - var extraMatchGroupCount = 0 - for(var i = 1; i < searchLen; ++i) { - if(matches[i] - matches[i-1] !== 1) {score -= matches[i]; ++extraMatchGroupCount} - } - var unmatchedDistance = matches[searchLen-1] - matches[0] - (searchLen-1) + var extraMatchGroupCount = 0; + for(var i = 1; i < searchLen; ++i) { + if(matches[i] - matches[i-1] !== 1) {score -= matches[i]; ++extraMatchGroupCount;} + } + var unmatchedDistance = matches[searchLen-1] - matches[0] - (searchLen-1); - score -= (12+unmatchedDistance) * extraMatchGroupCount // penality for more groups + score -= (12+unmatchedDistance) * extraMatchGroupCount; // penality for more groups - if(matches[0] !== 0) score -= matches[0]*matches[0]*.2 // penality for not starting near the beginning + if(matches[0] !== 0) score -= matches[0]*matches[0]*.2; // penality for not starting near the beginning - if(!successStrict) { - score *= 1000 - } else { - // successStrict on a target with too many beginning indexes loses points for being a bad target - var uniqueBeginningIndexes = 1 - for(var i = nextBeginningIndexes[0]; i < targetLen; i=nextBeginningIndexes[i]) ++uniqueBeginningIndexes + if(!successStrict) { + score *= 1000; + } else { + // successStrict on a target with too many beginning indexes loses points for being a bad target + var uniqueBeginningIndexes = 1; + for(var i = nextBeginningIndexes[0]; i < targetLen; i=nextBeginningIndexes[i]) ++uniqueBeginningIndexes; - if(uniqueBeginningIndexes > 24) score *= (uniqueBeginningIndexes-24)*10 // quite arbitrary numbers here ... - } + if(uniqueBeginningIndexes > 24) score *= (uniqueBeginningIndexes-24)*10; // quite arbitrary numbers here ... + } - score -= (targetLen - searchLen)/2 // penality for longer targets + score -= (targetLen - searchLen)/2; // penality for longer targets - if(isSubstring) score /= 1+searchLen*searchLen*1 // bonus for being a full substring - if(isSubstringBeginning) score /= 1+searchLen*searchLen*1 // bonus for substring starting on a beginningIndex + if(isSubstring) score /= 1+searchLen*searchLen*1; // bonus for being a full substring + if(isSubstringBeginning) score /= 1+searchLen*searchLen*1; // bonus for substring starting on a beginningIndex - score -= (targetLen - searchLen)/2 // penality for longer targets + score -= (targetLen - searchLen)/2; // penality for longer targets - return score - } + return score + }; - if(!successStrict) { - if(isSubstring) for(var i=0; i { - var seen_indexes = new Set() - var score = 0 - var result = NULL - - var first_seen_index_last_search = 0 - var searches = preparedSearch.spaceSearches - var searchesLen = searches.length - var changeslen = 0 - - // Return _nextBeginningIndexes back to its normal state - var resetNextBeginningIndexes = () => { - for(let i=changeslen-1; i>=0; i--) target._nextBeginningIndexes[nextBeginningIndexesChanges[i*2 + 0]] = nextBeginningIndexesChanges[i*2 + 1] - } - - var hasAtLeast1Match = false - for(var i=0; i { + var seen_indexes = new Set(); + var score = 0; + var result = NULL; + + var first_seen_index_last_search = 0; + var searches = preparedSearch.spaceSearches; + var searchesLen = searches.length; + var changeslen = 0; + + // Return _nextBeginningIndexes back to its normal state + var resetNextBeginningIndexes = () => { + for(let i=changeslen-1; i>=0; i--) target._nextBeginningIndexes[nextBeginningIndexesChanges[i*2 + 0]] = nextBeginningIndexesChanges[i*2 + 1]; + }; + + var hasAtLeast1Match = false; + for(var i=0; i=0; i--) { - if(toReplace !== target._nextBeginningIndexes[i]) break - target._nextBeginningIndexes[i] = newBeginningIndex - nextBeginningIndexesChanges[changeslen*2 + 0] = i - nextBeginningIndexesChanges[changeslen*2 + 1] = toReplace - changeslen++ - } + if(indexesIsConsecutiveSubstring) { + var newBeginningIndex = indexes[indexes.len-1] + 1; + var toReplace = target._nextBeginningIndexes[newBeginningIndex-1]; + for(let i=newBeginningIndex-1; i>=0; i--) { + if(toReplace !== target._nextBeginningIndexes[i]) break + target._nextBeginningIndexes[i] = newBeginningIndex; + nextBeginningIndexesChanges[changeslen*2 + 0] = i; + nextBeginningIndexesChanges[changeslen*2 + 1] = toReplace; + changeslen++; } } + } - score += result._score / searchesLen - allowPartialMatchScores[i] = result._score / searchesLen - - // dock points based on order otherwise "c man" returns Manifest.cpp instead of CheatManager.h - if(result._indexes[0] < first_seen_index_last_search) { - score -= (first_seen_index_last_search - result._indexes[0]) * 2 - } - first_seen_index_last_search = result._indexes[0] + score += result._score / searchesLen; + allowPartialMatchScores[i] = result._score / searchesLen; - for(var j=0; j score) { - if(allowPartialMatch) { - for(var i=0; i score) { + if(allowPartialMatch) { + for(var i=0; i str.replace(/\p{Script=Latin}+/gu, match => match.normalize('NFD')).replace(/[\u0300-\u036f]/g, '') + return result +}; - var prepareLowerInfo = (str) => { - str = remove_accents(str) - var strLen = str.length - var lower = str.toLowerCase() - var lowerCodes = [] // new Array(strLen) sparse array is too slow - var bitflags = 0 - var containsSpace = false // space isn't stored in bitflags because of how searching with a space works +// we use this instead of just .normalize('NFD').replace(/[\u0300-\u036f]/g, '') because that screws with japanese characters +const remove_accents = (str) => str.replace(/\p{Script=Latin}+/gu, match => match.normalize('NFD')).replace(/[\u0300-\u036f]/g, ''); - for(var i = 0; i < strLen; ++i) { - var lowerCode = lowerCodes[i] = lower.charCodeAt(i) +const prepareLowerInfo = (str) => { + str = remove_accents(str); + var strLen = str.length; + var lower = str.toLowerCase(); + var lowerCodes = []; // new Array(strLen) sparse array is too slow + var bitflags = 0; + var containsSpace = false; // space isn't stored in bitflags because of how searching with a space works - if(lowerCode === 32) { - containsSpace = true - continue // it's important that we don't set any bitflags for space - } + for(var i = 0; i < strLen; ++i) { + var lowerCode = lowerCodes[i] = lower.charCodeAt(i); - var bit = lowerCode>=97&&lowerCode<=122 ? lowerCode-97 // alphabet - : lowerCode>=48&&lowerCode<=57 ? 26 // numbers - // 3 bits available - : lowerCode<=127 ? 30 // other ascii - : 31 // other utf8 - bitflags |= 1<=97&&lowerCode<=122 ? lowerCode-97 // alphabet + : lowerCode>=48&&lowerCode<=57 ? 26 // numbers + // 3 bits available + : lowerCode<=127 ? 30 // other ascii + : 31; // other utf8 + bitflags |= 1< { - var targetLen = target.length - var beginningIndexes = []; var beginningIndexesLen = 0 - var wasUpper = false - var wasAlphanum = false - for(var i = 0; i < targetLen; ++i) { - var targetCode = target.charCodeAt(i) - var isUpper = targetCode>=65&&targetCode<=90 - var isAlphanum = isUpper || targetCode>=97&&targetCode<=122 || targetCode>=48&&targetCode<=57 - var isBeginning = isUpper && !wasUpper || !wasAlphanum || !isAlphanum - wasUpper = isUpper - wasAlphanum = isAlphanum - if(isBeginning) beginningIndexes[beginningIndexesLen++] = i - } - return beginningIndexes + + return {lowerCodes:lowerCodes, bitflags:bitflags, containsSpace:containsSpace, _lower:lower} +}; +const prepareBeginningIndexes = (target) => { + var targetLen = target.length; + var beginningIndexes = []; var beginningIndexesLen = 0; + var wasUpper = false; + var wasAlphanum = false; + for(var i = 0; i < targetLen; ++i) { + var targetCode = target.charCodeAt(i); + var isUpper = targetCode>=65&&targetCode<=90; + var isAlphanum = isUpper || targetCode>=97&&targetCode<=122 || targetCode>=48&&targetCode<=57; + var isBeginning = isUpper && !wasUpper || !wasAlphanum || !isAlphanum; + wasUpper = isUpper; + wasAlphanum = isAlphanum; + if(isBeginning) beginningIndexes[beginningIndexesLen++] = i; } - var prepareNextBeginningIndexes = (target) => { - target = remove_accents(target) - var targetLen = target.length - var beginningIndexes = prepareBeginningIndexes(target) - var nextBeginningIndexes = [] // new Array(targetLen) sparse array is too slow - var lastIsBeginning = beginningIndexes[0] - var lastIsBeginningI = 0 - for(var i = 0; i < targetLen; ++i) { - if(lastIsBeginning > i) { - nextBeginningIndexes[i] = lastIsBeginning - } else { - lastIsBeginning = beginningIndexes[++lastIsBeginningI] - nextBeginningIndexes[i] = lastIsBeginning===undefined ? targetLen : lastIsBeginning - } + return beginningIndexes +}; +const prepareNextBeginningIndexes = (target) => { + target = remove_accents(target); + var targetLen = target.length; + var beginningIndexes = prepareBeginningIndexes(target); + var nextBeginningIndexes = []; // new Array(targetLen) sparse array is too slow + var lastIsBeginning = beginningIndexes[0]; + var lastIsBeginningI = 0; + for(var i = 0; i < targetLen; ++i) { + if(lastIsBeginning > i) { + nextBeginningIndexes[i] = lastIsBeginning; + } else { + lastIsBeginning = beginningIndexes[++lastIsBeginningI]; + nextBeginningIndexes[i] = lastIsBeginning===undefined ? targetLen : lastIsBeginning; } - return nextBeginningIndexes - } - - var preparedCache = new Map() - var preparedSearchCache = new Map() - - // the theory behind these being globals is to reduce garbage collection by not making new arrays - var matchesSimple = []; var matchesStrict = [] - var nextBeginningIndexesChanges = [] // allows straw berry to match strawberry well, by modifying the end of a substring to be considered a beginning index for the rest of the search - var keysSpacesBestScores = []; var allowPartialMatchScores = [] - var tmpTargets = []; var tmpResults = [] - - // prop = 'key' 2.5ms optimized for this case, seems to be about as fast as direct obj[prop] - // prop = 'key1.key2' 10ms - // prop = ['key1', 'key2'] 27ms - // prop = obj => obj.tags.join() ??ms - var getValue = (obj, prop) => { - var tmp = obj[prop]; if(tmp !== undefined) return tmp - if(typeof prop === 'function') return prop(obj) // this should run first. but that makes string props slower - var segs = prop - if(!Array.isArray(prop)) segs = prop.split('.') - var len = segs.length - var i = -1 - while (obj && (++i < len)) obj = obj[segs[i]] - return obj } - - var isPrepared = (x) => { return typeof x === 'object' && typeof x._bitflags === 'number' } - var INFINITY = Infinity; var NEGATIVE_INFINITY = -INFINITY - var noResults = []; noResults.total = 0 - var NULL = null - - var noTarget = prepare('') - - // Hacked version of https://github.com/lemire/FastPriorityQueue.js - var fastpriorityqueue=r=>{var e=[],o=0,a={},v=r=>{for(var a=0,v=e[a],c=1;c>1]=e[a],c=1+(a<<1)}for(var f=a-1>>1;a>0&&v._score>1)e[a]=e[f];e[a]=v};return a.add=(r=>{var a=o;e[o++]=r;for(var v=a-1>>1;a>0&&r._score>1)e[a]=e[v];e[a]=r}),a.poll=(r=>{if(0!==o){var a=e[0];return e[0]=e[--o],v(),a}}),a.peek=(r=>{if(0!==o)return e[0]}),a.replaceTop=(r=>{e[0]=r,v()}),a} - var q = fastpriorityqueue() // reuse this - - // fuzzysort is written this way for minification. all names are mangeled unless quoted - return {'single':single, 'go':go, 'prepare':prepare, 'cleanup':cleanup} -}) // UMD + return nextBeginningIndexes +}; + +const preparedCache = new Map(); +const preparedSearchCache = new Map(); + +// the theory behind these being globals is to reduce garbage collection by not making new arrays +var matchesSimple = []; var matchesStrict = []; +var nextBeginningIndexesChanges = []; // allows straw berry to match strawberry well, by modifying the end of a substring to be considered a beginning index for the rest of the search +var keysSpacesBestScores = []; var allowPartialMatchScores = []; +var tmpTargets = []; var tmpResults = []; + +// prop = 'key' 2.5ms optimized for this case, seems to be about as fast as direct obj[prop] +// prop = 'key1.key2' 10ms +// prop = ['key1', 'key2'] 27ms +// prop = obj => obj.tags.join() ??ms +const getValue = (obj, prop) => { + var tmp = obj[prop]; if(tmp !== undefined) return tmp + if(typeof prop === 'function') return prop(obj) // this should run first. but that makes string props slower + var segs = prop; + if(!Array.isArray(prop)) segs = prop.split('.'); + var len = segs.length; + var i = -1; + while (obj && (++i < len)) obj = obj[segs[i]]; + return obj +}; + +const isPrepared = (x) => { return typeof x === 'object' && typeof x._bitflags === 'number' }; +const INFINITY = Infinity; const NEGATIVE_INFINITY = -INFINITY; +const noResults = []; noResults.total = 0; +const NULL = null; + +const noTarget = prepare(''); + +// Hacked version of https://github.com/lemire/FastPriorityQueue.js +const fastpriorityqueue=r=>{var e=[],o=0,a={},v=r=>{for(var a=0,v=e[a],c=1;c>1]=e[a],c=1+(a<<1);}for(var f=a-1>>1;a>0&&v._score>1)e[a]=e[f];e[a]=v;};return a.add=(r=>{var a=o;e[o++]=r;for(var v=a-1>>1;a>0&&r._score>1)e[a]=e[v];e[a]=r;}),a.poll=(r=>{if(0!==o){var a=e[0];return e[0]=e[--o],v(),a}}),a.peek=(r=>{if(0!==o)return e[0]}),a.replaceTop=(r=>{e[0]=r,v();}),a}; +const q = fastpriorityqueue(); // reuse this + +var fuzzysort = {single, go, prepare, cleanup}; + +exports.cleanup = cleanup; +exports.default = fuzzysort; +exports.go = go; +exports.prepare = prepare; +exports.single = single; +//# sourceMappingURL=fuzzysort.js.map diff --git a/fuzzysort.min.js b/fuzzysort.min.js index 5f61751..65a447a 100644 --- a/fuzzysort.min.js +++ b/fuzzysort.min.js @@ -1,2 +1,2 @@ -// https://github.com/farzher/fuzzysort v3.1.0 -((r,e)=>{"function"==typeof define&&define.amd?define([],e):"object"==typeof module&&module.exports?module.exports=e():r.fuzzysort=e()})(this,c=>{var f=r=>{"number"==typeof r?r=""+r:"string"!=typeof r&&(r="");var e=u(r);return x(r,{t:e.i,o:e.v,u:e.l})};class M{get["indexes"](){return this.p.slice(0,this.p.g).sort((r,e)=>r-e)}set["indexes"](r){return this.p=r}["highlight"](r,e){return((r,e="",f="")=>{for(var t="function"==typeof e?e:void 0,i=r.target,a=i.length,o=r.indexes,n="",v=0,u=0,s=!1,l=[],c=0;c{var f=new M;return f.target=r,f.obj=e.obj??Q,f.h=e.h??O,f.p=e.p??[],f.t=e.t??"",f.o=e.o??Q,f.k=e.k??Q,f.u=e.u??0,f},e=r=>r===O?0:10===r?O:1{"number"==typeof r?r=""+r:"string"!=typeof r&&(r=""),r=r.trim();var e=u(r),f=[];if(e.S)for(var t,i=r.split(/\s+/),i=[...new Set(i)],a=0;a{var e;return 999{var e;return 999{if(!1===f&&r.S)return C(r,e,t);for(var f=r.i,i=r.v,a=i[0],o=e.o,n=i.length,v=o.length,u=0,s=0,l=0;;){if(a===o[s]){if(q[l++]=s,++u===n)break;a=i[u]}if(v<=++s)return Q}var u=0,c=!1,p=0,b=e.k,d=(b===Q&&(b=e.k=N(e.target)),0);if((s=0===q[0]?0:b[q[0]-1])!==v)for(;;)if(v<=s){if(u<=0)break;if(200<++d)break;--u;var w=z[--p],s=b[w]}else if(i[u]===o[s]){if(z[p++]=s,++u===n){c=!0;break}++s}else s=b[s];var g=n<=1?-1:e.t.indexOf(f,q[0]),h=!!~g,y=h&&(0===g||e.k[g-1]===g);if(h&&!y)for(var k=0;k{for(var e=0,f=0,t=1;t{for(var t=new Set,i=0,a=Q,o=0,n=r._,v=n.length,u=0,s=()=>{for(let r=u-1;0<=r;r--)e.k[S[2*r+0]]=S[2*r+1]},l=!1,c=0;ci){if(f)for(c=0;cr.replace(/\p{Script=Latin}+/gu,r=>r.normalize("NFD")).replace(/[\u0300-\u036f]/g,""),u=r=>{for(var e=(r=v(r)).length,f=r.toLowerCase(),t=[],i=0,a=!1,o=0;o{for(var e=r.length,f=[],t=0,i=!1,a=!1,o=0;o{for(var e=(r=v(r)).length,f=s(r),t=[],i=f[0],a=0,o=0;o{var f=r[e];if(void 0!==f)return f;if("function"==typeof e)return e(r);for(var t=e,i=(t=Array.isArray(e)?t:e.split(".")).length,a=-1;r&&++a"object"==typeof r&&"number"==typeof r.u,K=1/0,O=-K,P=[],Q=(P.total=0,null),R=f(""),T=(o=[],n=0,t=r=>{for(var e=o[i=0],f=1;f>1]=o[i],f=1+(i<<1)}for(var a=i-1>>1;0>1)o[i]=o[a];o[i]=e},(r={}).add=r=>{var e=n;o[n++]=r;for(var f=e-1>>1;0>1)o[e]=o[f];o[e]=r},r.m=r=>{var e;if(0!==n)return e=o[0],o[0]=o[--n],t(),e},r.M=r=>{if(0!==n)return o[0]},r.C=r=>{o[0]=r,t()},r);return{single:(r,e)=>{var f;return!r||!e||(r=D(r),J(e)||(e=L(e)),((f=r.l)&e.u)!==f)?Q:F(r,e)},go:(r,e,f)=>{if(!r)return f?.all?((r,e)=>{var f=[],t=(f.total=r.length,e?.limit||K);if(e?.key)for(var i=0;i=t)return f}else if(e?.keys)for(var i=0;i=0;--u){var o=I(a,e.keys[u]);if(!o){v[u]=R;continue}if(!J(o))o=L(o);o.h=O;o.p.g=0;v[u]=o}v.obj=a;v.h=O;f.push(v);if(f.length>=t)return f}else for(var i=0;i=t)return f}return f})(e,f):P;var t=D(r),i=t.l,a=t.S,o=A(f?.threshold||0),n=f?.limit||K,v=0,u=0,s=e.length;function l(r){vT.M().h&&T.C(r))}if(f?.key)for(var c=f.key,p=0;pO&&(_=(B[r]+E[r])/4)>B[r]&&(B[r]=_),E[r]>B[r]&&(B[r]=E[r]);if(a){for(let r=0;r{a.clear(),l.clear()}}}); +!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports):"function"==typeof define&&define.amd?define(["exports"],r):r((e="undefined"!=typeof globalThis?globalThis:e||self).fuzzysort={})}(this,(function(e){"use strict";const r=(e,r)=>{if(!e||!r)return z;var t=_(e);I(r)||(r=g(r));var n=t.bitflags;return(n&r._bitflags)!==n?z:d(t,r)},t=(e,r,t)=>{if(!e)return t?.all?u(r,t):T;var n=_(e),s=n.bitflags,o=n.containsSpace,a=l(t?.threshold||0),f=t?.limit||A,c=0,v=0,h=r.length;function p(e){cO.peek()._score&&O.replaceTop(e))}if(t?.key)for(var x=t.key,b=0;b-1e3)if(S[e]>M)(q=(S[e]+L[e])/4)>S[e]&&(S[e]=q);L[e]>S[e]&&(S[e]=L[e])}}else m[D]=F;else m[D]=F;if(o){for(let e=0;e-1e3)if(P>M)(q=(P+H._score)/4)>P&&(P=q);H._score>P&&(P=H._score)}}if(N.obj=w,N._score=P,t?.scoreFn){if(!(P=t.scoreFn(N)))continue;P=l(P),N._score=P}P=0;--b)J[b]=O.poll();return J.total=c+v,J},n=e=>{"number"==typeof e?e=""+e:"string"!=typeof e&&(e="");var r=p(e);return a(e,{_targetLower:r._lower,_targetLowerCodes:r.lowerCodes,_bitflags:r.bitflags})},s=()=>{b.clear(),w.clear()};class o{get indexes(){return this._indexes.slice(0,this._indexes.len).sort(((e,r)=>e-r))}set indexes(e){return this._indexes=e}highlight(e,r){return((e,r="",t="")=>{for(var n="function"==typeof r?r:void 0,s=e.target,o=s.length,i=e.indexes,a="",f=0,l=0,c=!1,g=[],_=0;_{const t=new o;return t.target=e,t.obj=r.obj??z,t._score=r._score??M,t._indexes=r._indexes??[],t._targetLower=r._targetLower??"",t._targetLowerCodes=r._targetLowerCodes??z,t._nextBeginningIndexes=r._nextBeginningIndexes??z,t._bitflags=r._bitflags??0,t},f=e=>e===M?0:e>1?e:Math.E**(-2*((1-e)**.04307-1)),l=e=>0===e?M:e>1?e:1-Math.pow(Math.log(e)/-2+1,1/.04307),c=e=>{"number"==typeof e?e=""+e:"string"!=typeof e&&(e=""),e=e.trim();var r=p(e),t=[];if(r.containsSpace){var n=e.split(/\s+/);n=[...new Set(n)];for(var s=0;s{if(e.length>999)return n(e);var r=b.get(e);return void 0!==r||(r=n(e),b.set(e,r)),r},_=e=>{if(e.length>999)return c(e);var r=w.get(e);return void 0!==r||(r=c(e),w.set(e,r)),r},u=(e,r)=>{var t=[];t.total=e.length;var n=r?.limit||A;if(r?.key)for(var s=0;s=n)return t}}else if(r?.keys)for(s=0;s=0;--c){(_=B(o,r.keys[c]))?(I(_)||(_=g(_)),_._score=M,_._indexes.len=0,l[c]=_):l[c]=F}if(l.obj=o,l._score=M,t.push(l),t.length>=n)return t}else for(s=0;s=n))return t}return t},d=(e,r,t=!1,n=!1)=>{if(!1===t&&e.containsSpace)return v(e,r,n);for(var s=e._lower,i=e.lowerCodes,a=i[0],f=r._targetLowerCodes,l=i.length,c=f.length,g=0,_=0,u=0;;){if(a===f[_]){if(y[u++]=_,++g===l)break;a=i[g]}if(++_>=c)return z}g=0;var d=!1,h=0,p=r._nextBeginningIndexes;p===z&&(p=r._nextBeginningIndexes=x(r.target));var b=0;if((_=0===y[0]?0:p[y[0]-1])!==c)for(;;)if(_>=c){if(g<=0)break;if(++b>200)break;--g;var w=k[--h];_=p[w]}else{if(i[g]===f[_]){if(k[h++]=_,++g===l){d=!0;break}++_}else _=p[_]}var C=l<=1?-1:r._targetLower.indexOf(s,y[0]),S=!!~C,L=!!S&&(0===C||r._nextBeginningIndexes[C-1]===C);if(S&&!L)for(var j=0;j{for(var r=0,t=0,n=1;n24&&(r*=10*(s-24))}else r*=1e3;return r-=(c-l)/2,S&&(r/=1+l*l*1),L&&(r/=1+l*l*1),r-=(c-l)/2};if(d)if(L){for(j=0;j{for(var n=new Set,s=0,o=z,i=0,a=e.spaceSearches,f=a.length,l=0,c=()=>{for(let e=l-1;e>=0;e--)r._nextBeginningIndexes[C[2*e+0]]=C[2*e+1]},g=!1,_=0;_=0&&x===r._nextBeginningIndexes[e];e--)r._nextBeginningIndexes[e]=p,C[2*l+0]=e,C[2*l+1]=x,l++}}s+=o._score/f,L[_]=o._score/f,o._indexes[0]s){if(t)for(_=0;_e.replace(/\p{Script=Latin}+/gu,(e=>e.normalize("NFD"))).replace(/[\u0300-\u036f]/g,""),p=e=>{for(var r=(e=h(e)).length,t=e.toLowerCase(),n=[],s=0,o=!1,i=0;i=97&&a<=122?a-97:a>=48&&a<=57?26:a<=127?30:31);else o=!0}return{lowerCodes:n,bitflags:s,containsSpace:o,_lower:t}},x=e=>{for(var r=(e=h(e)).length,t=(e=>{for(var r=e.length,t=[],n=0,s=!1,o=!1,i=0;i=65&&a<=90,l=f||a>=97&&a<=122||a>=48&&a<=57,c=f&&!s||!o||!l;s=f,o=l,c&&(t[n++]=i)}return t})(e),n=[],s=t[0],o=0,i=0;ii?n[i]=s:(s=t[++o],n[i]=void 0===s?r:s);return n},b=new Map,w=new Map;var y=[],k=[],C=[],S=[],L=[],j=[],m=[];const B=(e,r)=>{var t=e[r];if(void 0!==t)return t;if("function"==typeof r)return r(e);var n=r;Array.isArray(r)||(n=r.split("."));for(var s=n.length,o=-1;e&&++o"object"==typeof e&&"number"==typeof e._bitflags,A=1/0,M=-A,T=[];T.total=0;const z=null,F=n(""),O=(D=[],E=0,P=e=>{for(var r=0,t=D[r],n=1;n>1]=D[r],n=1+(r<<1)}for(var o=r-1>>1;r>0&&t._score>1)D[r]=D[o];D[r]=t},(N={}).add=e=>{var r=E;D[E++]=e;for(var t=r-1>>1;r>0&&e._score>1)D[r]=D[t];D[r]=e},N.poll=e=>{if(0!==E){var r=D[0];return D[0]=D[--E],P(),r}},N.peek=e=>{if(0!==E)return D[0]},N.replaceTop=e=>{D[0]=e,P()},N);var D,E,N,P,q={single:r,go:t,prepare:n,cleanup:s};e.cleanup=s,e.default=q,e.go=t,e.prepare=n,e.single=r,Object.defineProperty(e,"__esModule",{value:!0})})); +//# sourceMappingURL=fuzzysort.min.js.map diff --git a/fuzzysort.mjs b/fuzzysort.mjs new file mode 100644 index 0000000..ca3b66e --- /dev/null +++ b/fuzzysort.mjs @@ -0,0 +1,682 @@ +// https://github.com/farzher/fuzzysort v3.1.0 +const single = (search, target) => { + if(!search || !target) return NULL + + var preparedSearch = getPreparedSearch(search); + if(!isPrepared(target)) target = getPrepared(target); + + var searchBitflags = preparedSearch.bitflags; + if((searchBitflags & target._bitflags) !== searchBitflags) return NULL + + return algorithm(preparedSearch, target) +}; + +const go = (search, targets, options) => { + if(!search) return options?.all ? all(targets, options) : noResults + + var preparedSearch = getPreparedSearch(search); + var searchBitflags = preparedSearch.bitflags; + var containsSpace = preparedSearch.containsSpace; + + var threshold = denormalizeScore( options?.threshold || 0 ); + var limit = options?.limit || INFINITY; + + var resultsLen = 0; var limitedCount = 0; + var targetsLen = targets.length; + + function push_result(result) { + if(resultsLen < limit) { q.add(result); ++resultsLen; } + else { + ++limitedCount; + if(result._score > q.peek()._score) q.replaceTop(result); + } + } + + // This code is copy/pasted 3 times for performance reasons [options.key, options.keys, no keys] + + // options.key + if(options?.key) { + var key = options.key; + for(var i = 0; i < targetsLen; ++i) { var obj = targets[i]; + var target = getValue(obj, key); + if(!target) continue + if(!isPrepared(target)) target = getPrepared(target); + + if((searchBitflags & target._bitflags) !== searchBitflags) continue + var result = algorithm(preparedSearch, target); + if(result === NULL) continue + if(result._score < threshold) continue + + result.obj = obj; + push_result(result); + } + + // options.keys + } else if(options?.keys) { + var keys = options.keys; + var keysLen = keys.length; + + outer: for(var i = 0; i < targetsLen; ++i) { var obj = targets[i]; + + { // early out based on bitflags + var keysBitflags = 0; + for (var keyI = 0; keyI < keysLen; ++keyI) { + var key = keys[keyI]; + var target = getValue(obj, key); + if(!target) { tmpTargets[keyI] = noTarget; continue } + if(!isPrepared(target)) target = getPrepared(target); + tmpTargets[keyI] = target; + + keysBitflags |= target._bitflags; + } + + if((searchBitflags & keysBitflags) !== searchBitflags) continue + } + + if(containsSpace) for(let i=0; i -1000) { + if(keysSpacesBestScores[i] > NEGATIVE_INFINITY) { + var tmp = (keysSpacesBestScores[i] + allowPartialMatchScores[i]) / 4;/*bonus score for having multiple matches*/ + if(tmp > keysSpacesBestScores[i]) keysSpacesBestScores[i] = tmp; + } + } + if(allowPartialMatchScores[i] > keysSpacesBestScores[i]) keysSpacesBestScores[i] = allowPartialMatchScores[i]; + } + } + + if(containsSpace) { + for(let i=0; i -1000) { + if(score > NEGATIVE_INFINITY) { + var tmp = (score + result._score) / 4;/*bonus score for having multiple matches*/ + if(tmp > score) score = tmp; + } + } + if(result._score > score) score = result._score; + } + } + + objResults.obj = obj; + objResults._score = score; + if(options?.scoreFn) { + score = options.scoreFn(objResults); + if(!score) continue + score = denormalizeScore(score); + objResults._score = score; + } + + if(score < threshold) continue + push_result(objResults); + } + + // no keys + } else { + for(var i = 0; i < targetsLen; ++i) { var target = targets[i]; + if(!target) continue + if(!isPrepared(target)) target = getPrepared(target); + + if((searchBitflags & target._bitflags) !== searchBitflags) continue + var result = algorithm(preparedSearch, target); + if(result === NULL) continue + if(result._score < threshold) continue + + push_result(result); + } + } + + if(resultsLen === 0) return noResults + var results = new Array(resultsLen); + for(var i = resultsLen - 1; i >= 0; --i) results[i] = q.poll(); + results.total = resultsLen + limitedCount; + return results +}; + + +// this is written as 1 function instead of 2 for minification. perf seems fine ... +// except when minified. the perf is very slow +const highlight = (result, open='', close='') => { + var callback = typeof open === 'function' ? open : undefined; + + var target = result.target; + var targetLen = target.length; + var indexes = result.indexes; + var highlighted = ''; + var matchI = 0; + var indexesI = 0; + var opened = false; + var parts = []; + + for(var i = 0; i < targetLen; ++i) { var char = target[i]; + if(indexes[indexesI] === i) { + ++indexesI; + if(!opened) { opened = true; + if(callback) { + parts.push(highlighted); highlighted = ''; + } else { + highlighted += open; + } + } + + if(indexesI === indexes.length) { + if(callback) { + highlighted += char; + parts.push(callback(highlighted, matchI++)); highlighted = ''; + parts.push(target.substr(i+1)); + } else { + highlighted += char + close + target.substr(i+1); + } + break + } + } else { + if(opened) { opened = false; + if(callback) { + parts.push(callback(highlighted, matchI++)); highlighted = ''; + } else { + highlighted += close; + } + } + } + highlighted += char; + } + + return callback ? parts : highlighted +}; + + +const prepare = (target) => { + if(typeof target === 'number') target = ''+target; + else if(typeof target !== 'string') target = ''; + var info = prepareLowerInfo(target); + return new_result(target, {_targetLower:info._lower, _targetLowerCodes:info.lowerCodes, _bitflags:info.bitflags}) +}; + +const cleanup = () => { preparedCache.clear(); preparedSearchCache.clear(); }; + + +// Below this point is only internal code +// Below this point is only internal code +// Below this point is only internal code +// Below this point is only internal code + + +class Result { + get ['indexes']() { return this._indexes.slice(0, this._indexes.len).sort((a,b)=>a-b) } + set ['indexes'](indexes) { return this._indexes = indexes } + ['highlight'](open, close) { return highlight(this, open, close) } + get ['score']() { return normalizeScore(this._score) } + set ['score'](score) { this._score = denormalizeScore(score); } +} + +class KeysResult extends Array { + get ['score']() { return normalizeScore(this._score) } + set ['score'](score) { this._score = denormalizeScore(score); } +} + +const new_result = (target, options) => { + const result = new Result(); + result['target'] = target; + result['obj'] = options.obj ?? NULL; + result._score = options._score ?? NEGATIVE_INFINITY; + result._indexes = options._indexes ?? []; + result._targetLower = options._targetLower ?? ''; + result._targetLowerCodes = options._targetLowerCodes ?? NULL; + result._nextBeginningIndexes = options._nextBeginningIndexes ?? NULL; + result._bitflags = options._bitflags ?? 0; + return result +}; + + +const normalizeScore = score => { + if(score === NEGATIVE_INFINITY) return 0 + if(score > 1) return score + return Math.E ** ( ((-score + 1)**.04307 - 1) * -2) +}; +const denormalizeScore = normalizedScore => { + if(normalizedScore === 0) return NEGATIVE_INFINITY + if(normalizedScore > 1) return normalizedScore + return 1 - Math.pow((Math.log(normalizedScore) / -2 + 1), 1 / 0.04307) +}; + + +const prepareSearch = (search) => { + if(typeof search === 'number') search = ''+search; + else if(typeof search !== 'string') search = ''; + search = search.trim(); + var info = prepareLowerInfo(search); + + var spaceSearches = []; + if(info.containsSpace) { + var searches = search.split(/\s+/); + searches = [...new Set(searches)]; // distinct + for(var i=0; i { + if(target.length > 999) return prepare(target) // don't cache huge targets + var targetPrepared = preparedCache.get(target); + if(targetPrepared !== undefined) return targetPrepared + targetPrepared = prepare(target); + preparedCache.set(target, targetPrepared); + return targetPrepared +}; +const getPreparedSearch = (search) => { + if(search.length > 999) return prepareSearch(search) // don't cache huge searches + var searchPrepared = preparedSearchCache.get(search); + if(searchPrepared !== undefined) return searchPrepared + searchPrepared = prepareSearch(search); + preparedSearchCache.set(search, searchPrepared); + return searchPrepared +}; + + +const all = (targets, options) => { + var results = []; results.total = targets.length; // this total can be wrong if some targets are skipped + + var limit = options?.limit || INFINITY; + + if(options?.key) { + for(var i=0;i= limit) return results + } + } else if(options?.keys) { + for(var i=0;i= 0; --keyI) { + var target = getValue(obj, options.keys[keyI]); + if(!target) { objResults[keyI] = noTarget; continue } + if(!isPrepared(target)) target = getPrepared(target); + target._score = NEGATIVE_INFINITY; + target._indexes.len = 0; + objResults[keyI] = target; + } + objResults.obj = obj; + objResults._score = NEGATIVE_INFINITY; + results.push(objResults); if(results.length >= limit) return results + } + } else { + for(var i=0;i= limit) return results + } + } + + return results +}; + + +const algorithm = (preparedSearch, prepared, allowSpaces=false, allowPartialMatch=false) => { + if(allowSpaces===false && preparedSearch.containsSpace) return algorithmSpaces(preparedSearch, prepared, allowPartialMatch) + + var searchLower = preparedSearch._lower; + var searchLowerCodes = preparedSearch.lowerCodes; + var searchLowerCode = searchLowerCodes[0]; + var targetLowerCodes = prepared._targetLowerCodes; + var searchLen = searchLowerCodes.length; + var targetLen = targetLowerCodes.length; + var searchI = 0; // where we at + var targetI = 0; // where you at + var matchesSimpleLen = 0; + + // very basic fuzzy match; to remove non-matching targets ASAP! + // walk through target. find sequential matches. + // if all chars aren't found then exit + for(;;) { + var isMatch = searchLowerCode === targetLowerCodes[targetI]; + if(isMatch) { + matchesSimple[matchesSimpleLen++] = targetI; + ++searchI; if(searchI === searchLen) break + searchLowerCode = searchLowerCodes[searchI]; + } + ++targetI; if(targetI >= targetLen) return NULL // Failed to find searchI + } + + var searchI = 0; + var successStrict = false; + var matchesStrictLen = 0; + + var nextBeginningIndexes = prepared._nextBeginningIndexes; + if(nextBeginningIndexes === NULL) nextBeginningIndexes = prepared._nextBeginningIndexes = prepareNextBeginningIndexes(prepared.target); + targetI = matchesSimple[0]===0 ? 0 : nextBeginningIndexes[matchesSimple[0]-1]; + + // Our target string successfully matched all characters in sequence! + // Let's try a more advanced and strict test to improve the score + // only count it as a match if it's consecutive or a beginning character! + var backtrackCount = 0; + if(targetI !== targetLen) for(;;) { + if(targetI >= targetLen) { + // We failed to find a good spot for this search char, go back to the previous search char and force it forward + if(searchI <= 0) break // We failed to push chars forward for a better match + + ++backtrackCount; if(backtrackCount > 200) break // exponential backtracking is taking too long, just give up and return a bad match + + --searchI; + var lastMatch = matchesStrict[--matchesStrictLen]; + targetI = nextBeginningIndexes[lastMatch]; + + } else { + var isMatch = searchLowerCodes[searchI] === targetLowerCodes[targetI]; + if(isMatch) { + matchesStrict[matchesStrictLen++] = targetI; + ++searchI; if(searchI === searchLen) { successStrict = true; break } + ++targetI; + } else { + targetI = nextBeginningIndexes[targetI]; + } + } + } + + // check if it's a substring match + var substringIndex = searchLen <= 1 ? -1 : prepared._targetLower.indexOf(searchLower, matchesSimple[0]); // perf: this is slow + var isSubstring = !!~substringIndex; + var isSubstringBeginning = !isSubstring ? false : substringIndex===0 || prepared._nextBeginningIndexes[substringIndex-1] === substringIndex; + + // if it's a substring match but not at a beginning index, let's try to find a substring starting at a beginning index for a better score + if(isSubstring && !isSubstringBeginning) { + for(var i=0; i { + var score = 0; + + var extraMatchGroupCount = 0; + for(var i = 1; i < searchLen; ++i) { + if(matches[i] - matches[i-1] !== 1) {score -= matches[i]; ++extraMatchGroupCount;} + } + var unmatchedDistance = matches[searchLen-1] - matches[0] - (searchLen-1); + + score -= (12+unmatchedDistance) * extraMatchGroupCount; // penality for more groups + + if(matches[0] !== 0) score -= matches[0]*matches[0]*.2; // penality for not starting near the beginning + + if(!successStrict) { + score *= 1000; + } else { + // successStrict on a target with too many beginning indexes loses points for being a bad target + var uniqueBeginningIndexes = 1; + for(var i = nextBeginningIndexes[0]; i < targetLen; i=nextBeginningIndexes[i]) ++uniqueBeginningIndexes; + + if(uniqueBeginningIndexes > 24) score *= (uniqueBeginningIndexes-24)*10; // quite arbitrary numbers here ... + } + + score -= (targetLen - searchLen)/2; // penality for longer targets + + if(isSubstring) score /= 1+searchLen*searchLen*1; // bonus for being a full substring + if(isSubstringBeginning) score /= 1+searchLen*searchLen*1; // bonus for substring starting on a beginningIndex + + score -= (targetLen - searchLen)/2; // penality for longer targets + + return score + }; + + if(!successStrict) { + if(isSubstring) for(var i=0; i { + var seen_indexes = new Set(); + var score = 0; + var result = NULL; + + var first_seen_index_last_search = 0; + var searches = preparedSearch.spaceSearches; + var searchesLen = searches.length; + var changeslen = 0; + + // Return _nextBeginningIndexes back to its normal state + var resetNextBeginningIndexes = () => { + for(let i=changeslen-1; i>=0; i--) target._nextBeginningIndexes[nextBeginningIndexesChanges[i*2 + 0]] = nextBeginningIndexesChanges[i*2 + 1]; + }; + + var hasAtLeast1Match = false; + for(var i=0; i=0; i--) { + if(toReplace !== target._nextBeginningIndexes[i]) break + target._nextBeginningIndexes[i] = newBeginningIndex; + nextBeginningIndexesChanges[changeslen*2 + 0] = i; + nextBeginningIndexesChanges[changeslen*2 + 1] = toReplace; + changeslen++; + } + } + } + + score += result._score / searchesLen; + allowPartialMatchScores[i] = result._score / searchesLen; + + // dock points based on order otherwise "c man" returns Manifest.cpp instead of CheatManager.h + if(result._indexes[0] < first_seen_index_last_search) { + score -= (first_seen_index_last_search - result._indexes[0]) * 2; + } + first_seen_index_last_search = result._indexes[0]; + + for(var j=0; j score) { + if(allowPartialMatch) { + for(var i=0; i str.replace(/\p{Script=Latin}+/gu, match => match.normalize('NFD')).replace(/[\u0300-\u036f]/g, ''); + +const prepareLowerInfo = (str) => { + str = remove_accents(str); + var strLen = str.length; + var lower = str.toLowerCase(); + var lowerCodes = []; // new Array(strLen) sparse array is too slow + var bitflags = 0; + var containsSpace = false; // space isn't stored in bitflags because of how searching with a space works + + for(var i = 0; i < strLen; ++i) { + var lowerCode = lowerCodes[i] = lower.charCodeAt(i); + + if(lowerCode === 32) { + containsSpace = true; + continue // it's important that we don't set any bitflags for space + } + + var bit = lowerCode>=97&&lowerCode<=122 ? lowerCode-97 // alphabet + : lowerCode>=48&&lowerCode<=57 ? 26 // numbers + // 3 bits available + : lowerCode<=127 ? 30 // other ascii + : 31; // other utf8 + bitflags |= 1< { + var targetLen = target.length; + var beginningIndexes = []; var beginningIndexesLen = 0; + var wasUpper = false; + var wasAlphanum = false; + for(var i = 0; i < targetLen; ++i) { + var targetCode = target.charCodeAt(i); + var isUpper = targetCode>=65&&targetCode<=90; + var isAlphanum = isUpper || targetCode>=97&&targetCode<=122 || targetCode>=48&&targetCode<=57; + var isBeginning = isUpper && !wasUpper || !wasAlphanum || !isAlphanum; + wasUpper = isUpper; + wasAlphanum = isAlphanum; + if(isBeginning) beginningIndexes[beginningIndexesLen++] = i; + } + return beginningIndexes +}; +const prepareNextBeginningIndexes = (target) => { + target = remove_accents(target); + var targetLen = target.length; + var beginningIndexes = prepareBeginningIndexes(target); + var nextBeginningIndexes = []; // new Array(targetLen) sparse array is too slow + var lastIsBeginning = beginningIndexes[0]; + var lastIsBeginningI = 0; + for(var i = 0; i < targetLen; ++i) { + if(lastIsBeginning > i) { + nextBeginningIndexes[i] = lastIsBeginning; + } else { + lastIsBeginning = beginningIndexes[++lastIsBeginningI]; + nextBeginningIndexes[i] = lastIsBeginning===undefined ? targetLen : lastIsBeginning; + } + } + return nextBeginningIndexes +}; + +const preparedCache = new Map(); +const preparedSearchCache = new Map(); + +// the theory behind these being globals is to reduce garbage collection by not making new arrays +var matchesSimple = []; var matchesStrict = []; +var nextBeginningIndexesChanges = []; // allows straw berry to match strawberry well, by modifying the end of a substring to be considered a beginning index for the rest of the search +var keysSpacesBestScores = []; var allowPartialMatchScores = []; +var tmpTargets = []; var tmpResults = []; + +// prop = 'key' 2.5ms optimized for this case, seems to be about as fast as direct obj[prop] +// prop = 'key1.key2' 10ms +// prop = ['key1', 'key2'] 27ms +// prop = obj => obj.tags.join() ??ms +const getValue = (obj, prop) => { + var tmp = obj[prop]; if(tmp !== undefined) return tmp + if(typeof prop === 'function') return prop(obj) // this should run first. but that makes string props slower + var segs = prop; + if(!Array.isArray(prop)) segs = prop.split('.'); + var len = segs.length; + var i = -1; + while (obj && (++i < len)) obj = obj[segs[i]]; + return obj +}; + +const isPrepared = (x) => { return typeof x === 'object' && typeof x._bitflags === 'number' }; +const INFINITY = Infinity; const NEGATIVE_INFINITY = -INFINITY; +const noResults = []; noResults.total = 0; +const NULL = null; + +const noTarget = prepare(''); + +// Hacked version of https://github.com/lemire/FastPriorityQueue.js +const fastpriorityqueue=r=>{var e=[],o=0,a={},v=r=>{for(var a=0,v=e[a],c=1;c>1]=e[a],c=1+(a<<1);}for(var f=a-1>>1;a>0&&v._score>1)e[a]=e[f];e[a]=v;};return a.add=(r=>{var a=o;e[o++]=r;for(var v=a-1>>1;a>0&&r._score>1)e[a]=e[v];e[a]=r;}),a.poll=(r=>{if(0!==o){var a=e[0];return e[0]=e[--o],v(),a}}),a.peek=(r=>{if(0!==o)return e[0]}),a.replaceTop=(r=>{e[0]=r,v();}),a}; +const q = fastpriorityqueue(); // reuse this + +var fuzzysort = {single, go, prepare, cleanup}; + +export { cleanup, fuzzysort as default, go, prepare, single }; +//# sourceMappingURL=fuzzysort.mjs.map diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..f972c76 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,531 @@ +{ + "name": "fuzzysort", + "version": "3.1.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "fuzzysort", + "version": "3.1.0", + "license": "MIT", + "devDependencies": { + "@rollup/plugin-terser": "^0.4.4", + "rollup": "^4.27.3" + } + }, + "node_modules/@jridgewell/gen-mapping": { + "version": "0.3.5", + "resolved": "https://registry.npmjs.org/@jridgewell/gen-mapping/-/gen-mapping-0.3.5.tgz", + "integrity": "sha512-IzL8ZoEDIBRWEzlCcRhOaCupYyN5gdIK+Q6fbFdPDg6HqX6jpkItn7DFIpW9LQzXG6Df9sA7+OKnq0qlz/GaQg==", + "dev": true, + "license": "MIT", + "dependencies": { + "@jridgewell/set-array": "^1.2.1", + "@jridgewell/sourcemap-codec": "^1.4.10", + "@jridgewell/trace-mapping": "^0.3.24" + }, + "engines": { + "node": ">=6.0.0" + } + }, + "node_modules/@jridgewell/resolve-uri": { + "version": "3.1.2", + "resolved": "https://registry.npmjs.org/@jridgewell/resolve-uri/-/resolve-uri-3.1.2.tgz", + "integrity": "sha512-bRISgCIjP20/tbWSPWMEi54QVPRZExkuD9lJL+UIxUKtwVJA8wW1Trb1jMs1RFXo1CBTNZ/5hpC9QvmKWdopKw==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=6.0.0" + } + }, + "node_modules/@jridgewell/set-array": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/@jridgewell/set-array/-/set-array-1.2.1.tgz", + "integrity": "sha512-R8gLRTZeyp03ymzP/6Lil/28tGeGEzhx1q2k703KGWRAI1VdvPIXdG70VJc2pAMw3NA6JKL5hhFu1sJX0Mnn/A==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=6.0.0" + } + }, + "node_modules/@jridgewell/source-map": { + "version": "0.3.6", + "resolved": "https://registry.npmjs.org/@jridgewell/source-map/-/source-map-0.3.6.tgz", + "integrity": "sha512-1ZJTZebgqllO79ue2bm3rIGud/bOe0pP5BjSRCRxxYkEZS8STV7zN84UBbiYu7jy+eCKSnVIUgoWWE/tt+shMQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "@jridgewell/gen-mapping": "^0.3.5", + "@jridgewell/trace-mapping": "^0.3.25" + } + }, + "node_modules/@jridgewell/sourcemap-codec": { + "version": "1.5.0", + "resolved": "https://registry.npmjs.org/@jridgewell/sourcemap-codec/-/sourcemap-codec-1.5.0.tgz", + "integrity": "sha512-gv3ZRaISU3fjPAgNsriBRqGWQL6quFx04YMPW/zD8XMLsU32mhCCbfbO6KZFLjvYpCZ8zyDEgqsgf+PwPaM7GQ==", + "dev": true, + "license": "MIT" + }, + "node_modules/@jridgewell/trace-mapping": { + "version": "0.3.25", + "resolved": "https://registry.npmjs.org/@jridgewell/trace-mapping/-/trace-mapping-0.3.25.tgz", + "integrity": "sha512-vNk6aEwybGtawWmy/PzwnGDOjCkLWSD2wqvjGGAgOAwCGWySYXfYoxt00IJkTF+8Lb57DwOb3Aa0o9CApepiYQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "@jridgewell/resolve-uri": "^3.1.0", + "@jridgewell/sourcemap-codec": "^1.4.14" + } + }, + "node_modules/@rollup/plugin-terser": { + "version": "0.4.4", + "resolved": "https://registry.npmjs.org/@rollup/plugin-terser/-/plugin-terser-0.4.4.tgz", + "integrity": "sha512-XHeJC5Bgvs8LfukDwWZp7yeqin6ns8RTl2B9avbejt6tZqsqvVoWI7ZTQrcNsfKEDWBTnTxM8nMDkO2IFFbd0A==", + "dev": true, + "license": "MIT", + "dependencies": { + "serialize-javascript": "^6.0.1", + "smob": "^1.0.0", + "terser": "^5.17.4" + }, + "engines": { + "node": ">=14.0.0" + }, + "peerDependencies": { + "rollup": "^2.0.0||^3.0.0||^4.0.0" + }, + "peerDependenciesMeta": { + "rollup": { + "optional": true + } + } + }, + "node_modules/@rollup/rollup-android-arm-eabi": { + "version": "4.27.4", + "resolved": "https://registry.npmjs.org/@rollup/rollup-android-arm-eabi/-/rollup-android-arm-eabi-4.27.4.tgz", + "integrity": "sha512-2Y3JT6f5MrQkICUyRVCw4oa0sutfAsgaSsb0Lmmy1Wi2y7X5vT9Euqw4gOsCyy0YfKURBg35nhUKZS4mDcfULw==", + "cpu": [ + "arm" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "android" + ] + }, + "node_modules/@rollup/rollup-android-arm64": { + "version": "4.27.4", + "resolved": "https://registry.npmjs.org/@rollup/rollup-android-arm64/-/rollup-android-arm64-4.27.4.tgz", + "integrity": "sha512-wzKRQXISyi9UdCVRqEd0H4cMpzvHYt1f/C3CoIjES6cG++RHKhrBj2+29nPF0IB5kpy9MS71vs07fvrNGAl/iA==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "android" + ] + }, + "node_modules/@rollup/rollup-darwin-arm64": { + "version": "4.27.4", + "resolved": "https://registry.npmjs.org/@rollup/rollup-darwin-arm64/-/rollup-darwin-arm64-4.27.4.tgz", + "integrity": "sha512-PlNiRQapift4LNS8DPUHuDX/IdXiLjf8mc5vdEmUR0fF/pyy2qWwzdLjB+iZquGr8LuN4LnUoSEvKRwjSVYz3Q==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ] + }, + "node_modules/@rollup/rollup-darwin-x64": { + "version": "4.27.4", + "resolved": "https://registry.npmjs.org/@rollup/rollup-darwin-x64/-/rollup-darwin-x64-4.27.4.tgz", + "integrity": "sha512-o9bH2dbdgBDJaXWJCDTNDYa171ACUdzpxSZt+u/AAeQ20Nk5x+IhA+zsGmrQtpkLiumRJEYef68gcpn2ooXhSQ==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ] + }, + "node_modules/@rollup/rollup-freebsd-arm64": { + "version": "4.27.4", + "resolved": "https://registry.npmjs.org/@rollup/rollup-freebsd-arm64/-/rollup-freebsd-arm64-4.27.4.tgz", + "integrity": "sha512-NBI2/i2hT9Q+HySSHTBh52da7isru4aAAo6qC3I7QFVsuhxi2gM8t/EI9EVcILiHLj1vfi+VGGPaLOUENn7pmw==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "freebsd" + ] + }, + "node_modules/@rollup/rollup-freebsd-x64": { + "version": "4.27.4", + "resolved": "https://registry.npmjs.org/@rollup/rollup-freebsd-x64/-/rollup-freebsd-x64-4.27.4.tgz", + "integrity": "sha512-wYcC5ycW2zvqtDYrE7deary2P2UFmSh85PUpAx+dwTCO9uw3sgzD6Gv9n5X4vLaQKsrfTSZZ7Z7uynQozPVvWA==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "freebsd" + ] + }, + "node_modules/@rollup/rollup-linux-arm-gnueabihf": { + "version": "4.27.4", + "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-arm-gnueabihf/-/rollup-linux-arm-gnueabihf-4.27.4.tgz", + "integrity": "sha512-9OwUnK/xKw6DyRlgx8UizeqRFOfi9mf5TYCw1uolDaJSbUmBxP85DE6T4ouCMoN6pXw8ZoTeZCSEfSaYo+/s1w==", + "cpu": [ + "arm" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ] + }, + "node_modules/@rollup/rollup-linux-arm-musleabihf": { + "version": "4.27.4", + "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-arm-musleabihf/-/rollup-linux-arm-musleabihf-4.27.4.tgz", + "integrity": "sha512-Vgdo4fpuphS9V24WOV+KwkCVJ72u7idTgQaBoLRD0UxBAWTF9GWurJO9YD9yh00BzbkhpeXtm6na+MvJU7Z73A==", + "cpu": [ + "arm" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ] + }, + "node_modules/@rollup/rollup-linux-arm64-gnu": { + "version": "4.27.4", + "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-arm64-gnu/-/rollup-linux-arm64-gnu-4.27.4.tgz", + "integrity": "sha512-pleyNgyd1kkBkw2kOqlBx+0atfIIkkExOTiifoODo6qKDSpnc6WzUY5RhHdmTdIJXBdSnh6JknnYTtmQyobrVg==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ] + }, + "node_modules/@rollup/rollup-linux-arm64-musl": { + "version": "4.27.4", + "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-arm64-musl/-/rollup-linux-arm64-musl-4.27.4.tgz", + "integrity": "sha512-caluiUXvUuVyCHr5DxL8ohaaFFzPGmgmMvwmqAITMpV/Q+tPoaHZ/PWa3t8B2WyoRcIIuu1hkaW5KkeTDNSnMA==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ] + }, + "node_modules/@rollup/rollup-linux-powerpc64le-gnu": { + "version": "4.27.4", + "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-powerpc64le-gnu/-/rollup-linux-powerpc64le-gnu-4.27.4.tgz", + "integrity": "sha512-FScrpHrO60hARyHh7s1zHE97u0KlT/RECzCKAdmI+LEoC1eDh/RDji9JgFqyO+wPDb86Oa/sXkily1+oi4FzJQ==", + "cpu": [ + "ppc64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ] + }, + "node_modules/@rollup/rollup-linux-riscv64-gnu": { + "version": "4.27.4", + "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-riscv64-gnu/-/rollup-linux-riscv64-gnu-4.27.4.tgz", + "integrity": "sha512-qyyprhyGb7+RBfMPeww9FlHwKkCXdKHeGgSqmIXw9VSUtvyFZ6WZRtnxgbuz76FK7LyoN8t/eINRbPUcvXB5fw==", + "cpu": [ + "riscv64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ] + }, + "node_modules/@rollup/rollup-linux-s390x-gnu": { + "version": "4.27.4", + "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-s390x-gnu/-/rollup-linux-s390x-gnu-4.27.4.tgz", + "integrity": "sha512-PFz+y2kb6tbh7m3A7nA9++eInGcDVZUACulf/KzDtovvdTizHpZaJty7Gp0lFwSQcrnebHOqxF1MaKZd7psVRg==", + "cpu": [ + "s390x" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ] + }, + "node_modules/@rollup/rollup-linux-x64-gnu": { + "version": "4.27.4", + "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-x64-gnu/-/rollup-linux-x64-gnu-4.27.4.tgz", + "integrity": "sha512-Ni8mMtfo+o/G7DVtweXXV/Ol2TFf63KYjTtoZ5f078AUgJTmaIJnj4JFU7TK/9SVWTaSJGxPi5zMDgK4w+Ez7Q==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ] + }, + "node_modules/@rollup/rollup-linux-x64-musl": { + "version": "4.27.4", + "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-x64-musl/-/rollup-linux-x64-musl-4.27.4.tgz", + "integrity": "sha512-5AeeAF1PB9TUzD+3cROzFTnAJAcVUGLuR8ng0E0WXGkYhp6RD6L+6szYVX+64Rs0r72019KHZS1ka1q+zU/wUw==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ] + }, + "node_modules/@rollup/rollup-win32-arm64-msvc": { + "version": "4.27.4", + "resolved": "https://registry.npmjs.org/@rollup/rollup-win32-arm64-msvc/-/rollup-win32-arm64-msvc-4.27.4.tgz", + "integrity": "sha512-yOpVsA4K5qVwu2CaS3hHxluWIK5HQTjNV4tWjQXluMiiiu4pJj4BN98CvxohNCpcjMeTXk/ZMJBRbgRg8HBB6A==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "win32" + ] + }, + "node_modules/@rollup/rollup-win32-ia32-msvc": { + "version": "4.27.4", + "resolved": "https://registry.npmjs.org/@rollup/rollup-win32-ia32-msvc/-/rollup-win32-ia32-msvc-4.27.4.tgz", + "integrity": "sha512-KtwEJOaHAVJlxV92rNYiG9JQwQAdhBlrjNRp7P9L8Cb4Rer3in+0A+IPhJC9y68WAi9H0sX4AiG2NTsVlmqJeQ==", + "cpu": [ + "ia32" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "win32" + ] + }, + "node_modules/@rollup/rollup-win32-x64-msvc": { + "version": "4.27.4", + "resolved": "https://registry.npmjs.org/@rollup/rollup-win32-x64-msvc/-/rollup-win32-x64-msvc-4.27.4.tgz", + "integrity": "sha512-3j4jx1TppORdTAoBJRd+/wJRGCPC0ETWkXOecJ6PPZLj6SptXkrXcNqdj0oclbKML6FkQltdz7bBA3rUSirZug==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "win32" + ] + }, + "node_modules/@types/estree": { + "version": "1.0.6", + "resolved": "https://registry.npmjs.org/@types/estree/-/estree-1.0.6.tgz", + "integrity": "sha512-AYnb1nQyY49te+VRAVgmzfcgjYS91mY5P0TKUDCLEM+gNnA+3T6rWITXRLYCpahpqSQbN5cE+gHpnPyXjHWxcw==", + "dev": true, + "license": "MIT" + }, + "node_modules/acorn": { + "version": "8.14.0", + "resolved": "https://registry.npmjs.org/acorn/-/acorn-8.14.0.tgz", + "integrity": "sha512-cl669nCJTZBsL97OF4kUQm5g5hC2uihk0NxY3WENAC0TYdILVkAyHymAntgxGkl7K+t0cXIrH5siy5S4XkFycA==", + "dev": true, + "license": "MIT", + "bin": { + "acorn": "bin/acorn" + }, + "engines": { + "node": ">=0.4.0" + } + }, + "node_modules/buffer-from": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/buffer-from/-/buffer-from-1.1.2.tgz", + "integrity": "sha512-E+XQCRwSbaaiChtv6k6Dwgc+bx+Bs6vuKJHHl5kox/BaKbhiXzqQOwK4cO22yElGp2OCmjwVhT3HmxgyPGnJfQ==", + "dev": true, + "license": "MIT" + }, + "node_modules/commander": { + "version": "2.20.3", + "resolved": "https://registry.npmjs.org/commander/-/commander-2.20.3.tgz", + "integrity": "sha512-GpVkmM8vF2vQUkj2LvZmD35JxeJOLCwJ9cUkugyk2nuhbv3+mJvpLYYt+0+USMxE+oj+ey/lJEnhZw75x/OMcQ==", + "dev": true, + "license": "MIT" + }, + "node_modules/fsevents": { + "version": "2.3.3", + "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.3.tgz", + "integrity": "sha512-5xoDfX+fL7faATnagmWPpbFtwh/R77WmMMqqHGS65C3vvB0YHrgF+B1YmZ3441tMj5n63k0212XNoJwzlhffQw==", + "dev": true, + "hasInstallScript": true, + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": "^8.16.0 || ^10.6.0 || >=11.0.0" + } + }, + "node_modules/randombytes": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/randombytes/-/randombytes-2.1.0.tgz", + "integrity": "sha512-vYl3iOX+4CKUWuxGi9Ukhie6fsqXqS9FE2Zaic4tNFD2N2QQaXOMFbuKK4QmDHC0JO6B1Zp41J0LpT0oR68amQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "safe-buffer": "^5.1.0" + } + }, + "node_modules/rollup": { + "version": "4.27.4", + "resolved": "https://registry.npmjs.org/rollup/-/rollup-4.27.4.tgz", + "integrity": "sha512-RLKxqHEMjh/RGLsDxAEsaLO3mWgyoU6x9w6n1ikAzet4B3gI2/3yP6PWY2p9QzRTh6MfEIXB3MwsOY0Iv3vNrw==", + "dev": true, + "license": "MIT", + "dependencies": { + "@types/estree": "1.0.6" + }, + "bin": { + "rollup": "dist/bin/rollup" + }, + "engines": { + "node": ">=18.0.0", + "npm": ">=8.0.0" + }, + "optionalDependencies": { + "@rollup/rollup-android-arm-eabi": "4.27.4", + "@rollup/rollup-android-arm64": "4.27.4", + "@rollup/rollup-darwin-arm64": "4.27.4", + "@rollup/rollup-darwin-x64": "4.27.4", + "@rollup/rollup-freebsd-arm64": "4.27.4", + "@rollup/rollup-freebsd-x64": "4.27.4", + "@rollup/rollup-linux-arm-gnueabihf": "4.27.4", + "@rollup/rollup-linux-arm-musleabihf": "4.27.4", + "@rollup/rollup-linux-arm64-gnu": "4.27.4", + "@rollup/rollup-linux-arm64-musl": "4.27.4", + "@rollup/rollup-linux-powerpc64le-gnu": "4.27.4", + "@rollup/rollup-linux-riscv64-gnu": "4.27.4", + "@rollup/rollup-linux-s390x-gnu": "4.27.4", + "@rollup/rollup-linux-x64-gnu": "4.27.4", + "@rollup/rollup-linux-x64-musl": "4.27.4", + "@rollup/rollup-win32-arm64-msvc": "4.27.4", + "@rollup/rollup-win32-ia32-msvc": "4.27.4", + "@rollup/rollup-win32-x64-msvc": "4.27.4", + "fsevents": "~2.3.2" + } + }, + "node_modules/safe-buffer": { + "version": "5.2.1", + "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.1.tgz", + "integrity": "sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==", + "dev": true, + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/feross" + }, + { + "type": "patreon", + "url": "https://www.patreon.com/feross" + }, + { + "type": "consulting", + "url": "https://feross.org/support" + } + ], + "license": "MIT" + }, + "node_modules/serialize-javascript": { + "version": "6.0.2", + "resolved": "https://registry.npmjs.org/serialize-javascript/-/serialize-javascript-6.0.2.tgz", + "integrity": "sha512-Saa1xPByTTq2gdeFZYLLo+RFE35NHZkAbqZeWNd3BpzppeVisAqpDjcp8dyf6uIvEqJRd46jemmyA4iFIeVk8g==", + "dev": true, + "license": "BSD-3-Clause", + "dependencies": { + "randombytes": "^2.1.0" + } + }, + "node_modules/smob": { + "version": "1.5.0", + "resolved": "https://registry.npmjs.org/smob/-/smob-1.5.0.tgz", + "integrity": "sha512-g6T+p7QO8npa+/hNx9ohv1E5pVCmWrVCUzUXJyLdMmftX6ER0oiWY/w9knEonLpnOp6b6FenKnMfR8gqwWdwig==", + "dev": true, + "license": "MIT" + }, + "node_modules/source-map": { + "version": "0.6.1", + "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz", + "integrity": "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==", + "dev": true, + "license": "BSD-3-Clause", + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/source-map-support": { + "version": "0.5.21", + "resolved": "https://registry.npmjs.org/source-map-support/-/source-map-support-0.5.21.tgz", + "integrity": "sha512-uBHU3L3czsIyYXKX88fdrGovxdSCoTGDRZ6SYXtSRxLZUzHg5P/66Ht6uoUlHu9EZod+inXhKo3qQgwXUT/y1w==", + "dev": true, + "license": "MIT", + "dependencies": { + "buffer-from": "^1.0.0", + "source-map": "^0.6.0" + } + }, + "node_modules/terser": { + "version": "5.36.0", + "resolved": "https://registry.npmjs.org/terser/-/terser-5.36.0.tgz", + "integrity": "sha512-IYV9eNMuFAV4THUspIRXkLakHnV6XO7FEdtKjf/mDyrnqUg9LnlOn6/RwRvM9SZjR4GUq8Nk8zj67FzVARr74w==", + "dev": true, + "license": "BSD-2-Clause", + "dependencies": { + "@jridgewell/source-map": "^0.3.3", + "acorn": "^8.8.2", + "commander": "^2.20.0", + "source-map-support": "~0.5.20" + }, + "bin": { + "terser": "bin/terser" + }, + "engines": { + "node": ">=10" + } + } + } +} diff --git a/package.json b/package.json index 6d31769..a772ed8 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,4 @@ { - "name" : "fuzzysort", "version" : "3.1.0", "author" : "farzher", @@ -10,24 +9,33 @@ "repository": { "type" : "git", - "url" : "https://github.com/farzher/fuzzysort.git" + "url" : "git+https://github.com/farzher/fuzzysort.git" }, + "type": "commonjs", "main": "fuzzysort.js", + "module": "fuzzysort.mjs", + "browser": "fuzzysort.min.js", "scripts": { - "test" : "node test/test.js", - "test-min" : "node test/test.js min", - - "minify" : "uglifyjs fuzzysort.js -o fuzzysort.min.js -m -c --mangle-props keep_quoted --comments /farzher/" + "test": "node test/test.js", + "test-min": "node test/test.js min", + "build": "rollup -c", + "dev": "rollup -c -w", + "pretest": "npm run build" }, "files": [ "fuzzysort.js", + "fuzzysort.mjs", "fuzzysort.min.js", - "index.d.ts" + "index.d.ts", + "*.map" ], - "types": "index.d.ts" - + "types": "index.d.ts", + "devDependencies": { + "rollup": "^4.27.3", + "@rollup/plugin-terser": "^0.4.4" + } } diff --git a/rollup.config.mjs b/rollup.config.mjs new file mode 100644 index 0000000..0b4c477 --- /dev/null +++ b/rollup.config.mjs @@ -0,0 +1,24 @@ +import terser from '@rollup/plugin-terser'; +import fs from 'fs/promises'; +import { defineConfig } from 'rollup'; + +const pkg = JSON.parse(await fs.readFile('package.json', 'utf8')); + +const banner = '// https://github.com/farzher/fuzzysort v' + pkg.version; +const exports = 'named'; +const sourcemap = true; + +export default defineConfig([ + { + input: 'src/fuzzysort.js', + output: { banner, exports, sourcemap, file: pkg.browser, format: 'umd', name: 'fuzzysort' }, + plugins: [terser()], + }, + { + input: 'src/fuzzysort.js', + output: [ + { banner, exports, sourcemap, file: pkg.main, format: 'cjs' }, + { banner, exports, sourcemap, file: pkg.module, format: 'es' }, + ], + }, +]); diff --git a/src/fuzzysort.js b/src/fuzzysort.js new file mode 100644 index 0000000..87c6239 --- /dev/null +++ b/src/fuzzysort.js @@ -0,0 +1,678 @@ +export const single = (search, target) => { + if(!search || !target) return NULL + + var preparedSearch = getPreparedSearch(search) + if(!isPrepared(target)) target = getPrepared(target) + + var searchBitflags = preparedSearch.bitflags + if((searchBitflags & target._bitflags) !== searchBitflags) return NULL + + return algorithm(preparedSearch, target) +} + +export const go = (search, targets, options) => { + if(!search) return options?.all ? all(targets, options) : noResults + + var preparedSearch = getPreparedSearch(search) + var searchBitflags = preparedSearch.bitflags + var containsSpace = preparedSearch.containsSpace + + var threshold = denormalizeScore( options?.threshold || 0 ) + var limit = options?.limit || INFINITY + + var resultsLen = 0; var limitedCount = 0 + var targetsLen = targets.length + + function push_result(result) { + if(resultsLen < limit) { q.add(result); ++resultsLen } + else { + ++limitedCount + if(result._score > q.peek()._score) q.replaceTop(result) + } + } + + // This code is copy/pasted 3 times for performance reasons [options.key, options.keys, no keys] + + // options.key + if(options?.key) { + var key = options.key + for(var i = 0; i < targetsLen; ++i) { var obj = targets[i] + var target = getValue(obj, key) + if(!target) continue + if(!isPrepared(target)) target = getPrepared(target) + + if((searchBitflags & target._bitflags) !== searchBitflags) continue + var result = algorithm(preparedSearch, target) + if(result === NULL) continue + if(result._score < threshold) continue + + result.obj = obj + push_result(result) + } + + // options.keys + } else if(options?.keys) { + var keys = options.keys + var keysLen = keys.length + + outer: for(var i = 0; i < targetsLen; ++i) { var obj = targets[i] + + { // early out based on bitflags + var keysBitflags = 0 + for (var keyI = 0; keyI < keysLen; ++keyI) { + var key = keys[keyI] + var target = getValue(obj, key) + if(!target) { tmpTargets[keyI] = noTarget; continue } + if(!isPrepared(target)) target = getPrepared(target) + tmpTargets[keyI] = target + + keysBitflags |= target._bitflags + } + + if((searchBitflags & keysBitflags) !== searchBitflags) continue + } + + if(containsSpace) for(let i=0; i -1000) { + if(keysSpacesBestScores[i] > NEGATIVE_INFINITY) { + var tmp = (keysSpacesBestScores[i] + allowPartialMatchScores[i]) / 4/*bonus score for having multiple matches*/ + if(tmp > keysSpacesBestScores[i]) keysSpacesBestScores[i] = tmp + } + } + if(allowPartialMatchScores[i] > keysSpacesBestScores[i]) keysSpacesBestScores[i] = allowPartialMatchScores[i] + } + } + + if(containsSpace) { + for(let i=0; i -1000) { + if(score > NEGATIVE_INFINITY) { + var tmp = (score + result._score) / 4/*bonus score for having multiple matches*/ + if(tmp > score) score = tmp + } + } + if(result._score > score) score = result._score + } + } + + objResults.obj = obj + objResults._score = score + if(options?.scoreFn) { + score = options.scoreFn(objResults) + if(!score) continue + score = denormalizeScore(score) + objResults._score = score + } + + if(score < threshold) continue + push_result(objResults) + } + + // no keys + } else { + for(var i = 0; i < targetsLen; ++i) { var target = targets[i] + if(!target) continue + if(!isPrepared(target)) target = getPrepared(target) + + if((searchBitflags & target._bitflags) !== searchBitflags) continue + var result = algorithm(preparedSearch, target) + if(result === NULL) continue + if(result._score < threshold) continue + + push_result(result) + } + } + + if(resultsLen === 0) return noResults + var results = new Array(resultsLen) + for(var i = resultsLen - 1; i >= 0; --i) results[i] = q.poll() + results.total = resultsLen + limitedCount + return results +} + + +// this is written as 1 function instead of 2 for minification. perf seems fine ... +// except when minified. the perf is very slow +const highlight = (result, open='', close='') => { + var callback = typeof open === 'function' ? open : undefined + + var target = result.target + var targetLen = target.length + var indexes = result.indexes + var highlighted = '' + var matchI = 0 + var indexesI = 0 + var opened = false + var parts = [] + + for(var i = 0; i < targetLen; ++i) { var char = target[i] + if(indexes[indexesI] === i) { + ++indexesI + if(!opened) { opened = true + if(callback) { + parts.push(highlighted); highlighted = '' + } else { + highlighted += open + } + } + + if(indexesI === indexes.length) { + if(callback) { + highlighted += char + parts.push(callback(highlighted, matchI++)); highlighted = '' + parts.push(target.substr(i+1)) + } else { + highlighted += char + close + target.substr(i+1) + } + break + } + } else { + if(opened) { opened = false + if(callback) { + parts.push(callback(highlighted, matchI++)); highlighted = '' + } else { + highlighted += close + } + } + } + highlighted += char + } + + return callback ? parts : highlighted +} + + +export const prepare = (target) => { + if(typeof target === 'number') target = ''+target + else if(typeof target !== 'string') target = '' + var info = prepareLowerInfo(target) + return new_result(target, {_targetLower:info._lower, _targetLowerCodes:info.lowerCodes, _bitflags:info.bitflags}) +} + +export const cleanup = () => { preparedCache.clear(); preparedSearchCache.clear() } + + +// Below this point is only internal code +// Below this point is only internal code +// Below this point is only internal code +// Below this point is only internal code + + +class Result { + get ['indexes']() { return this._indexes.slice(0, this._indexes.len).sort((a,b)=>a-b) } + set ['indexes'](indexes) { return this._indexes = indexes } + ['highlight'](open, close) { return highlight(this, open, close) } + get ['score']() { return normalizeScore(this._score) } + set ['score'](score) { this._score = denormalizeScore(score) } +} + +class KeysResult extends Array { + get ['score']() { return normalizeScore(this._score) } + set ['score'](score) { this._score = denormalizeScore(score) } +} + +const new_result = (target, options) => { + const result = new Result() + result['target'] = target + result['obj'] = options.obj ?? NULL + result._score = options._score ?? NEGATIVE_INFINITY + result._indexes = options._indexes ?? [] + result._targetLower = options._targetLower ?? '' + result._targetLowerCodes = options._targetLowerCodes ?? NULL + result._nextBeginningIndexes = options._nextBeginningIndexes ?? NULL + result._bitflags = options._bitflags ?? 0 + return result +} + + +const normalizeScore = score => { + if(score === NEGATIVE_INFINITY) return 0 + if(score > 1) return score + return Math.E ** ( ((-score + 1)**.04307 - 1) * -2) +} +const denormalizeScore = normalizedScore => { + if(normalizedScore === 0) return NEGATIVE_INFINITY + if(normalizedScore > 1) return normalizedScore + return 1 - Math.pow((Math.log(normalizedScore) / -2 + 1), 1 / 0.04307) +} + + +const prepareSearch = (search) => { + if(typeof search === 'number') search = ''+search + else if(typeof search !== 'string') search = '' + search = search.trim() + var info = prepareLowerInfo(search) + + var spaceSearches = [] + if(info.containsSpace) { + var searches = search.split(/\s+/) + searches = [...new Set(searches)] // distinct + for(var i=0; i { + if(target.length > 999) return prepare(target) // don't cache huge targets + var targetPrepared = preparedCache.get(target) + if(targetPrepared !== undefined) return targetPrepared + targetPrepared = prepare(target) + preparedCache.set(target, targetPrepared) + return targetPrepared +} +const getPreparedSearch = (search) => { + if(search.length > 999) return prepareSearch(search) // don't cache huge searches + var searchPrepared = preparedSearchCache.get(search) + if(searchPrepared !== undefined) return searchPrepared + searchPrepared = prepareSearch(search) + preparedSearchCache.set(search, searchPrepared) + return searchPrepared +} + + +const all = (targets, options) => { + var results = []; results.total = targets.length // this total can be wrong if some targets are skipped + + var limit = options?.limit || INFINITY + + if(options?.key) { + for(var i=0;i= limit) return results + } + } else if(options?.keys) { + for(var i=0;i= 0; --keyI) { + var target = getValue(obj, options.keys[keyI]) + if(!target) { objResults[keyI] = noTarget; continue } + if(!isPrepared(target)) target = getPrepared(target) + target._score = NEGATIVE_INFINITY + target._indexes.len = 0 + objResults[keyI] = target + } + objResults.obj = obj + objResults._score = NEGATIVE_INFINITY + results.push(objResults); if(results.length >= limit) return results + } + } else { + for(var i=0;i= limit) return results + } + } + + return results +} + + +const algorithm = (preparedSearch, prepared, allowSpaces=false, allowPartialMatch=false) => { + if(allowSpaces===false && preparedSearch.containsSpace) return algorithmSpaces(preparedSearch, prepared, allowPartialMatch) + + var searchLower = preparedSearch._lower + var searchLowerCodes = preparedSearch.lowerCodes + var searchLowerCode = searchLowerCodes[0] + var targetLowerCodes = prepared._targetLowerCodes + var searchLen = searchLowerCodes.length + var targetLen = targetLowerCodes.length + var searchI = 0 // where we at + var targetI = 0 // where you at + var matchesSimpleLen = 0 + + // very basic fuzzy match; to remove non-matching targets ASAP! + // walk through target. find sequential matches. + // if all chars aren't found then exit + for(;;) { + var isMatch = searchLowerCode === targetLowerCodes[targetI] + if(isMatch) { + matchesSimple[matchesSimpleLen++] = targetI + ++searchI; if(searchI === searchLen) break + searchLowerCode = searchLowerCodes[searchI] + } + ++targetI; if(targetI >= targetLen) return NULL // Failed to find searchI + } + + var searchI = 0 + var successStrict = false + var matchesStrictLen = 0 + + var nextBeginningIndexes = prepared._nextBeginningIndexes + if(nextBeginningIndexes === NULL) nextBeginningIndexes = prepared._nextBeginningIndexes = prepareNextBeginningIndexes(prepared.target) + targetI = matchesSimple[0]===0 ? 0 : nextBeginningIndexes[matchesSimple[0]-1] + + // Our target string successfully matched all characters in sequence! + // Let's try a more advanced and strict test to improve the score + // only count it as a match if it's consecutive or a beginning character! + var backtrackCount = 0 + if(targetI !== targetLen) for(;;) { + if(targetI >= targetLen) { + // We failed to find a good spot for this search char, go back to the previous search char and force it forward + if(searchI <= 0) break // We failed to push chars forward for a better match + + ++backtrackCount; if(backtrackCount > 200) break // exponential backtracking is taking too long, just give up and return a bad match + + --searchI + var lastMatch = matchesStrict[--matchesStrictLen] + targetI = nextBeginningIndexes[lastMatch] + + } else { + var isMatch = searchLowerCodes[searchI] === targetLowerCodes[targetI] + if(isMatch) { + matchesStrict[matchesStrictLen++] = targetI + ++searchI; if(searchI === searchLen) { successStrict = true; break } + ++targetI + } else { + targetI = nextBeginningIndexes[targetI] + } + } + } + + // check if it's a substring match + var substringIndex = searchLen <= 1 ? -1 : prepared._targetLower.indexOf(searchLower, matchesSimple[0]) // perf: this is slow + var isSubstring = !!~substringIndex + var isSubstringBeginning = !isSubstring ? false : substringIndex===0 || prepared._nextBeginningIndexes[substringIndex-1] === substringIndex + + // if it's a substring match but not at a beginning index, let's try to find a substring starting at a beginning index for a better score + if(isSubstring && !isSubstringBeginning) { + for(var i=0; i { + var score = 0 + + var extraMatchGroupCount = 0 + for(var i = 1; i < searchLen; ++i) { + if(matches[i] - matches[i-1] !== 1) {score -= matches[i]; ++extraMatchGroupCount} + } + var unmatchedDistance = matches[searchLen-1] - matches[0] - (searchLen-1) + + score -= (12+unmatchedDistance) * extraMatchGroupCount // penality for more groups + + if(matches[0] !== 0) score -= matches[0]*matches[0]*.2 // penality for not starting near the beginning + + if(!successStrict) { + score *= 1000 + } else { + // successStrict on a target with too many beginning indexes loses points for being a bad target + var uniqueBeginningIndexes = 1 + for(var i = nextBeginningIndexes[0]; i < targetLen; i=nextBeginningIndexes[i]) ++uniqueBeginningIndexes + + if(uniqueBeginningIndexes > 24) score *= (uniqueBeginningIndexes-24)*10 // quite arbitrary numbers here ... + } + + score -= (targetLen - searchLen)/2 // penality for longer targets + + if(isSubstring) score /= 1+searchLen*searchLen*1 // bonus for being a full substring + if(isSubstringBeginning) score /= 1+searchLen*searchLen*1 // bonus for substring starting on a beginningIndex + + score -= (targetLen - searchLen)/2 // penality for longer targets + + return score + } + + if(!successStrict) { + if(isSubstring) for(var i=0; i { + var seen_indexes = new Set() + var score = 0 + var result = NULL + + var first_seen_index_last_search = 0 + var searches = preparedSearch.spaceSearches + var searchesLen = searches.length + var changeslen = 0 + + // Return _nextBeginningIndexes back to its normal state + var resetNextBeginningIndexes = () => { + for(let i=changeslen-1; i>=0; i--) target._nextBeginningIndexes[nextBeginningIndexesChanges[i*2 + 0]] = nextBeginningIndexesChanges[i*2 + 1] + } + + var hasAtLeast1Match = false + for(var i=0; i=0; i--) { + if(toReplace !== target._nextBeginningIndexes[i]) break + target._nextBeginningIndexes[i] = newBeginningIndex + nextBeginningIndexesChanges[changeslen*2 + 0] = i + nextBeginningIndexesChanges[changeslen*2 + 1] = toReplace + changeslen++ + } + } + } + + score += result._score / searchesLen + allowPartialMatchScores[i] = result._score / searchesLen + + // dock points based on order otherwise "c man" returns Manifest.cpp instead of CheatManager.h + if(result._indexes[0] < first_seen_index_last_search) { + score -= (first_seen_index_last_search - result._indexes[0]) * 2 + } + first_seen_index_last_search = result._indexes[0] + + for(var j=0; j score) { + if(allowPartialMatch) { + for(var i=0; i str.replace(/\p{Script=Latin}+/gu, match => match.normalize('NFD')).replace(/[\u0300-\u036f]/g, '') + +const prepareLowerInfo = (str) => { + str = remove_accents(str) + var strLen = str.length + var lower = str.toLowerCase() + var lowerCodes = [] // new Array(strLen) sparse array is too slow + var bitflags = 0 + var containsSpace = false // space isn't stored in bitflags because of how searching with a space works + + for(var i = 0; i < strLen; ++i) { + var lowerCode = lowerCodes[i] = lower.charCodeAt(i) + + if(lowerCode === 32) { + containsSpace = true + continue // it's important that we don't set any bitflags for space + } + + var bit = lowerCode>=97&&lowerCode<=122 ? lowerCode-97 // alphabet + : lowerCode>=48&&lowerCode<=57 ? 26 // numbers + // 3 bits available + : lowerCode<=127 ? 30 // other ascii + : 31 // other utf8 + bitflags |= 1< { + var targetLen = target.length + var beginningIndexes = []; var beginningIndexesLen = 0 + var wasUpper = false + var wasAlphanum = false + for(var i = 0; i < targetLen; ++i) { + var targetCode = target.charCodeAt(i) + var isUpper = targetCode>=65&&targetCode<=90 + var isAlphanum = isUpper || targetCode>=97&&targetCode<=122 || targetCode>=48&&targetCode<=57 + var isBeginning = isUpper && !wasUpper || !wasAlphanum || !isAlphanum + wasUpper = isUpper + wasAlphanum = isAlphanum + if(isBeginning) beginningIndexes[beginningIndexesLen++] = i + } + return beginningIndexes +} +const prepareNextBeginningIndexes = (target) => { + target = remove_accents(target) + var targetLen = target.length + var beginningIndexes = prepareBeginningIndexes(target) + var nextBeginningIndexes = [] // new Array(targetLen) sparse array is too slow + var lastIsBeginning = beginningIndexes[0] + var lastIsBeginningI = 0 + for(var i = 0; i < targetLen; ++i) { + if(lastIsBeginning > i) { + nextBeginningIndexes[i] = lastIsBeginning + } else { + lastIsBeginning = beginningIndexes[++lastIsBeginningI] + nextBeginningIndexes[i] = lastIsBeginning===undefined ? targetLen : lastIsBeginning + } + } + return nextBeginningIndexes +} + +const preparedCache = new Map() +const preparedSearchCache = new Map() + +// the theory behind these being globals is to reduce garbage collection by not making new arrays +var matchesSimple = []; var matchesStrict = [] +var nextBeginningIndexesChanges = [] // allows straw berry to match strawberry well, by modifying the end of a substring to be considered a beginning index for the rest of the search +var keysSpacesBestScores = []; var allowPartialMatchScores = [] +var tmpTargets = []; var tmpResults = [] + +// prop = 'key' 2.5ms optimized for this case, seems to be about as fast as direct obj[prop] +// prop = 'key1.key2' 10ms +// prop = ['key1', 'key2'] 27ms +// prop = obj => obj.tags.join() ??ms +const getValue = (obj, prop) => { + var tmp = obj[prop]; if(tmp !== undefined) return tmp + if(typeof prop === 'function') return prop(obj) // this should run first. but that makes string props slower + var segs = prop + if(!Array.isArray(prop)) segs = prop.split('.') + var len = segs.length + var i = -1 + while (obj && (++i < len)) obj = obj[segs[i]] + return obj +} + +const isPrepared = (x) => { return typeof x === 'object' && typeof x._bitflags === 'number' } +const INFINITY = Infinity; const NEGATIVE_INFINITY = -INFINITY +const noResults = []; noResults.total = 0 +const NULL = null + +const noTarget = prepare('') + +// Hacked version of https://github.com/lemire/FastPriorityQueue.js +const fastpriorityqueue=r=>{var e=[],o=0,a={},v=r=>{for(var a=0,v=e[a],c=1;c>1]=e[a],c=1+(a<<1)}for(var f=a-1>>1;a>0&&v._score>1)e[a]=e[f];e[a]=v};return a.add=(r=>{var a=o;e[o++]=r;for(var v=a-1>>1;a>0&&r._score>1)e[a]=e[v];e[a]=r}),a.poll=(r=>{if(0!==o){var a=e[0];return e[0]=e[--o],v(),a}}),a.peek=(r=>{if(0!==o)return e[0]}),a.replaceTop=(r=>{e[0]=r,v()}),a} +const q = fastpriorityqueue() // reuse this + +export default {single, go, prepare, cleanup} diff --git a/test/test.html b/test/test.html index e1c46ce..a88f508 100644 --- a/test/test.html +++ b/test/test.html @@ -9,7 +9,7 @@ - +