Here Document Using ES2015 Template String
This package provides heredoc as tag of ES2015 Template String.
It trims white spaces in the template string and strips common indentation like <<~
in Ruby.
This package has no dependency.
const heredoc = ; { const msg = heredoc` Error: Expected: Actual : `; assert; console;}
Without this package, we need to care about indentation of the string.
const msg = `Error: Expected: Actual : `;
Installation
Available via npm.
$ npm install heredocument
- commonjs
const heredoc = ;
- ES6 Modules
;
- Browser
Usage
By default, a heredoc tag for template strings is exported. Almost all cases, you can use it out of box.
const heredoc = ;console;
This package cares about tab (\t
) character. By default, tab character is handled as 8 whitespaces.
If the indentation is split at the middle of tab character, it will be split into white spaces.
const heredoc = ;assert;
And default newline is NL (\n
).
These behaviors can be customized. There are two options; tabSize
, inputNewline
and outputNewline
.
const heredoc = tabSize: 4 inputNewline: '\n\r' outputNewline: '\n\r'; assert;// => OK
- tabSize : The number of whitespaces for 1 tab character (
\t
). Default is8
. - inputNewline : The newline character to split the string into lines. Default value is
\n
. - outputNewline : The newline character used in output string. If not specified, value of
inputNewline
is used. Default value is\n
. - newlineAtEnd : EOL should be added at the end of string or not. If
true
, newline will be added at the end. Default value istrue
.
For example, below oneline
tag replaces all newlines in input hence it can create single line string from multiline template string.
const heredoc = ;const oneline = ; const msg = oneline` This message is too long. So we need to split the string literal such as "blah blah" + "blah blah". But with this 'oneline' tag, all newlines (including intepolated string) are removed and you can get oneline string.`; assert;// => OK
Development
$ git clone https://github.com/rhysd/heredocument.git && cd ./heredocument$ npm install # Install all dependencies $ npm run lint # Run linter $ npm test # Run tests with mocha $ npm bench # Run benchmark $ npm profile # Run profiling
License
Distributed under the MIT license.