question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`toString` is not idempotent for strings?

See original GitHub issue

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"'

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:19 (17 by maintainers)

github_iconTop GitHub Comments

2reactions
semmelcommented, May 3, 2017

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’s R.map, R.filter, R.reduce are obvious. Then there are functions like R.test and R.match which just dispatch to JS. Fine. However, except for Numbers, R.toString has nothing to do with JavaScript’s toString().

I still have not encountered a situation there R.toString() could come in handy:

  • for dictionary Objects and Arrays it’s JSON.stringify(),
  • for Strings it’s something like String.quote() (or JSON.stringify()),
  • for Numbers it’s Number.toString(10) (or JSON.stringify()),
  • for Date it produces the JS code of a date constructor call in a string

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. Applying R.toString() a number of times results in multiply quoted strings.

2reactions
dmitrizcommented, Apr 22, 2017

Oh I see, I’ve thought that kind of method is also called inspect.

toString reads 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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

What is the JSLint approved way to convert a number to a ...
I believe that the JSLint approved way is to call .toString() on the number: var stringified = 1..toString(); // Note the use of...
Read more >
String.prototype.toString() - JavaScript - MDN Web Docs
The toString() method returns a string representing the specified string value.
Read more >
Function.prototype.toString revision - TC39
If the object was defined using ECMAScript code and the returned string representation is not in the form of a MethodDefinition or GeneratorMethod...
Read more >
Query idempotence - DataStax Java Driver
The driver does not parse query strings, so it can't infer it automatically (except for statements coming from the query builder, see below)....
Read more >
What are Idempotent and Safe methods of HTTP and REST ...
Safe methods are HTTP methods that do not modify the resource like a GET request is safe ... (answer); 3 ways to convert...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found