Hacker News new | past | comments | ask | show | jobs | submit login
Multiline strings in JavaScript (github.com/sindresorhus)
98 points by mofle on July 12, 2014 | hide | past | favorite | 68 comments



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


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)


The following browsers return comments:

- Latest Chrome

- Firefox >=17

- Safari >=4

- Opera >=9

- Internet Explorer >=6

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


Not just "Latest Chrome". Chrome has been doing it for at least 2 years. I know, because I'm using it with it.


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.


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


That's correct. Chrome is practically versionless.


Closure Compiler.


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


So you would suggest using external text files to store every SQL/Cypher query that is used in a model-based node app?


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.


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('');


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.


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


HA! I (don't) see what you did there :)


There is a space following one of the slashes.


... also this is (presumably) 'joined' at parse time, rather than runtime (and so only once per execution, guaranteed).


You can use the `/*@preserve` directive.

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


Hey, thanks - that'll teach me for not reading the full page!


Ooh I really really like this. Preserving newlines (one of the stated usecases for multiline) is dead easy too.

  var str = [
    //...
  ].join("\n");



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.


FYI, CoffeeScript has multiline strings (and a bunch of other nice things!)


The next version of JavaScript does too: http://www.2ality.com/2011/09/quasi-literals.html


Sorry but what does this have to do with anything?


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?


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?


Coffeescript is just javascript with syntactic sugar on top. This library provides sugar for multiline strings.


To me, this library tastes more like too much salt.


I do. I love learning about closely related topics while reading the comments.


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?


Yes.


Well, no.


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.


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>


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.


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.


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


There's a section on minification here: https://github.com/sindresorhus/multiline#minification


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


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


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.


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.


Yeah, only it does several other things do. Like optionally removing indend whitespace.

Did you even read TFA?


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!


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?


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).


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.


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?


This snippet does the same thing, mostly:

    var multiline = function(){/*
    foo
    bar
    baz
    */}.toSource().match(/\/\*([\s\S]*)\*\//)[1].trim()


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.


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.


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.


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.


This is really handy for DFP creative templates. Thank you.


Today I learned that in JavaScript, comments are significant and part of the code.

(And people complain about Python having significant whitespace?)


Only significant if you want it to be.


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.


Be sure to scroll to the bottom for a real-world example, this is very brittle.


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.


This is a very clever hack. Great use of anonymous functions and toString.


Why not a pre-processor?


In Node.js you don't want a compile step. In the browser however that would probably be a better solution indeed.


no compile step needed in nodejs. Most third party languages can hack require so using the transpiled language requires no manual compilation.


Why don't you want a compile step?


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.


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`


I think i saw 8 libraries before who abused comments like this.

Edit: Yawn.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: