Skip to content

Commit

Permalink
Fix results for digit-only and white-space-only input
Browse files Browse the repository at this point in the history
As the algorithm ignores characters which occur in all or
most languages, `getTopScript` returned `cmn` for these
values (for example, `123 456 789`), as it is the first
script to test and other scripts to reach higher scores.

This bug-fix changes that by checking for a score of `0`, and
returning the `und` string/tuple.

Closes GH-18.
  • Loading branch information
wooorm committed Mar 25, 2015
1 parent fac55ab commit e0624ae
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 4 deletions.
2 changes: 1 addition & 1 deletion dist/franc-all.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/franc-most.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/franc.js

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion lib/franc.js
Original file line number Diff line number Diff line change
Expand Up @@ -294,10 +294,15 @@ function detectAll(value, options) {

/*
* One languages exists for the most-used script.
*
* If no matches occured, such as a digit only string,
* exit with `und`.
*/

if (!(script[0] in data)) {
return singleLanguageTuples(script[0]);
return singleLanguageTuples(
script[1] === 0 ? 'und' : script[0]
);
}

/*
Expand Down
12 changes: 12 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ describe('franc()', function () {

assert(result === 'und');
});

it('should return `und` for generic characters', function () {
assert(franc('987 654 321') === 'und');
});
});

describe('franc.all()', function () {
Expand Down Expand Up @@ -143,6 +147,14 @@ describe('franc.all()', function () {
assert(result.length === 1);
});

it('should return `[["und", 1]]` for generic characters', function () {
var result = franc.all('987 654 321');
assert(result[0][0] === 'und');
assert(result[0][1] === 1);
assert(result[0].length === 2);
assert(result.length === 1);
});

it('should work on weird values', function () {
var result = franc.all('the the the the the ');

Expand Down

0 comments on commit e0624ae

Please sign in to comment.