All Projects → sindresorhus → Multiline

sindresorhus / Multiline

Licence: mit
Multiline strings in JavaScript

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Multiline

Gulp Ngmin
[DEPRECATED] Pre-minify AngularJS apps with ngmin
Stars: ✭ 89 (-93.78%)
Mutual labels:  deprecated
Pfincrementalstore
Offline Parse with Core Data Persistence, an NSIncrementalStore subclass.
Stars: ✭ 98 (-93.16%)
Mutual labels:  deprecated
Gulp Vulcanize
Concatenate a set of Web Components into one file
Stars: ✭ 101 (-92.95%)
Mutual labels:  deprecated
Notifier For Github Safari
Safari extension - Displays your GitHub notifications unread count
Stars: ✭ 90 (-93.72%)
Mutual labels:  deprecated
Haha
DEPRECATED Java library to automate the analysis of Android heap dumps.
Stars: ✭ 1,337 (-6.63%)
Mutual labels:  deprecated
One To One Sample Apps
DEPRECATED: OpenTok One-to-One Communication Sample App
Stars: ✭ 99 (-93.09%)
Mutual labels:  deprecated
Sass Rails Source Maps
DEPRECATED: Rails gem for generating sass source maps
Stars: ✭ 88 (-93.85%)
Mutual labels:  deprecated
Tinyeditor
JavaScript WYSIWYG Editor (deprecated)
Stars: ✭ 106 (-92.6%)
Mutual labels:  deprecated
Ohmy Auth
OAuth made easy for PHP (deprecated)
Stars: ✭ 98 (-93.16%)
Mutual labels:  deprecated
Gulp Ftp
[DEPRECATED] Upload files to an FTP-server
Stars: ✭ 100 (-93.02%)
Mutual labels:  deprecated
Secretary
DEPRECATED Secrets management for dynamic environments
Stars: ✭ 93 (-93.51%)
Mutual labels:  deprecated
React Combinators
[NOT MAINTAINED] Seamless combination of React and reactive programming
Stars: ✭ 95 (-93.37%)
Mutual labels:  deprecated
Baobab
DEPRECATED - The application that powers Gandi's Status website (status.gandi.net).
Stars: ✭ 99 (-93.09%)
Mutual labels:  deprecated
Julian
⛔️DEPRECATED Brilliantly clever PHP calendar class
Stars: ✭ 89 (-93.78%)
Mutual labels:  deprecated
Closure Linter
Automatically exported from code.google.com/p/closure-linter
Stars: ✭ 104 (-92.74%)
Mutual labels:  deprecated
Plum
A deployer library for PHP 5.3
Stars: ✭ 88 (-93.85%)
Mutual labels:  deprecated
Chef Rundeck
Integrates Chef with RunDeck, we are referring users to https://github.com/atheiman/better-chef-rundeck as this repository is inactive at this time.
Stars: ✭ 98 (-93.16%)
Mutual labels:  deprecated
Govuk template
❗️GOV.UK Template is deprecated, and will only receive major bug fixes and security patches. A template containing the GOV.UK header and footer, and associated assets.
Stars: ✭ 107 (-92.53%)
Mutual labels:  deprecated
Pytest Ipdb
Provides ipdb on failures for py.test.
Stars: ✭ 104 (-92.74%)
Mutual labels:  deprecated
Paper Reading
深度学习论文阅读、数据仓库实践体验。比做算法的懂工程落地,比做工程的懂算法模型。
Stars: ✭ 101 (-92.95%)
Mutual labels:  deprecated

multiline Build Status

Multiline strings in JavaScript

No more string concatenation or array join!

Use ES2015 template literals instead whenever possible.

Before

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

After

const str = multiline(()=>{/*
<!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 multiline

Usage

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

const str = multiline(()=>{/*
<!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.

	const str = multiline.stripIndent(()=>{/*
			<!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:

const str = 'unicorns';

console.log(multiline(()=>{/*
  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.

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

FAQ

But JS already has multiline strings with \?

const 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:

const str = 'foo\n\
bar';

But then you could just as well concatenate:

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

Browser

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

$ npm install multiline

With Webpack, Browserify, or something similar.

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:

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

License

MIT © Sindre Sorhus

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].