最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Can Ramda have isString? - Stack Overflow

programmeradmin3浏览0评论

Background

I am trying to use ramda and I need a pure function that lets me know if a given input is a string or not, much like lodash _.isString.

Question

After searching everywhere I couldn't find anything in Ramda for this. So I wonder, is there a way, using any of Ramda's existing functions, that I can create a isString function?

I find this horribly limiting and is it is not possible i might just use lodash in the end :S

Background

I am trying to use ramda and I need a pure function that lets me know if a given input is a string or not, much like lodash _.isString.

Question

After searching everywhere I couldn't find anything in Ramda for this. So I wonder, is there a way, using any of Ramda's existing functions, that I can create a isString function?

I find this horribly limiting and is it is not possible i might just use lodash in the end :S

Share Improve this question asked Dec 13, 2017 at 16:45 Flame_PhoenixFlame_Phoenix 17.6k40 gold badges143 silver badges283 bronze badges 5
  • 1 Is there anything wrong with using const isString = x => typeof x === 'string' ? – Dan Mandel Commented Dec 13, 2017 at 16:48
  • just use const isString = x => typeof x === 'string' && x instanceof String – WilomGfx Commented Dec 13, 2017 at 16:50
  • 1 Or look at this answer for a more robust way of checking stackoverflow.com/a/17772086/2880747 – WilomGfx Commented Dec 13, 2017 at 16:51
  • So, what I conclude from your posts is that Ramda does not have such a function and neither can I compose it using Ramda tools. Is there a feedback channel where I can propose such a thing? – Flame_Phoenix Commented Dec 13, 2017 at 17:16
  • See my answer for how Ramda supplies this. But you can ask questions on Ramda's Gitter Channel or raise issues on Ramda's Issues page. – Scott Sauyet Commented Dec 13, 2017 at 17:44
Add a comment  | 

3 Answers 3

Reset to default 25

Rather than have isString, isObject, isArray, isFunction, etc, Ramda simply provides is, which you can use to create any of these you like:

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

And you don't have to create the intermediate function. You can just use it as is:

R.is(String, 'foo') //=> true
R.is(String, {a: 'foo'}) //=> false

this should also work R.type(x) === "String"

If you're looking for predicate functions for ramda you should look into ramda-adjunct library. The most popular and widely used extension to ramda, that comes with goodies ramda doesn't have, nor will never implement due to various reasons. I do maintain the library and we have have more than 25 contributors.

Regarding R.is, I would be careful with this function, it uses JavaScript instanceof operator under the hood. When inspecting objects from different JavaScript realms/envs e.g. IFrame, you may be surprised with the result of R.is return value.

发布评论

评论列表(0)

  1. 暂无评论