Skip to content

Commit

Permalink
fix(*): handle no match condition
Browse files Browse the repository at this point in the history
  • Loading branch information
sanjayaksaxena committed Sep 27, 2017
1 parent e2251dd commit 2856d2a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
4 changes: 3 additions & 1 deletion README.md
Expand Up @@ -54,8 +54,10 @@ Original Reference: UNIMATCH:
**Examples**

```javascript
// returns { distance: 0.08333333333333337, similarity: 0.9166666666666666 }
jaro( 'daniel', 'danielle' );
// -> { distance: 0.08333333333333337, similarity: 0.9166666666666666 }
jaro( 'god', 'father' );
// -> { distance: 1, similarity: 0 }
```

Returns **[object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** containing `distance` and `similarity` values between 0 and 1.
Expand Down
10 changes: 7 additions & 3 deletions src/wink-jaro-distance.js
Expand Up @@ -38,8 +38,10 @@
* @return {object} - containing `distance` and `similarity` values between 0 and 1.
*
* @example
* // returns { distance: 0.08333333333333337, similarity: 0.9166666666666666 }
* jaro( 'daniel', 'danielle' );
* // -> { distance: 0.08333333333333337, similarity: 0.9166666666666666 }
* jaro( 'god', 'father' );
* // -> { distance: 1, similarity: 0 }
*/
var jaro = function ( s1, s2 ) {
// On the basis of the length of `s1` and `s2`, the shorter length string will
Expand Down Expand Up @@ -104,8 +106,10 @@ var jaro = function ( s1, s2 ) {
}
}
transposes /= 2;
// Compute similarity.
smlrty = ( ( matches / shortLen ) + ( matches / longLen ) + ( ( matches - transposes ) / matches ) ) / 3;
// Compute similarity; if no `matches` means similarity is 0!
smlrty = ( matches === 0 ) ?
0 :
( ( matches / shortLen ) + ( matches / longLen ) + ( ( matches - transposes ) / matches ) ) / 3;
return {
distance: 1 - smlrty,
similarity: smlrty
Expand Down
6 changes: 6 additions & 0 deletions test/wink-jaro-distance-specs.js
Expand Up @@ -45,6 +45,12 @@ describe( 'jaro()', function () {
{ whenInputIs: [ 'abcd', 'dcba' ], expectedOutputIs: { similarity: 0.5, distance: 0.5 } },
{ whenInputIs: [ 'washington', 'notgnihsaw' ], expectedOutputIs: { similarity: 0.43333333333333335, distance: 1 - 0.43333333333333335 } },
{ whenInputIs: [ 'washington', 'washingtonx' ], expectedOutputIs: { similarity: 0.9696969696969697, distance: 1 - 0.9696969696969697 } },
// No match case
{ whenInputIs: [ 'sat', 'urn' ], expectedOutputIs: { similarity: 0, distance: 1 } },
// Empty string case
{ whenInputIs: [ '', '' ], expectedOutputIs: { similarity: 1, distance: 0 } },
// One of them is Empty
{ whenInputIs: [ '', 'urn' ], expectedOutputIs: { similarity: 0, distance: 1 } },
];

tests.forEach( function ( t ) {
Expand Down

0 comments on commit 2856d2a

Please sign in to comment.