有效值:
- 是字符串还是数字(R.is(string), R.is(number)
- 不等于“-”、“$”和“0”
- 不为 null、false、未定义或 NaN (Number.isNaN)
如何使用 ramda 创建一个 const isValid = ...
函数?
where
函数已关闭 https://ramdajs.com/docs/#where 。但它只接受和反对您可以检查每个 Prop 的地方。
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>
关于javascript - Ramda,检查字符串是否有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56183296/