Socket
Socket
Sign inDemoInstall

multiline

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

multiline - npm Package Compare versions

Comparing version 0.1.0 to 0.2.0

12

multiline.js

@@ -11,5 +11,5 @@ /*!

// start matching after: comment start block => optional whitespace => newline
// start matching after: comment start block => ! or @preserve => optional whitespace => newline
// stop matching before: last newline => optional whitespace => comment end block
var reCommentContents = /\/\*\s*(?:\r\n|\n)([\s\S]*?)(?:\r\n|\n)\s*\*\//;
var reCommentContents = /\/\*!?(?:\@preserve)?\s*(?:\r\n|\n)([\s\S]*?)(?:\r\n|\n)\s*\*\//;

@@ -21,3 +21,9 @@ var multiline = function (fn) {

return reCommentContents.exec(fn.toString())[1];
var match = reCommentContents.exec(fn.toString());
if (!match) {
throw new TypeError('Multiline comment missing.');
}
return match[1];
};

@@ -24,0 +30,0 @@

{
"name": "multiline",
"version": "0.1.0",
"version": "0.2.0",
"description": "Multiline strings in JavaScript",

@@ -5,0 +5,0 @@ "license": "MIT",

@@ -1,2 +0,2 @@

# multiline [![Build Status](https://secure.travis-ci.org/sindresorhus/multiline.png?branch=master)](http://travis-ci.org/sindresorhus/multiline)
# multiline [![Build Status](https://travis-ci.org/sindresorhus/multiline.png?branch=master)](https://travis-ci.org/sindresorhus/multiline)

@@ -9,3 +9,29 @@ > Multiline strings in JavaScript

#### Before
```js
var str = '' +
'<!doctype html>' +
'<html>' +
' <body>' +
' <h1>❤ unicorns</h1>' +
' </body>' +
'</html>' +
'';
```
#### After
```js
var str = multiline(function(){/*
<!doctype html>
<html>
<body>
<h1>❤ unicorns</h1>
</body>
</html>
*/});
```
## How

@@ -15,3 +41,3 @@

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.
Even though it's [slower than string concat](http://jsperf.com/multiline), that shouldn't realistically matter as you can still do 2 million of those a second. Convenience over micro performance always.

@@ -21,50 +47,91 @@

Download [manually](https://github.com/sindresorhus/multiline/releases) or with a package-manager.
```bash
$ npm install --save multiline
```
#### [npm](https://npmjs.org/package/multiline)
```
npm install --save multiline
```
## Example
#### [Bower](http://bower.io)
Everything after the first newline and before the last will be returned as seen below:
```js
var str = multiline(function(){/*
<!doctype html>
<html>
<body>
<h1>❤ unicorns</h1>
</body>
</html>
*/});
console.log(str);
```
bower install --save multiline
```
#### [Component](https://github.com/component/component)
Which outputs:
```
component install sindresorhus/multiline
<!doctype html>
<html>
<body>
<h1>❤ unicorns</h1>
</body>
</html>
```
And a [real-world use-case](https://github.com/sindresorhus/pageres/blob/cb85922dec2b962c7b45484023c9ba43a9abf6bd/cli.js#L14-L33).
## Example
Everything after the first newline and before the last will be returned as seen below:
## Browser
### 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 `0` after the comment so it's not removed as dead-code.
The final result would be:
```js
var str = multiline(function(){/*
var str = multiline(function(){/*!@preserve
<!doctype html>
<html>
<body>
<h1>Hello world!</h1>
<h1>❤ unicorns</h1>
</body>
</html>
*/});
*/0});
```
console.log(str);
// <!doctype html>
// <html>
// <body>
// <h1>Hello world!</h1>
// </body>
// </html>
### Install
Download [manually](https://github.com/sindresorhus/multiline/releases) or with a package-manager.
#### [Bower](http://bower.io)
```
bower install --save multiline
```
#### [Component](https://github.com/component/component)
```
component install sindresorhus/multiline
```
## 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.
I've also done an [experiment](experiment.js) where you don't need the anonymous function. It's too fragile and slow to be practical though.

@@ -78,3 +145,3 @@ It generates a callstack and extracts the contents of the comment in the function call.

<body>
<h1>Hello world!</h1>
<h1>❤ unicorns</h1>
</body>

@@ -86,4 +153,32 @@ </html>

## FAQ
### But JS already has multiline strings with `\`?
```js
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:
```js
var str = 'foo\n\
bar';
```
But then you could just as well concatenate:
```js
var str = 'foo\n' +
'bar';
```
*Note that ES6 will have real [multiline strings](https://github.com/lukehoban/es6features#template-strings).*
## License
MIT © [Sindre Sorhus](http://sindresorhus.com)
[MIT](http://opensource.org/licenses/MIT) © [Sindre Sorhus](http://sindresorhus.com)
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc