我正在尝试使用ramda,我需要一个纯函数,让我知道一个给定的输入是否是一个字符串,就像lodash _.isString
。
在搜索到处后,我在Ramda找不到任何东西。所以我想知道,有没有办法,使用任何Ramda现有的函数,我可以创建一个isString
函数?
我发现这可怕的限制,我不可能最后使用lodash:S
而不是拥有isString
,isObject
,isArray
,isFunction
等,Ramda只提供is
,您可以使用它来创建您喜欢的任何这些:
const isString = R.is(String)
const isRectangle = R.is(Rectangle)
isString('foo') //=> true
isString(42) //=> false
isRectangle(new Rectangle(3, 5)) //=> true
isRectangle(new Square(7)) //=> true (if Rectangle is in the prototype chain of Square)
isRectangle(new Triangle(3, 4, 5)) //=> false
而且您不必创建中间函数。您可以按原样使用它:
R.is(String, 'foo') //=> true
R.is(String, {a: 'foo'}) //=> false
这也应该工作R.type(x) === "String"