Skip to content

Commit c2b9a80

Browse files
committed
GH-21 comparison check fail for short & empty string. Fixed other small issues - typos, namings.
1 parent 5183bea commit c2b9a80

2 files changed

Lines changed: 22 additions & 10 deletions

File tree

lib/LanguageDetect.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ var LanguageDetect = module.exports = function (languageType) {
6868
this.unicodeMap = dbLang['trigram-unicodemap'];
6969

7070
this.languageType = languageType || null;
71-
}
71+
};
7272

7373
LanguageDetect.prototype = {
7474

@@ -170,10 +170,11 @@ LanguageDetect.prototype = {
170170
*/
171171
detect:function (sample, limit) {
172172
var me = this
173-
, limit = +limit || 0
174173
, scores = [];
175174

176-
if (sample == '' && sample.length < 3) return [];
175+
limit = +limit || 0;
176+
177+
if (sample == '' || String(sample).length < 3) return [];
177178

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

185186
if (trigramCount == 0) return [];
186187

187-
var keys = [], i;
188+
var keys = [], i, lang;
188189

189190
if (this.useUnicodeNarrowing) {
190191
var blocks = sampleObj.getUnicodeBlocks()
191-
, langs = Object.keys(blocks)
192-
, keysLength = langs.length;
192+
, languages = Object.keys(blocks)
193+
, keysLength = languages.length;
193194

194-
for (var i = keysLength; i--;) {
195-
if (this.unicodeMap[langs[i]]) {
196-
for (var lang in this.unicodeMap[langs[i]]) {
195+
for (i = keysLength; i--;) {
196+
if (this.unicodeMap[languages[i]]) {
197+
for (lang in this.unicodeMap[languages[i]]) {
197198
if (!~keys.indexOf(lang)) keys.push(lang);
198199
}
199200
}
@@ -229,4 +230,4 @@ LanguageDetect.prototype = {
229230
// limit the number of returned scores
230231
return limit > 0 ? scores.slice(0, limit) : scores;
231232
}
232-
}
233+
};

test/LanguageDetect.test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,17 @@ exports.getLanguageCount = function (t) {
8181
return t.done();
8282
};
8383

84+
exports.detectShortString = function(t) {
85+
var l = new LanguageDetect();
86+
87+
t.deepEqual([], l.detect(''));
88+
t.deepEqual([], l.detect('a'));
89+
t.deepEqual([], l.detect('ab'));
90+
t.notDeepEqual([], l.detect('abc'));
91+
92+
return t.done();
93+
};
94+
8495
exports.detectEnglish = function (t) {
8596
var l = new LanguageDetect();
8697

0 commit comments

Comments
 (0)