multiline

WebJar for multiline

Лицензия

Лицензия

MIT
Группа

Группа

org.webjars.npm
Идентификатор

Идентификатор

multiline
Последняя версия

Последняя версия

1.0.2
Дата

Дата

Тип

Тип

jar
Описание

Описание

multiline
WebJar for multiline
Ссылка на сайт

Ссылка на сайт

http://webjars.org
Система контроля версий

Система контроля версий

https://github.com/sindresorhus/multiline

Скачать multiline

Как подключить последнюю версию

<!-- https://jarcasting.com/artifacts/org.webjars.npm/multiline/ -->
<dependency>
    <groupId>org.webjars.npm</groupId>
    <artifactId>multiline</artifactId>
    <version>1.0.2</version>
</dependency>
// https://jarcasting.com/artifacts/org.webjars.npm/multiline/
implementation 'org.webjars.npm:multiline:1.0.2'
// https://jarcasting.com/artifacts/org.webjars.npm/multiline/
implementation ("org.webjars.npm:multiline:1.0.2")
'org.webjars.npm:multiline:jar:1.0.2'
<dependency org="org.webjars.npm" name="multiline" rev="1.0.2">
  <artifact name="multiline" type="jar" />
</dependency>
@Grapes(
@Grab(group='org.webjars.npm', module='multiline', version='1.0.2')
)
libraryDependencies += "org.webjars.npm" % "multiline" % "1.0.2"
[org.webjars.npm/multiline "1.0.2"]

Зависимости

compile (1)

Идентификатор библиотеки Тип Версия
org.webjars.npm : strip-indent jar [1.0.0,2)

Модули Проекта

Данный проект не имеет модулей.

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

Версии библиотеки

Версия
1.0.2