Here are some handy tagged template functions to make your ES6 template literals work better!
npm install @karmaniverous/tagged-templates
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
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. |
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. |
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. |
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. |
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. |
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!