0

Python's repr() method converts strings to their raw value.

So that:

aa a
Hello

Becomes

aa a\nHello

I tried to do the same in a node.js script with util.inspect but it had converted:

GET / HTTP/1.1
Host: localhost:8081
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:74.0) Gecko/20100101 Firefox/74.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive
Upgrade-Insecure-Requests: 1
Cache-Control: max-age=0

to

'GET / HTTP/1.1\r\n' +
  'Host: localhost:8081\r\n' +
  'User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:74.0) Gecko/20100101 Firefox/74.0\r\n' +
  'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8\r\n' +
  'Accept-Language: en-US,en;q=0.5\r\n' +
  'Accept-Encoding: gzip, deflate\r\n' +
  'Connection: keep-alive\r\n' +
  'Upgrade-Insecure-Requests: 1\r\n' +
  '\r\n'

Q

How do I get the same results as python's repr, without the un-necessary + signs and concatenations? (it should be one long string).

toti
  • 325
  • 4
  • 12
  • Is it for you ? https://stackoverflow.com/questions/24902061/is-there-an-repr-equivalent-for-javascript – BENARD Patrick Apr 08 '20 at 11:26
  • 1
    I don't think there is a built-in tool for what you ask. But what do you actually need this for because this seems a bit like [an XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). I can't think of a good reason to convert a string *content* into a string *literal*. – VLAZ Apr 08 '20 at 11:28
  • @BENARDPatrick - No, as the solution is not in there.. please un-duplicate me.. – toti Apr 08 '20 at 13:56
  • @VLAZ - I need to pass line-seperated string to another process via IPC.. it's either that or serializing – toti Apr 08 '20 at 13:57
  • @toti OK, so what's wrong with serialising to JSON? – VLAZ Apr 08 '20 at 14:00
  • Why serialize to JSON when you can feed your curiosity and figure out what's wrong util.inspect should do the job but behaving weirdly. – toti Apr 08 '20 at 14:07
  • I don't think anything is *wrong* with it. In fact, looking at [the documentation](https://nodejs.org/api/util.html#util_util_inspect_object_options) it explicitly says "*The `util.inspect()` method returns a string representation of `object` that is **intended for debugging**.*" (emphasis mine). The very next sentence reads "*The output of `util.inspect` may change at any time and **should not be depended upon programmatically***". Emphasis mine, again. So, it seems that the tool isn't wrong - however, its usage *here* is. – VLAZ Apr 08 '20 at 14:25
  • What can I do then? :( – toti Apr 09 '20 at 11:01

0 Answers0