sindresorhus / multiline

Multiline strings in JavaScript

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Code based unicode symbols

piuccio opened this issue · comments

Do you think it's possible to extend multiline to support unicode characters?

In my tests I need to prepare multi line text including control characters, like this

console.log(multiline(function(){/*
\0
\u0000
*/}));

But the \ is escaped and displayed as a \, I'd like to insert the \0 control character in the string.

That comes out as:

\0
\u0000

for me.

Can you submit a failing test or a fix?

Exactly, it comes as a string of two characters \ and 0.

What I'd like is to have one character, the control character 0.

You can try with \n or \t.

Maybe I'll have time to submit a test tomorrow

Hmm, I guess i could decode the string with decodeURIComponent().

@mathiasbynens thoughts?

That’s not what decodeURIComponent does.

The easiest way to turn '\\0' into '\0' (and similar for any other possible type of escape sequence) is to use eval, sadly. Be careful not to introduce any security vulnerabilities along the way :( I do something like it here: https://github.com/mathiasbynens/mothereff.in/blob/1a74e1614c7b4293ac28572a007bd6aada3e267e/js-escapes/eff.js#L61-L70

You could try to do the unescaping statically with a bunch of .replace() calls but it’s tricky.

@mathiasbynens localeval looks extremely slow as it uses childprocess/worker.

Can you make a module based on https://github.com/mathiasbynens/mothereff.in/blob/1a74e1614c7b4293ac28572a007bd6aada3e267e/js-escapes/eff.js#L61-L70 that I can recommend?

IMHO this should be WONTFIXed. The main reason for using multiline is to not have to use escape sequences (for line terminators). If you want to use escape sequences in your strings, then don’t use multiline.

Fixing it would be confusing — people expecting some level of compatibility with template strings (as implied in the README) would then get incorrect results. See the first example on https://github.com/lukehoban/es6features#template-strings which the README links to:

// Basic literal string creation
`In JavaScript '\n' is a line-feed.`

That template string is equivalent to 'In JavaScript \'\\n\' is a line-feed.'. Fixing this issue would make multiline behave differently for no good reason AFAICT.

In OP’s case, just use '\0\n\u0000' or just '\0\n\0'. There’s a tool that makes this sort of thing easy for you: http://mothereff.in/js-escapes#1%5C0%0A%5Cu0000

@mathiasbynens good point. i completely agree. thanks for chiming in :)

Thanks for the useful references