Errors and edge cases
Return values
Section titled “Return values”hamming()andnormalizedHamming()returnnullwhen the strings have different lengths.closest()returnsnullwhen no item matches the query, when the best match scores belowminScore, or when the list is empty.remove(index)on an index returnsfalsefor an index out of range (negative, or not less thansize).- An index after
destroy()does not throw. It behaves as an empty index and accepts new items.
What throws
Section titled “What throws”Invalid arguments throw before any work is done. In Node.js most of them are an Error with the code InvalidArg. In the WebAssembly build they are an Error, a TypeError or a RangeError. Check for instanceof Error rather than a specific class.
maxResults, and themaxDistancethreshold oflevenshteinMany,damerauLevenshteinMany,indelMany,hammingManyand their*ManyU32variants, must be a non-negative integer orInfinity(no limit).NaN, negative and fractional values throw.- A
NaNminScoreorminSimilaritythrows. Any other number is accepted. remove(index)throws aTypeErrorfor an index that is not a number, and aRangeErrorfor one that is not an integer (NaN,±Infinityor a fraction).*Batchfunctions throw when a pair is not exactly two strings.- In the WebAssembly build, a value that is not a string where a string is expected, an array that is not an array of strings, and a threshold that is not a number (such as
'2'ortrue) throw aTypeError. searchObjects()andFuzzyObjectIndexthrow aTypeErrorwhenoptions.keysis missing or empty.- Object and keyed search throw when a weight is negative,
NaNor infinite, or when the weights sum to 0 or overflow. The lower-levelsearchKeys()andKeyedFuzzyIndexalso throw when the key text arrays have different lengths, or when the number of weights differs from the number of keys. - An unknown
scoreModeormatchModethrows. FuzzyIndex.deserialize(),KeyedFuzzyIndex.deserialize()andFuzzyObjectIndex.deserialize()throw on corrupt data and on data from another format version.addMany()onKeyedFuzzyIndexandFuzzyObjectIndexis atomic: when one item is invalid, nothing is added, and the error names the item (item 3).