GithubHelp home page GithubHelp logo

multiline's Introduction

multiline Build Status

Multiline strings in JavaScript

No more string concatenation or array join!

Note that ES6 will have template string which can be multiline, but time...

Before

var str = '' +
'<!doctype html>' +
'<html>' +
'	<body>' +
'		<h1>❤ unicorns</h1>' +
'	</body>' +
'</html>' +
'';

After

var str = multiline(function(){/*
<!doctype html>
<html>
	<body>
		<h1>❤ unicorns</h1>
	</body>
</html>
*/});

How

It works by wrapping the text in a block comment, anonymous function, and a function call. The anonymous function is passed into the function call and the contents of the comment extracted.

Even though it's slower than string concat, that shouldn't realistically matter as you can still do 2 million of those a second. Convenience over micro performance always.

Install

$ npm install --save multiline

Usage

Everything after the first newline and before the last will be returned as seen below:

var str = multiline(function(){/*
<!doctype html>
<html>
	<body>
		<h1>❤ unicorns</h1>
	</body>
</html>
*/});

Which outputs:

<!doctype html>
<html>
	<body>
		<h1>❤ unicorns</h1>
	</body>
</html>

Strip indent

You can use multiline.stripIndent() to be able to indent your multiline string without preserving the redundant leading whitespace.

	var str = multiline.stripIndent(function(){/*
			<!doctype html>
			<html>
				<body>
					<h1>❤ unicorns</h1>
				</body>
			</html>
	*/});

Which outputs:

<!doctype html>
<html>
	<body>
		<h1>❤ unicorns</h1>
	</body>
</html>

String substitution

console.log() supports string substitution:

var str = 'unicorns';

console.log(multiline(function(){/*
  I love %s
*/}), str);

//=> I love unicorns

Use cases

Have one? Let me know.

Experiment

I've also done an experiment where you don't need the anonymous function. It's too fragile and slow to be practical though.

It generates a callstack and extracts the contents of the comment in the function call.

var str = multiline(/*
<!doctype html>
<html>
	<body>
		<h1>❤ unicorns</h1>
	</body>
</html>
*/);

FAQ

But JS already has multiline strings with \?

var str = 'foo\
bar';

This is not a multiline string. It's line-continuation. It doesn't preserve newlines, which is the main reason for wanting multiline strings.

You would need to do the following:

var str = 'foo\n\
bar';

But then you could just as well concatenate:

var str = 'foo\n' +
'bar';

Note that ES6 will have real multiline strings.

Browser

While it does work fine in the browser, it's mainly intended for use in Node.js. Use at your own risk.

Install

$ npm install --save multiline

(with Browserify)

$ bower install --save multiline

Compatibility

  • Latest Chrome
  • Firefox >=17
  • Safari >=4
  • Opera >=9
  • Internet Explorer >=6

Minification

Even though minifiers strip comments by default there are ways to preserve them:

  • Uglify: Use /*@preserve instead of /* and enable the comments option
  • Closure Compiler: Use /*@preserve instead of /*
  • YUI Compressor: Use /*! instead of /*

You also need to add console.log after the comment so it's not removed as dead-code.

The final result would be:

var str = multiline(function(){/*!@preserve
<!doctype html>
<html>
	<body>
		<h1>❤ unicorns</h1>
	</body>
</html>
*/console.log});

License

MIT © Sindre Sorhus

multiline's People

Contributors

sindresorhus avatar sheerun avatar freethejazz avatar

Watchers

James Cloos avatar Peng Chen™ avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.