9

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

Flame_Phoenix
  • 16,489
  • 37
  • 131
  • 266
  • 1
    Is there anything wrong with using `const isString = x => typeof x === 'string'` ? – Dan Mandel Dec 13 '17 at 16:48
  • just use `const isString = x => typeof x === 'string' && x instanceof String` – WilomGfx Dec 13 '17 at 16:50
  • 1
    Or look at this answer for a more robust way of checking https://stackoverflow.com/a/17772086/2880747 – WilomGfx Dec 13 '17 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 Dec 13 '17 at 17:16
  • See my answer for how Ramda supplies this. But you can ask questions on Ramda's [Gitter Channel](https://gitter.im/ramda/ramda) or raise issues on Ramda's [Issues page](https://github.com/ramda/ramda/issues/new). – Scott Sauyet Dec 13 '17 at 17:44

3 Answers3

23

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
Scott Sauyet
  • 49,207
  • 4
  • 49
  • 103
0

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

Viliam Simko
  • 1,711
  • 17
  • 31
-2

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.

Vladimír Gorej
  • 217
  • 2
  • 7