usefulTags

A tiny JavaScript library incorporating some useful template tag functions.


code size: < 10kb license: MIT release codefactor code style: prettier

usefulTags is a small JavaScript library that adds a few useful and commonly used ES6 template tag functions. It can also be used as a regular function working with normal strings.

4 functions are exposed:

  1. stripIndent
  2. stripAllIndents
  3. oneLine
  4. oneLineConcatenate

This project is not yet complete and functionality is subject to change. Full documentation is in the works.

Installation

Node.js / iojs

Any version is supported.

Install the package with npm i usefultags.

For CommonJS enviroments:

const usefulTags = require("usefultags");
//...

For ESModule enviroments:

import {stripIndent, stripAllIndents, oneLine, oneLineConcatenate} from "usefultags";
//...

For RequireJS enviroments

const requirejs = require("requirejs");

requirejs.config({
    nodeRequire: require
});

requirejs(["usefultags"], (usefulTags) => {
    //...
});

Deno

import * from "https://deno.land/x/usefultags@ver/usefulTags.mjs";
//...

Make sure you replace @ver with the version tag you plan on using.


Browsers

Make sure you replace @ver with the verion tag you plan on using for all examples below.

For ES6 browsers

<script type="module">
    import {stripIndent, stripAllIndents, oneLine, oneLineConcatenate} from "https://unpkg.com/usefultags@ver/usefulTags.mjs";
    //...
</script>

For IEFF targets

<script src="https://unpkg.com/usefultags@ver/dist/usefulTags.js"></script>
<script>
    //...
</script>

For RequireJS

<script src="https://requirejs.org/docs/release/2.3.6/minified/require.js"></script>
<script>
    requirejs(["https://unpkg.com/usefultags@ver/dist/usefulTags.js"], (usefulTags) => {
        //...
});
</script>

Usage

stripIndent`
    hello
    world`;
stripIndent(
    "hello\n" +
    "world"
);

stripIndent representing any of the functions. A string or array is required as an argument when used as a normal function.

Better documentation is still being worked on.

Examples:

stripIndent:

const line = stripIndent`
        This
        is
        ${"a"}
        multi-line
        newline
         
            indented  
        string.
        Random number: ${Math.random()}.`;
console.log(line);

Expected Output:

This
is
a
multi-line
newline
 
    indented  
string.
Random number: 0.xxxxxxxxxxxxxxxx.

stripAllIndents:

const line = stripAllIndents`This
    is
         ${"a"}
      multi-line
        newline
         
            indented  
        string.
        Random number: ${Math.random()}.`;
console.log(line);

Expected Output:

This
is
a
multi-line
newline

indented  
string.
Random number: 0.xxxxxxxxxxxxxxxx.

oneLine:

const line = oneLine`
        This
        is
        ${"a"}
        multi-line
        newline
         
            indented  
        string.
        Random number: ${Math.random()}.`;
console.log(line);

Expected Output:

This is a multi-line newline indented  string. Random number: 0.xxxxxxxxxxxxxxxx.

oneLineConcatenate:

const line = oneLineConcatenate`
        This
        is
        ${"a"}
        multi-line
        newline
         
            indented  
        string.
        Random number: ${Math.random()}.`;
console.log(line);
Expected Output:
Thisisamulti-linenewlineindented  string.Random number: 0.xxxxxxxxxxxxxxxx.