Skip to content
This repository has been archived by the owner on Sep 5, 2024. It is now read-only.

karmaniverous/tagged-templates

Repository files navigation

tagged-templates

Here are some handy tagged template functions to make your ES6 template literals work better!

Installation

npm install @karmaniverous/tagged-templates

Usage

import { def, n2e, sn2e, sn2u } from '@karmaniverous/tagged-templates';

// Replicate default template literal functionality. Just FYI!
console.log(def`a${'b'}c`); // 'abc'
console.log(def`a${undefined}c`); // 'aundefinedc'

// Replace any nil expression (i.e. null or undefined) with an empty string.
console.log(n2e`a${undefined}c`); // 'ac'

// Return an empty string if any expression is nil.
console.log(sn2e`a${undefined}c`); // ''

// Return null if any expression is nil.
console.log(sn2n`a${undefined}c`); // null

// Return undefined if any expression is nil.
console.log(sn2u`a${undefined}c`); // undefined

API Documentation

taggedTemplates

taggedTemplates.normstr ⇒ string

Normalize a string by converting diacriticals to base characters, removing non-word characters, and converting to lower case.

Kind: static constant of taggedTemplates
Returns: string - The normalized string or undefined if not a string.

Param Type Description
str string The string to normalize.

taggedTemplates.def ⇒ string

Replicates standard string template behavior.

Kind: static constant of taggedTemplates
Returns: string - The output string.

Param Type Description
strings Array.<string> The string literals.
...exp any The expressions.

taggedTemplates.n2e ⇒ string

Nil to Empty: replaces nil expressions with empty strings.

Kind: static constant of taggedTemplates
Returns: string - The output string.

Param Type Description
strings Array.<string> The string literals.
...exp any The expressions.

taggedTemplates.sn2e ⇒ string

Some Nil to Empty: returns empty string when any expression nil.

Kind: static constant of taggedTemplates
Returns: string - The output string.

Param Type Description
strings Array.<string> The string literals.
...exp any The expressions.

taggedTemplates.sn2n ⇒ string

Some Nil to Null: returns null when any expression nil.

Kind: static constant of taggedTemplates
Returns: string - The output string.

Param Type Description
strings Array.<string> The string literals.
...exp any The expressions.

taggedTemplates.sn2u ⇒ string

Some Nil to Undefined: returns undefined when any expression nil.

Kind: static constant of taggedTemplates
Returns: string - The output string.

Param Type Description
strings Array.<string> The string literals.
...exp any The expressions.

See more great templates and other tools on my GitHub Profile!