node-language-detection icon indicating copy to clipboard operation
node-language-detection copied to clipboard

Bug in the detectOne Function

Open Talfa5-5 opened this issue 2 years ago • 0 comments

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;
};

Talfa5-5 avatar Dec 04 '23 18:12 Talfa5-5