Skip to content

Commit

Permalink
Fix failure when script not in white-list
Browse files Browse the repository at this point in the history
Closes GH-22.
  • Loading branch information
wooorm committed Jul 13, 2015
1 parent 31136bf commit bddf79b
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 21 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.

43 changes: 25 additions & 18 deletions lib/franc.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,28 @@ function getDistance(trigrams, model) {
return distance;
}

/**
* Create a single tuple as a list of tuples from a given
* language code.
*
* @param {string} language - A single language.
* @return {Array.<Array.<string, number>>} An array
* containing a single language--distance.
*/
function singleLanguageTuples(language) {
return [[language, 1]];
}

/**
* Create a single `und` tuple.
*
* @return {Array.<Array.<string, number>>} An array
* containing a single language--distance.
*/
function und() {
return singleLanguageTuples('und');
}

/**
* Get the distance between an array of trigram--count
* tuples, and multiple trigram dictionaries.
Expand All @@ -184,7 +206,7 @@ function getDistances(trigrams, languages, options) {
]);
}

return distances.sort(sort);
return distances.length ? distances.sort(sort) : und();
}

/**
Expand Down Expand Up @@ -249,19 +271,6 @@ function normalize(value, distances) {
return distances;
}

/**
* Create a single tuple as a list of tuples from a given
* language code.
*
* @param {string} language - A single
* language--distance tuple.
* @return {Array.<Array.<string, number>>} An array
* containing a single language--distance.
*/
function singleLanguageTuples(language) {
return [[language, 1]];
}

/**
* Get a list of probable languages the given value is
* written in.
Expand All @@ -280,7 +289,7 @@ function detectAll(value, options) {
}

if (!value || value.length < minLength) {
return singleLanguageTuples('und');
return und();
}

value = value.substr(0, MAX_LENGTH);
Expand All @@ -300,9 +309,7 @@ function detectAll(value, options) {
*/

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

/*
Expand Down
18 changes: 18 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var fixtures = require('./fixtures');

var MAGIC_NUMBER = 42;
var MAGIC_LANGUAGE = 'pol';
var SOME_HEBREW = 'הפיתוח הראשוני בשנות ה־80 התמקד בגנו ובמערכת הגרפית';

/*
* The fixture belonging to magic number should not equal
Expand Down Expand Up @@ -95,6 +96,14 @@ describe('franc()', function () {
assert(result === MAGIC_LANGUAGE);
});

it('should accept `whitelist` for different scripts', function () {
var result = franc(SOME_HEBREW, {
'whitelist': ['eng']
});

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

it('should accept `minLength`', function () {
var result = franc('the', {
'minLength': 3
Expand Down Expand Up @@ -184,6 +193,15 @@ describe('franc.all()', function () {
assert(result[0][0] === MAGIC_LANGUAGE);
});

it('should accept `whitelist` for different scripts', function () {
var result = franc.all(SOME_HEBREW, {
'whitelist': ['eng']
});

assert(result.length === 1);
assert(result[0][0] === 'und');
});

it('should accept `minLength`', function () {
var result = franc.all('the', {
'minLength': 3
Expand Down

0 comments on commit bddf79b

Please sign in to comment.