“stringValues”已定义但从未使用过no-unused-vars

时间:2016-09-07 16:04:41

标签: javascript jquery html css jslint

我是JS的新手,我试图在另一个函数中调用一个函数。当我尝试它时,我得到了lint错误。 - stringValues在数字之间插入逗号以显示正确的格式,1000表示为1,000,而10000表示为10,000

 return Number(unitsOfNumbers.join('')).stringValues();
  • 在下面提供我的代码和错误。

ERROR:

"stringValues" is defined but never used  no-unused-vars

CODE:

  import {differentCountriesCurrency} from 'sports-input-utils/lib/formatting';

    Object.defineProperty(exports, '__esModule', {
        value: true
    });
    exports.appleBrowser = appleBrowser;
    exports.appleBrowserWithDecimals = appleBrowserWithDecimals;


    function stringValues(x, sep, grp) {
        var sx = (''+x).split('.'), s = '', i, j;
        sep || (sep = ','); // default seperator
        grp || grp === 0 || (grp = 3); // default grouping
        i = sx[0].length;
        while (i > grp) {
            j = i - grp;
            s = sep + sx[0].slice(j, i) + s;
            i = j;
        }
        s = sx[0].slice(0, i) + s;
        sx[0] = s;
        return sx.join('.');
    }

    function appleBrowser(value, parm) {
        var unitsOfNumbers;
        if (!value) {
            return value;
        }

        // extract unitsOfNumbers. if no unitsOfNumbers, fill in a zero.
        unitsOfNumbers = value.match(/\d/g) || ['0'];
        return Number(unitsOfNumbers.join('')).stringValues();
    }

    function appleBrowserWithDecimals(value, parm) {
        var unitsOfNumbers;
        if (!value) {
            return value;
        }

        // extract unitsOfNumbers. if no unitsOfNumbers, fill in a zero.
        unitsOfNumbers = value.match(/\d/g) || ['0'];

        // zero-pad a one-digit input
        if (unitsOfNumbers.length === 1) {
            unitsOfNumbers.unshift('0');
        }

        // add a decimal point
        unitsOfNumbers.splice(unitsOfNumbers.length - 2, 0, '.');

        return Number(unitsOfNumbers.join('')).stringValues();
    }
    //# sourceMappingURL=formatting.js.map

    exports.limitMaximumLength = limitMaximumLength;
    function limitMaximumLength(value, parm) {
        if (value.length < parm) {
            return value;
        } else {
            return value.substring(0, parm);
        }
    }

    exports.differentCountriesCurrencyWithMaxLen = differentCountriesCurrencyWithMaxLen;
    function differentCountriesCurrencyWithMaxLen (value) {
        var isSafari;
        return differentCountriesCurrency(limitMaximumLength(value, 7));

        isSafari = navigator.userAgent.indexOf("Safari") > -1;
        if (isSafari) {
            return appleBrowser(limitMaximumLength(value, 7));
        }
    }

1 个答案:

答案 0 :(得分:1)

不担心其他掉线错误或检查有关逻辑的任何内容(未检查它是否符合您的要求),我可以删除no-unused-vars错误,如下所示:

import {differentCountriesCurrency} from 'sports-input-utils/lib/formatting';

Object.defineProperty(exports, '__esModule', {
    value: true
});

function stringValues(x, sep, grp) {
    var sx = (''+x).split('.'), s = '', i, j;
    sep || (sep = ','); // default seperator
    grp || grp === 0 || (grp = 3); // default grouping
    i = sx[0].length;
    while (i > grp) {
        j = i - grp;
        s = sep + sx[0].slice(j, i) + s;
        i = j;
    }
    s = sx[0].slice(0, i) + s;
    sx[0] = s;
    return sx.join('.');
};

exports.appleBrowser = function (value, parm) {
    var unitsOfNumbers;
    if (!value) {
        return value;
    }

    // extract unitsOfNumbers. if no unitsOfNumbers, fill in a zero.
    unitsOfNumbers = value.match(/\d/g) || ['0'];
    return stringValues(Number(unitsOfNumbers.join('')));
};

exports.appleBrowserWithDecimals = function (value, parm) {
    var unitsOfNumbers;
    if (!value) {
        return value;
    }

    // extract unitsOfNumbers. if no unitsOfNumbers, fill in a zero.
    unitsOfNumbers = value.match(/\d/g) || ['0'];

    // zero-pad a one-digit input
    if (unitsOfNumbers.length === 1) {
        unitsOfNumbers.unshift('0');
    }

    // add a decimal point
    unitsOfNumbers.splice(unitsOfNumbers.length - 2, 0, '.');

    return stringValues(Number(unitsOfNumbers.join('')));
};

exports.limitMaximumLength = function (value, parm) {
    if (value.length < parm) {
        return value;
    } else {
        return value.substring(0, parm);
    }
};

exports.differentCountriesCurrencyWithMaxLen = function (value) {
    var isSafari;
    return differentCountriesCurrency(limitMaximumLength(value, 7)); 
    // ^^^ There's no way this ^^^ is what you want. 
    // The rest of the function is always ignored. You've already returned.

    isSafari = navigator.userAgent.indexOf("Safari") > -1;
    if (isSafari) {
        return appleBrowser(limitMaximumLength(value, 7));
    }
};

为了消除这个错误,我完全按照@JosephYoung所说。您尚未将stringValues添加到任何数字返回。你在全球宣布它。

所以不要这样做:

return Number(unitsOfNumbers.join('')).stringValues();

假装在stringValues的返回值上有另一个Number作为函数属性。您的函数stringValues存在于全局范围内,永远不会被调用。

要调用您声明的内容,请执行以下操作:

return stringValues(Number(unitsOfNumbers.join('')));

需要注意的一个重要的可读性问题:而不是像你这样的行......

exports.limitMaximumLength = limitMaximumLength;
function limitMaximumLength(value, parm) {

...继续把作业放在一行这样......

exports.limitMaximumLength = function (value, parm) {

可以更轻松地告诉您要导出的内容以及什么是“私有”实用程序功能。