ramda: `toString` is not idempotent for strings?
Somewhat surprisingly:
R.toString('abc'); //=> "\"abc\""
'abc'.toString(); //=> "abc"
Is it intended?
http://ramdajs.com/repl/?v=0.23.0#?toString('abc')%3B %2F%2F%3D> '"abc"'
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 19 (17 by maintainers)
I agree with @dmitriz that the name
R.toString()is surprising and I would expect it to be idempotent for Strings.For a beginner the similarities between JavaScript’s functional torso in Array’s
map,filter,reduce, etc. and Ramda’sR.map,R.filter,R.reduceare obvious. Then there are functions likeR.testandR.matchwhich just dispatch to JS. Fine. However, except for Numbers,R.toStringhas nothing to do with JavaScript’stoString().I still have not encountered a situation there
R.toString()could come in handy:JSON.stringify(),String.quote()(orJSON.stringify()),Number.toString(10)(orJSON.stringify()),Idempotence: The JS
toString()method is often used to ensure that a variable is of type String if one is in doubt whether it is a Number or already a String. ApplyingR.toString()a number of times results in multiply quoted strings.Oh I see, I’ve thought that kind of method is also called
inspect.toStringreads like the JS traditional type conversion to String, which is also identical with Lodash:https://lodash.com/docs/4.17.4#toString
I understand that Ramda is different from Lodash, but still find it unfortunate that methods with the same name produce different results.
We could deprecate
R.toStringand suggest that users migrate toshow.@skirankumar7: No. Among other differences:
In this case, @dmitriz, we have
R.toString(R.toString({a: {a: 1}, b: [1]})).R.toStringcould choose between single and double quotes in order to minimize escaping, but I don’t like this option because:'and"will still require escaping).@semmel, it sounds as though
Stringis the function you’re after. 😉R.toStringis very helpful when writing a REPL or generating documentation. In these contexts it is not acceptable for0and"0",/x/and"/x/", etc. to have colliding string representations.