node-language-detection
node-language-detection copied to clipboard
Bug in the detectOne Function
exports.detectOne = function (text) {
var langs = exports.detect(text);
return langs.length > 0 ? langs[0].lang : null;
};
Above is the implementation of the detectOne function from index.js. If the detect function failed to detect the language, it will return null. However, the detectOne function assumes that this will never happen. It tries to get the length property of the output from the detect function without checking whether it is null first. I recommend fixing this by using the code below instead:
exports.detectOne = function (text) {
var langs = exports.detect(text);
if (langs === null) return null;
return langs.length > 0 ? langs[0].lang : null;
};