Security News
RubyGems.org Adds New Maintainer Role
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Multiline strings in JavaScript
No more string concatenation or array join!
Use ES2015 template literals instead whenever possible.
const str = '' +
'<!doctype html>' +
'<html>' +
' <body>' +
' <h1>❤ unicorns</h1>' +
' </body>' +
'</html>' +
'';
const str = multiline(()=>{/*
<!doctype html>
<html>
<body>
<h1>❤ unicorns</h1>
</body>
</html>
*/});
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.
$ npm install multiline
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>
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>
console.log()
supports string substitution:
const str = 'unicorns';
console.log(multiline(()=>{/*
I love %s
*/}), str);
//=> 'I love unicorns'
Have one? Let me know.
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>
*/);
\
?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';
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.
Even though minifiers strip comments by default there are ways to preserve them:
/*@preserve
instead of /*
and enable the comments
option/*@preserve
instead of /*
/*!
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});
MIT © Sindre Sorhus
FAQs
Multiline strings in JavaScript
The npm package multiline receives a total of 63,149 weekly downloads. As such, multiline popularity was classified as popular.
We found that multiline demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.
Security News
Research
Socket's threat research team has detected five malicious npm packages targeting Roblox developers, deploying malware to steal credentials and personal data.