aceakash / string-similarity

Finds degree of similarity between two strings, based on Dice's Coefficient, which is mostly better than Levenshtein distance.
MIT License
2.53k stars 128 forks source link

allow findBestMatch to accept an array of objects #124

Open chrissyast opened 2 years ago

chrissyast commented 2 years ago

Use case:

Instead of wanting to compare ["foo","bar","baz"], it can be useful to pass in an array of objects for which you want to compare one property, i.e.

[
    { name: "foo", otherProperty: 23 },
    { name: "bar", otherProperty: 27 },
    { name: "baz", otherProperty: 99 }
]

and instruct the function to compare based on the name property, but return the whole object in the response.

Summary of changes:

findBestMatch now accepts a key argument. If this argument is not supplied, current behaviour will be retained (i.e. the array member will be used for comparison and must be a string). If a key is supplied, all members of that array must be objects which contain key as a property. object[key] is used for comparison, but object is returned in the results.

Additional tests added for this functionality