Skip to content

Bug in the detectOne Function #6

@Talfa5-5

Description

@Talfa5-5
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;
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions