Ramda,检查字符串是否有效
Ramda, check if string is valid
有效值:
- 是字符串还是数字(R.is(字符串),R.is(数字)
- 不等于'-'和'$'和'0'
- 不是 null、false、undefined 或 NaN (Number.isNaN)
我如何使用 ramda 创建一个 const isValid = ...
函数?
where
功能关闭https://ramdajs.com/docs/#where。但它只接受和反对你可以检查每个道具的地方。
both
函数只接受两个参数。
这个任务最好用简单的方式解决JavaScript:
function isValid(value) {
if (typeof value !== 'string' && typeof value !== 'number')
return false;
if (value === '-' || value === '$' || value === '0')
return false;
if (value === null || value === false || value === undefined || Number.isNaN(value))
return false;
return true;
}
这很容易理解,相当短的代码。我看不出使用 Ramda 如何改善这一点。
这使用 Ramda 的方式尽我所能:
const isValid = arg => R.both(R.is(String, arg) || R.is(Number, arg), R.both(R.not(R.includes(arg, ["$", "-", "0"])), R.not(arg)));
如果将类型检查和内容检查分开,您仍然可以使用 both
:
const isValid = both(
either(is(String), is(Number)),
complement(anyPass([
isNil,
equals('0'),
equals('-'),
equals('$'),
equals(NaN),
equals(false)
]))
);
console.log(isValid([]));
console.log(isValid(1));
console.log(isValid(NaN));
console.log(isValid('foo'));
console.log(isValid('$'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.min.js"></script>
<script>const {both, either, is, complement, anyPass, isNil, equals} = R;</script>
首先,请注意您的某些规则是多余的。因为它必须是字符串或数字,所以它已经不能是 null
、false
或 undefined
。所以你的逻辑可以更简单。
我建议在这里将 Ramda 用于几个辅助函数,但也许不使用纯 point-free Ramda 函数。这似乎捕捉得很好:
const isValid = s =>
(is (String, s) || is (Number, s)) && ! includes (s, [NaN, '-', '$', '0'] );
['a', 'b', 42, false, NaN, null, '-', '-0', '$', {}].forEach(s =>
console.log(`${s} => ${isValid(s)}`)
)
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.js"></script><script>
const {is, includes} = R </script>
相同代码的纯 JS 版本如下所示:
const isValid = s =>
(typeof s == 'string' || typeof s == 'number') && ! [NaN, '-', '$', '0'] .includes (s);
Ramda 版本稍微简单一些,在我看来,更具可读性。我永远不会因为一点点不同就包括 Ramda。但如果已经在使用 Ramda,那么这个版本就有意义了。
但是完全 point-free Ramda 版本对我来说看起来 更少 简单。这并不可怕,但我不会选择它,即使它很容易写:
const isValid = both (
either ( is (String), is (Number) ),
complement (includes (__, [NaN, '-', '$', '0'] ) )
)
有效值:
- 是字符串还是数字(R.is(字符串),R.is(数字)
- 不等于'-'和'$'和'0'
- 不是 null、false、undefined 或 NaN (Number.isNaN)
我如何使用 ramda 创建一个 const isValid = ...
函数?
where
功能关闭https://ramdajs.com/docs/#where。但它只接受和反对你可以检查每个道具的地方。
both
函数只接受两个参数。
这个任务最好用简单的方式解决JavaScript:
function isValid(value) {
if (typeof value !== 'string' && typeof value !== 'number')
return false;
if (value === '-' || value === '$' || value === '0')
return false;
if (value === null || value === false || value === undefined || Number.isNaN(value))
return false;
return true;
}
这很容易理解,相当短的代码。我看不出使用 Ramda 如何改善这一点。
这使用 Ramda 的方式尽我所能:
const isValid = arg => R.both(R.is(String, arg) || R.is(Number, arg), R.both(R.not(R.includes(arg, ["$", "-", "0"])), R.not(arg)));
如果将类型检查和内容检查分开,您仍然可以使用 both
:
const isValid = both(
either(is(String), is(Number)),
complement(anyPass([
isNil,
equals('0'),
equals('-'),
equals('$'),
equals(NaN),
equals(false)
]))
);
console.log(isValid([]));
console.log(isValid(1));
console.log(isValid(NaN));
console.log(isValid('foo'));
console.log(isValid('$'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.min.js"></script>
<script>const {both, either, is, complement, anyPass, isNil, equals} = R;</script>
首先,请注意您的某些规则是多余的。因为它必须是字符串或数字,所以它已经不能是 null
、false
或 undefined
。所以你的逻辑可以更简单。
我建议在这里将 Ramda 用于几个辅助函数,但也许不使用纯 point-free Ramda 函数。这似乎捕捉得很好:
const isValid = s =>
(is (String, s) || is (Number, s)) && ! includes (s, [NaN, '-', '$', '0'] );
['a', 'b', 42, false, NaN, null, '-', '-0', '$', {}].forEach(s =>
console.log(`${s} => ${isValid(s)}`)
)
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.js"></script><script>
const {is, includes} = R </script>
相同代码的纯 JS 版本如下所示:
const isValid = s =>
(typeof s == 'string' || typeof s == 'number') && ! [NaN, '-', '$', '0'] .includes (s);
Ramda 版本稍微简单一些,在我看来,更具可读性。我永远不会因为一点点不同就包括 Ramda。但如果已经在使用 Ramda,那么这个版本就有意义了。
但是完全 point-free Ramda 版本对我来说看起来 更少 简单。这并不可怕,但我不会选择它,即使它很容易写:
const isValid = both (
either ( is (String), is (Number) ),
complement (includes (__, [NaN, '-', '$', '0'] ) )
)