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 |3 Answers
Reset to default 25Rather 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.
const isString = x => typeof x === 'string'
? – Dan Mandel Commented Dec 13, 2017 at 16:48const isString = x => typeof x === 'string' && x instanceof String
– WilomGfx Commented Dec 13, 2017 at 16:50