A set of well-tested template literal tag functions for ES2015+ that solve common string manipulation and formatting problems.
npx @tessl/cli install tessl/npm-common-tags@1.8.0Common Tags is a comprehensive set of well-tested template literal tag functions for ES2015+ that solve common string manipulation and formatting problems in JavaScript development. It offers specialized tags for stripping indentation, converting multiline strings to single lines, safely rendering HTML with XSS protection, and formatting arrays as lists with various delimiters and conjunctions.
npm install common-tagsimport { stripIndent, html, oneLine, commaLists } from "common-tags";For CommonJS:
const { stripIndent, html, oneLine, commaLists } = require("common-tags");Direct module imports (for tree-shaking):
import stripIndent from "common-tags/lib/stripIndent";
import html from "common-tags/lib/html";import { stripIndent, html, commaLists } from "common-tags";
// Strip indentation while preserving relative indents
const code = stripIndent`
function hello() {
console.log("Hello World");
}
`;
// Render HTML with proper indentation
const items = ['apple', 'banana', 'cherry'];
const htmlOutput = html`
<ul>
${items.map(item => `<li>${item}</li>`)}
</ul>
`;
// Format arrays as comma-separated lists
const message = commaLists`
I like ${['apples', 'bananas', 'watermelons']}
They're delicious!
`;
// "I like apples, bananas, watermelons\nThey're delicious!"Common Tags is built on a flexible TemplateTag class architecture that supports transformer plugins and composable pipelines:
TemplateTag class that processes template literals through transformer pluginsCore template tags for manipulating string formatting and indentation. Perfect for code generation, template rendering, and text processing.
// Strip indentation while preserving relative structure
function stripIndent(strings, ...expressions): string;
// Strip all indentation from every line
function stripIndents(strings, ...expressions): string;
// Convert multiline strings to single line
function oneLine(strings, ...expressions): string;
// Convert to single line and trim whitespace
function oneLineTrim(strings, ...expressions): string;HTML-focused template tags with indentation handling and XSS protection for safe HTML rendering.
// Render HTML with proper array and newline indentation
function html(strings, ...expressions): string;
// HTML-safe rendering with XSS protection
function safeHtml(strings, ...expressions): string;Template tags specialized for formatting arrays as readable lists with various separators and conjunctions.
// Render arrays as space-separated lists
function inlineLists(strings, ...expressions): string;
// Render arrays as comma-separated lists
function commaLists(strings, ...expressions): string;
// Comma-separated with "and" conjunction
function commaListsAnd(strings, ...expressions): string;
// Comma-separated with "or" conjunction
function commaListsOr(strings, ...expressions): string;Powerful TemplateTag class and transformer system for building custom template tags with composable functionality.
// Core class for creating custom template tags
class TemplateTag {
constructor(...transformers);
tag(strings, ...expressions): string | Function;
}
// Built-in transformers for common operations
function trimResultTransformer(side?: string): Transformer;
function stripIndentTransformer(type?: string): Transformer;
function inlineArrayTransformer(opts?: ArrayOptions): Transformer;
function replaceResultTransformer(replaceWhat: string | RegExp, replaceWith: string): Transformer;interface Transformer {
onString?(str: string): string;
onSubstitution?(substitution: any, resultSoFar: string): any;
onEndResult?(endResult: string): string;
}
interface ArrayOptions {
separator?: string; // Default: ''
conjunction?: string; // Default: ''
serial?: boolean; // Default: false
}All template tags support tail processing by accepting a function as the first argument:
import { oneLine } from "common-tags";
// Instead of nested template tags
oneLine`${String.raw`foo\nbar`}`;
// Use tail processing for cleaner composition
oneLine(String.raw)`foo\nbar`;Template tags can be called as regular functions on strings:
import { stripIndent } from "common-tags";
// Use as template tag
const result1 = stripIndent`
indented text
`;
// Use as function
const result2 = stripIndent(" indented text\\n more text");