leeoniya/uFuzzy

should `intraMode: 1` allow errors in the last char?

leeoniya opened this issue · 2 comments

currently it doesn't, so bleu will not match blue

let chars = p.slice(lftIdx, rgtIdx);

To me it sounds reasonable, probably what you expect when allowing 1 error that there is not distinction if it is in between or at the end of the string.

should work now. i also tightened some rules for terms < 5 chars. e.g. you really don't want to match every term that contains ble when searching blue due to allowing single deletion of "u".

if you actually want to do this, there is now intraSlice and intraRules options that allows for tweaking per-term in the needle exactly what is required. the defaults are currently set to intraSlice: [1, Infinty] (allows errors in all but first char) and intraRules which limits what types of errors are tolerated in short terms:

uFuzzy/src/uFuzzy.js

Lines 113 to 153 in 4b1249a

if (intraRules == null) {
intraRules = p => {
// default is exact term matches only
let _intraSlice = OPTS.intraSlice, // requires first char
_intraIns = 0,
_intraSub = 0,
_intraTrn = 0,
_intraDel = 0;
let plen = p.length;
// prevent junk matches by requiring stricter rules for short terms
if (plen <= 4) {
if (plen >= 3) {
// one swap in non-first char when 3-4 chars
_intraTrn = 1;
// or one insertion when 4 chars
if (plen == 4)
_intraIns = 1;
}
// else exact match when 1-2 chars
}
// use supplied opts
else {
_intraSlice = intraSlice;
_intraIns = intraIns,
_intraSub = intraSub,
_intraTrn = intraTrn,
_intraDel = intraDel;
}
return {
intraSlice: _intraSlice,
intraIns: _intraIns,
intraSub: _intraSub,
intraTrn: _intraTrn,
intraDel: _intraDel,
};
};
}