Skip to content

Commit

Permalink
GH-21 comparison check fail for short & empty string. Fixed other sma…
Browse files Browse the repository at this point in the history
…ll issues - typos, namings.
  • Loading branch information
RuslanZavacky committed Mar 1, 2014
1 parent 5183bea commit c2b9a80
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
21 changes: 11 additions & 10 deletions lib/LanguageDetect.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ var LanguageDetect = module.exports = function (languageType) {
this.unicodeMap = dbLang['trigram-unicodemap'];

this.languageType = languageType || null;
}
};

LanguageDetect.prototype = {

Expand Down Expand Up @@ -170,10 +170,11 @@ LanguageDetect.prototype = {
*/
detect:function (sample, limit) {
var me = this
, limit = +limit || 0
, scores = [];

if (sample == '' && sample.length < 3) return [];
limit = +limit || 0;

if (sample == '' || String(sample).length < 3) return [];

var sampleObj = new Parser(sample);
sampleObj.setPadStart(true);
Expand All @@ -184,16 +185,16 @@ LanguageDetect.prototype = {

if (trigramCount == 0) return [];

var keys = [], i;
var keys = [], i, lang;

if (this.useUnicodeNarrowing) {
var blocks = sampleObj.getUnicodeBlocks()
, langs = Object.keys(blocks)
, keysLength = langs.length;
, languages = Object.keys(blocks)
, keysLength = languages.length;

for (var i = keysLength; i--;) {
if (this.unicodeMap[langs[i]]) {
for (var lang in this.unicodeMap[langs[i]]) {
for (i = keysLength; i--;) {
if (this.unicodeMap[languages[i]]) {
for (lang in this.unicodeMap[languages[i]]) {
if (!~keys.indexOf(lang)) keys.push(lang);
}
}
Expand Down Expand Up @@ -229,4 +230,4 @@ LanguageDetect.prototype = {
// limit the number of returned scores
return limit > 0 ? scores.slice(0, limit) : scores;
}
}
};
11 changes: 11 additions & 0 deletions test/LanguageDetect.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,17 @@ exports.getLanguageCount = function (t) {
return t.done();
};

exports.detectShortString = function(t) {
var l = new LanguageDetect();

t.deepEqual([], l.detect(''));
t.deepEqual([], l.detect('a'));
t.deepEqual([], l.detect('ab'));
t.notDeepEqual([], l.detect('abc'));

return t.done();
};

exports.detectEnglish = function (t) {
var l = new LanguageDetect();

Expand Down

0 comments on commit c2b9a80

Please sign in to comment.