gpt4 book ai didi

javascript - "stringValues"已定义但从未使用过 no-unused-vars

转载 作者:太空宇宙 更新时间:2023-11-04 09:47:28 26 4
gpt4 key购买 nike

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

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

错误:

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

代码:

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

最佳答案

不必担心其他 linting 错误或检查有关逻辑的任何内容(尚未检查它是否按照您的意愿行事),我可以像这样删除 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 添加到 Number 返回的任何内容。您在全局范围内声明了它。

所以不要这样做:

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) {

可以更轻松地判断您正在导出什么以及什么是“私有(private)”实用程序函数。

关于javascript - "stringValues"已定义但从未使用过 no-unused-vars,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39374685/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com