string-similarity icon indicating copy to clipboard operation
string-similarity copied to clipboard

allow findBestMatch to accept an array of objects

Open chrissyast opened this issue 3 years ago • 0 comments

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

chrissyast avatar May 18 '22 16:05 chrissyast