News for hackers. Stuff that matters.
Re:Multiline strings in JavaScript by mappum on Saturday July 12, 2014 @06:20AM (#8024178)
That's a really smart hack, I didn't realize comments were included in the output of Function#toString. You could do other cool things, e.g. an annotation for type checking:

  function checkType(f) {
    return function(a) {
      var type = f.toString().match(/\/\/(.*)\n/)[1].trim();
      if(type !== typeof(a)) throw new Error('Invalid type');
      return f(a);
    }
  }
  
  var halve = checkType(function(n) {
    // number
    return n / 2;
  });
  
  halve(4); // returns 2
  halve('string'); // emits error
Re:Multiline strings in JavaScript by tlrobinson on Saturday July 12, 2014 @07:13AM (#8024259)
I didn't realize comments were included in the output of Function#toString

Probably because sometimes they're not, depending on the JavaScript engine (though perhaps all of the major modern browsers do)

Re:Multiline strings in JavaScript by mofle on Saturday July 12, 2014 @07:29AM (#8024287)
The following browsers return comments:

- Latest Chrome

- Firefox >=17

- Safari >=4

- Opera >=9

- Internet Explorer >=6

From: https://github.com/sindresorhus/multiline#compatibility

Re:Multiline strings in JavaScript by coldtea on Saturday July 12, 2014 @10:01AM (#8024558)
Not just "Latest Chrome". Chrome has been doing it for at least 2 years. I know, because I'm using it with it.
Re:Multiline strings in JavaScript by manojlds on Saturday July 12, 2014 @10:28AM (#8024608)
I think Chrome is mentioned as "Latest Chrome" because:

1) There have been so many versions and not easy to know what came when.

2) Most people get updated to Chrome latest automatically without any fuss.

Re:Multiline strings in JavaScript by msujaws on Saturday July 12, 2014 @01:39PM (#8025193)
In fact, (2) is not as true as it used to be. This graph, http://cdn.arstechnica.net/wp-content/uploads/2014/07/chrome..., shows that about 6-7 percent of worldwide browsers are out-of-date Chrome installs, and the trend is for that to grow.

From http://arstechnica.com/information-technology/2014/07/window...

Re:Multiline strings in JavaScript by mofle on Saturday July 12, 2014 @11:01AM (#8024723)
That's correct. Chrome is practically versionless.
Re:Multiline strings in JavaScript by igl on Saturday July 12, 2014 @09:41AM (#8024518)
Closure Compiler.
Re:Multiline strings in JavaScript by adrusi on Saturday July 12, 2014 @06:42AM (#8024204)
When I was first really to program I was into hacking javascript to do the crazy things I wanted it to do. I basically did exactly this, albeit with a worse interpolation feature[1]. Don't actually do this please. In the browser, just open up a different <script type="text/plain"> and grab the string from that tag. On the server you can just use a file. Any string that's actually long enough for `["foo", "bar"].join("\n")` to be too cumbersome should be in a separate file, especially if the other way involves a massive hack.

[1]: http://zurb.com/forrst/posts/Javascript_native_multi_line_st...

Re:Multiline strings in JavaScript by jonpacker on Saturday July 12, 2014 @10:24AM (#8024603)
So you would suggest using external text files to store every SQL/Cypher query that is used in a model-based node app?
Re:Multiline strings in JavaScript by adrusi on Saturday July 12, 2014 @12:29PM (#8024968)
Well, if the query is longer than a 3-4 lines then it's really a program in its own right, and yes, I think it deserves its own file.
Re:Multiline strings in JavaScript by wldlyinaccurate on Saturday July 12, 2014 @07:04AM (#8024237)
Probably worth pointing out that this won't play nice with most minification utilities unless you don't mind leaving all comments in the output.

I think for this reason I still prefer using Array.prototype.join for multiline strings:

    var str = [
        '<!doctype html>',
        '<html>',
            '<body>',
                '<h1>❤ unicorns</h1>',
            '</body>',
        '</html>',
    ].join('');
Re:Multiline strings in JavaScript by gravity13 on Saturday July 12, 2014 @07:16AM (#8024266)
You can also

  var str = ' \
      <!doctype html> \
      <html> \
        <body> \
          <h1>❤ unicorns</h1> \
        </body> \
      </html> \
  ' 
I find it's better than string concat because the trailing \ is just one annoying thing rather than three.
Re:Multiline strings in JavaScript by jaredmcateer on Saturday July 12, 2014 @11:41AM (#8024820)
6 months later:

    var str = ' \
        <!doctype html> \
        <html> \
          <body> \ 
            <h1>❤ unicorns</h1> \
          </body> \
        </html> \
    ' 
Help I'm maintaining your code and I get a syntax error
Re:Multiline strings in JavaScript by jasonkostempski on Saturday July 12, 2014 @01:29PM (#8025160)
HA! I (don't) see what you did there :)
Re:Multiline strings in JavaScript by msujaws on Saturday July 12, 2014 @01:37PM (#8025183)
There is a space following one of the slashes.
Re:Multiline strings in JavaScript by tekacs on Saturday July 12, 2014 @07:46AM (#8024317)
... also this is (presumably) 'joined' at parse time, rather than runtime (and so only once per execution, guaranteed).
Re:Multiline strings in JavaScript by mofle on Saturday July 12, 2014 @07:08AM (#8024248)
You can use the `/*@preserve` directive.

See: https://github.com/sindresorhus/multiline#minification

Re:Multiline strings in JavaScript by wldlyinaccurate on Saturday July 12, 2014 @07:13AM (#8024260)
Hey, thanks - that'll teach me for not reading the full page!
Re:Multiline strings in JavaScript by andrey-p on Saturday July 12, 2014 @11:49AM (#8024839)
Ooh I really really like this. Preserving newlines (one of the stated usecases for multiline) is dead easy too.

  var str = [
    //...
  ].join("\n");
Re:Multiline strings in JavaScript by abrkn on Saturday July 12, 2014 @06:09AM (#8024165)
Re:Multiline strings in JavaScript by martin-adams on Saturday July 12, 2014 @06:15AM (#8024173)
Interesting solution. I usually find this type of problem when doing templating in the client when updating things via Ajax. I either use the DOM to store the template or end up concatenating for smaller things.

Since to preserve these comments in minification requires conscious effort, I wonder if there's an alternative that could convert the comments to regular JS before minification. Obviously you can't just run it in Node as normal doing it this way.

Re:Multiline strings in JavaScript by tlrobinson on Saturday July 12, 2014 @07:14AM (#8024263)
FYI, CoffeeScript has multiline strings (and a bunch of other nice things!)
Re:Multiline strings in JavaScript by mofle on Saturday July 12, 2014 @07:46AM (#8024319)
The next version of JavaScript does too: http://www.2ality.com/2011/09/quasi-literals.html
Re:Multiline strings in JavaScript by mushishi on Saturday July 12, 2014 @07:34AM (#8024290)
Sorry but what does this have to do with anything?
Re:Multiline strings in JavaScript by tlrobinson on Saturday July 12, 2014 @07:45AM (#8024315)
If you want niceties like multiline strings, CoffeeScript is a better option than clever hacks like this.

    var str = multiline(function(){/*!@preserve
    multi
    line
    string
    */console.log});
Really?
Re:Multiline strings in JavaScript by mushishi on Saturday July 12, 2014 @09:21AM (#8024464)
Well, I don't get what is the point of suggesting alternative languages that have multiline strings when we are talking about one specific language.

The topic is about how to do multiline string in Javascript in one way, and one alternative is not to use multiline strings in Javascript. It's kinda big jump from the original purpose of the topic to start suggesting alternative languages... if we are to be this slack, then we can start spamming most threads with weakly related stuff. For example, you can easily advertise coffeescript in each and every javascript thread in a similar way you did in this specific thread but do we really need want that?

Re:Multiline strings in JavaScript by nottombrown on Saturday July 12, 2014 @12:04PM (#8024886)
Coffeescript is just javascript with syntactic sugar on top. This library provides sugar for multiline strings.
Re:Multiline strings in JavaScript by idlewan on Saturday July 12, 2014 @04:38PM (#8025719)
To me, this library tastes more like too much salt.
Re:Multiline strings in JavaScript by asdfologist on Saturday July 12, 2014 @09:40AM (#8024514)
I do. I love learning about closely related topics while reading the comments.
Re:Multiline strings in JavaScript by coldtea on Saturday July 12, 2014 @10:09AM (#8024579)
So, a transpiler, with new syntax, and its own set of BS issues, including an added step in your JS workflow (for the transpiling), is better than a small library doing exactly what you want?
Re:Multiline strings in JavaScript by applecore on Saturday July 12, 2014 @01:00PM (#8025061)
Yes.
Re:Multiline strings in JavaScript by coldtea on Saturday July 12, 2014 @01:27PM (#8025154)
Well, no.
Re:Multiline strings in JavaScript by PavlovsCat on Saturday July 12, 2014 @06:05AM (#8024154)
I thank you from the bottom of my heart. This drove me crazy when playing around with WebGL and shaders, because for various reasons I didn't want to put those in separate files.
Re:Multiline strings in JavaScript by waynecochran on Saturday July 12, 2014 @12:46PM (#8025026)
Why aren't you just doing this, for example?

        <script id="fragment" type="x-shader">
	precision highp float;
	uniform vec3 glyphColor;
	void main() {
	        gl_FragColor = vec4(glyphColor, 1.0);
	}
	</script>
Re:Multiline strings in JavaScript by PavlovsCat on Sunday July 13, 2014 @02:38AM (#8026819)
Because you can't do that from within javascript, and my HTML was really just a stub to load all the javascript I didn't want to have to otherwise deal with.
Re:Multiline strings in JavaScript by ttty on Saturday July 12, 2014 @06:15AM (#8024174)
The first problem I see, is using any build process which will remove all the comments. Therefore your string will become ''. I wonder how to avoid this.
Re:Multiline strings in JavaScript by mofle on Saturday July 12, 2014 @06:22AM (#8024179)
It's mainly intended for use in Node.js, but I've documented how to use it with a minifier for the front-end: https://github.com/sindresorhus/multiline#minification
Re:Multiline strings in JavaScript by criswell on Saturday July 12, 2014 @08:51AM (#8024415)
There's a section on minification here: https://github.com/sindresorhus/multiline#minification
Re:Multiline strings in JavaScript by evilpie on Saturday July 12, 2014 @08:02AM (#8024348)
>Note that ES6 will have template string which can be multiline, but time...

Firefox Nightly currently supports just enough of template strings to give you multiline strings.

Re:Multiline strings in JavaScript by Excavator on Saturday July 12, 2014 @02:10PM (#8025305)
Since a couple weeks back templating is also implemented¹, just lacking tagged templates and documentation²³.

1: https://bugzilla.mozilla.org/show_bug.cgi?id=688857#c22

2: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...

3: https://developer.mozilla.org/en-US/docs/Web/JavaScript/ECMA...

Re:Multiline strings in JavaScript by mattdesl on Saturday July 12, 2014 @09:51AM (#8024531)
Cool hack, but a cleaner solution would be to just use fs.readFileSync if you really want long strings, and let browserify/brfs to inline it for you in the browser.

Using this in a library would be pretty unsafe, given how it's browser-dependent, is prone to issues with minifcation, and could just break with future versions of your JS engine.

Re:Multiline strings in JavaScript by cyphunk on Saturday July 12, 2014 @08:31AM (#8024394)
seriously? you people be nuts

    http://stackoverflow.com/a/15558082/2371924
    var myString = function(){/*
      multi-line
      string
    */}.toString().slice(14,-3)
take this code snippet, throw a package.json and some yaml in and make another npm to Show HN.
Re:Multiline strings in JavaScript by coldtea on Saturday July 12, 2014 @10:06AM (#8024568)
Yeah, only it does several other things do. Like optionally removing indend whitespace.

Did you even read TFA?

Re:Multiline strings in JavaScript by eob on Saturday July 12, 2014 @03:17PM (#8025514)
At one point the E4X standard (back in 2007?) enabled a cool multi-line string hack by promoting XML elements to language entities. You could say:

    var htmlFragment = "" + (<r><![CDATA[
    l(a
    
    le
    af
    fa
    
    ll
    
    s)
    one
    l
    
    iness
    
    - e.e. cummings
    ;
    ]]></r>);
And then `htmlFragment` would contain your multi-line string. (The addition with the empty string coerces the <r> element to output its `toString()` value, which happens to be the innerHTML).

This is largely a historical factoid, though. I don't think browsers beyond Mozilla ended up supporting E4X.

Really clever hack with the function's toString method!

Re:Multiline strings in JavaScript by jasonkostempski on Saturday July 12, 2014 @01:21PM (#8025142)
Cool hack but not supporting multiline strings is a feature as far as I'm concerned. Everyone seems to agree that HTML shouldn't contain JavaScript, why should JavaScript contain HTML?
Re:Multiline strings in JavaScript by biscarch on Saturday July 12, 2014 @02:05PM (#8025293)
That is actually currently being contested by React and .jsx.

That said, I've seen multiline abused for writing SQL queries as well, which would have been better off being written in a separate .SQL file and `fs.read` into a string with a descriptive var name. (Ignoring that massive SQL strings is a code smell imo).

Re:Multiline strings in JavaScript by siculars on Saturday July 12, 2014 @04:58PM (#8025766)
Except if your application generates dynamic queries. And not of the sort where variables in the WHERE clause are dynamically evaluated. Of the sort where the entire structure of your SQL is dynamically driven by user interaction with your application.
Re:Multiline strings in JavaScript by biscarch on Saturday July 12, 2014 @08:27PM (#8026207)
Are you saying that large SQL strings isn't a code smell if the application is dynamically putting them together? That seems like DSL/Query Builder territory to me. Perhaps you could share an example?
Re:Multiline strings in JavaScript by andrelaszlo on Saturday July 12, 2014 @07:41AM (#8024305)
This snippet does the same thing, mostly:

    var multiline = function(){/*
    foo
    bar
    baz
    */}.toSource().match(/\/\*([\s\S]*)\*\//)[1].trim()
Re:Multiline strings in JavaScript by mofle on Saturday July 12, 2014 @07:45AM (#8024314)
Yeah, pretty much, but in Node we wrap repetitive things in modules, as they're super cheap. Your snippet is also missing some things like indentation stripping, error handling and browser support.
Re:Multiline strings in JavaScript by jgillich on Saturday July 12, 2014 @08:59AM (#8024434)
I just use ES6 template strings together with the es6ify transform for Browserify. Works in all browsers and very likely performs much better than using Function.prototype.toString.
Re:Multiline strings in JavaScript by z3t4 on Saturday July 12, 2014 @04:04PM (#8025647)
If you do multi line strings you could probably do it in a smarter way. Examples:

appendChild instead of innerHTML .html file instead of html inside a .js file. .css file instead of css inside a .js file.

It can get very messy if you store "data" inside JS. Especially if you store markup or styling in the JS.

Re:Multiline strings in JavaScript by tach4n on Saturday July 12, 2014 @03:27PM (#8025545)
Wouldn't something like a Sweet.js macro be better for this? Just have it output a string with '\n\' at the end of lines, or using Array.join() or whatever you like.
Re:Multiline strings in JavaScript by chrishawes on Saturday July 12, 2014 @06:37AM (#8024196)
This is really handy for DFP creative templates. Thank you.
Re:Multiline strings in JavaScript by teddyh on Saturday July 12, 2014 @08:16AM (#8024374)
Today I learned that in JavaScript, comments are significant and part of the code.

(And people complain about Python having significant whitespace?)

Re:Multiline strings in JavaScript by Kiro on Saturday July 12, 2014 @08:17AM (#8024377)
Only significant if you want it to be.
Re:Multiline strings in JavaScript by tfb on Saturday July 12, 2014 @01:08PM (#8025088)
Weird... I just just randomly looking at this a few days ago upon a search for "js multiline" when wondering about the common way to spell multiline (multi line vs multi-line vs multiline). I was intrigued by his code though, ultimately in awe of the cleverness and conciseness of it. Very cool hack! Crazy coincidence to see the very code on HN only a few days later.
Re:Multiline strings in JavaScript by ricardobeat on Saturday July 12, 2014 @08:52AM (#8024419)
Be sure to scroll to the bottom for a real-world example, this is very brittle.
Re:Multiline strings in JavaScript by VeejayRampay on Saturday July 12, 2014 @10:46AM (#8024657)
In other news, the ubiquitous language of the ubiquitous platform of the information age doesn't support multiline strings. But I guess if we keep throwing enough modules and libraries at it, it'll end up being nice to develop for.

Alternatively wait for ES6 or ES7 to (maybe) solve the problem.

Re:Multiline strings in JavaScript by robgibbons on Saturday July 12, 2014 @03:49PM (#8025613)
This is a very clever hack. Great use of anonymous functions and toString.
Re:Multiline strings in JavaScript by dsego on Saturday July 12, 2014 @07:48AM (#8024322)
Why not a pre-processor?
Re:Multiline strings in JavaScript by mofle on Saturday July 12, 2014 @07:57AM (#8024342)
In Node.js you don't want a compile step. In the browser however that would probably be a better solution indeed.
Re:Multiline strings in JavaScript by aikah on Saturday July 12, 2014 @09:38AM (#8024505)
no compile step needed in nodejs. Most third party languages can hack require so using the transpiled language requires no manual compilation.
Re:Multiline strings in JavaScript by alextgordon on Saturday July 12, 2014 @08:04AM (#8024353)
Why don't you want a compile step?
Re:Multiline strings in JavaScript by mofle on Saturday July 12, 2014 @08:16AM (#8024375)
Because a compile step is annoying when it's not strictly needed. In the browser you usually have some kind of build system for concatenation and minifying anyways.
Re:Multiline strings in JavaScript by knubie on Saturday July 12, 2014 @08:46AM (#8024411)
With coffeescript, at least, there is no extra compile step from the user's point of view. You can run `coffee myapp.coffee` in the same way you would run `node myapp.js`
Re:Multiline strings in JavaScript by igl on Saturday July 12, 2014 @07:02AM (#8024233)
I think i saw 8 libraries before who abused comments like this.

Edit: Yawn.