Ramda是否有isString函数?

15 浏览
0 Comments

Ramda是否有isString函数?

背景

我正在尝试使用ramda,我需要一个纯函数来判断给定的输入是否为字符串,就像lodash的_.isString一样。

问题

在搜索了所有地方之后,我在Ramda中找不到任何相关的东西。所以我想知道,是否有一种方法可以使用Ramda的任何现有函数来创建一个isString函数?

我发现这非常限制性,如果不可能的话,我最终可能会使用lodash :S

admin 更改状态以发布 2023年5月19日
0
0 Comments

这个也应该可以工作:R.type(x) === "String"

0
0 Comments

与其分别使用isStringisObjectisArrayisFunction等函数,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

0