ne-tag-fns
Template Tags Function Library
Overview
I am constantly finding myself rewriting variations of this code. So, in big girl fashion, I am putting out a library I can reuse and share with others who seek a similar solution.
Installation
Simply add the module to your code as a regular, peer or dev dependency as you see fit. These instructions presume you will use it as a normal dependency.
cd <your project folder>npm install --save ne-tag-fns
Tag Functions Included
Tag functions included with the library that are directly usable are
dropLowest
dedent
inline
inlineJoin
inline
Usage
The inline
tag function takes the contents of a given string, even spread over several lines, and converts it to a nice single spaced output. This makes string creation in organizations where you must conform to 80 columns far easier to work with.
Inline does this by performing a few steps
- If either the first or last line are simply whitespace, they are removed
- Each line is trim'ed of leading and trailing whitespace
- All remaining carriage returns and new lines will be converted to a single space.
Example
const inline = // or let description = inline` This is a nice message, and its contents will be all on a single line. ` description === `This is a nice message, and its contents will be all on a single line` // true
inlineJoin
Usage
The inlineJoin
tag function takes the contents of a given string, even spread over several lines, and converts it to a nice single spaced output. This makes string creation in organizations where you must conform to 80 columns far easier to work with.
inlineJoin does this by performing a few steps
- If either the first or last line are simply whitespace, they are removed
- Each line is trim'ed of leading and trailing whitespace
- All remaining carriage returns and new lines will be converted to the supplied string.
- Optionally, all leading newlines can be trimmed
- Optionally, all trailing newlines can be trimmed
Unlike inline
, inlineJoin
needs to be executed with parameters before it
is used as a tag function as its execution actually returns the tag function
customized in the manner desired.
Example
const inlineJoin = // or let description = ` ABC DEF ` === 'ABC-DEF' // true let skipLeading = ` ABC DEF ` === 'ABCDEF' // true let skipLeadingAndTrailing = ` ABC DEF GHI ` === `ABC-DEF-GHI` // true
dropLowest
Usage
In your application, wherever you may have strings that span longer than your code style allows or where you simply wish to clean up your code, import the function and use.
const dropLowest = ; // or { return dropLowest` // A comment that has some code in it function contrivedFunction() { return 42 } `;} // or, more simply... const someString = dropLowest` Some string where all lines in the comment will have their common amount of shared whitespace removed. By default the lowest count, in this case 2, will be dropped and these two lines will also be flush to the left to allow for some margin of error. But this set of lines will be indented by the difference, so two spaces in. - Brie`
dedent
Usage
If you do not like that the lowest indentation count is dropped, you can use dedent
to turn off this behavior
const dedent = ; // or const someString = dedent` Some string where all lines in the comment will have their common amount of shared whitespace removed. With custom the lowest count, in this case 2, will not be dropped and these two lines will also keep their indentation But this set of lines will be indented as seen, so four spaces in. - Brie`