UNPKG

@karmaniverous/tagged-templates

Version:

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

93 lines (86 loc) 3.39 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.sn2u = exports.sn2n = exports.sn2e = exports.normstr = exports.n2e = exports.def = void 0; /** * @module taggedTemplates */ /** * Normalize a string by converting diacriticals to base characters, removing non-word characters, and converting to lower case. * * @param {string} str - The string to normalize. * @returns {string} The normalized string or undefined if not a string. */ const normstr = str => str?.normalize && str?.replace && str?.toLowerCase ? str.normalize('NFKD').replace(/[\p{Diacritic}\W]/gu, '').toLowerCase() : undefined; /** * Replicates standard string template behavior. * * @param {string[]} strings - The string literals. * @param {...any} exp - The expressions. * @returns {string} The output string. */ exports.normstr = normstr; const def = function (strings) { for (var _len = arguments.length, exp = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { exp[_key - 1] = arguments[_key]; } return exp.reduce((output, e, i) => output + e + strings[i + 1], strings[0]); }; /** * Nil to Empty: replaces nil expressions with empty strings. * * @param {string[]} strings - The string literals. * @param {...any} exp - The expressions. * @returns {string} The output string. */ exports.def = def; const n2e = function (strings) { for (var _len2 = arguments.length, exp = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) { exp[_key2 - 1] = arguments[_key2]; } return exp.reduce((output, e, i) => output + (e ?? '') + strings[i + 1], strings[0]); }; /** * Some Nil to Empty: returns empty string when any expression nil. * * @param {string[]} strings - The string literals. * @param {...any} exp - The expressions. * @returns {string} The output string. */ exports.n2e = n2e; const sn2e = function (strings) { for (var _len3 = arguments.length, exp = new Array(_len3 > 1 ? _len3 - 1 : 0), _key3 = 1; _key3 < _len3; _key3++) { exp[_key3 - 1] = arguments[_key3]; } return exp.some(e => (e ?? undefined) === undefined) ? '' : exp.reduce((output, e, i) => output + e + strings[i + 1], strings[0]); }; /** * Some Nil to Null: returns null when any expression nil. * * @param {string[]} strings - The string literals. * @param {...any} exp - The expressions. * @returns {string} The output string. */ exports.sn2e = sn2e; const sn2n = function (strings) { for (var _len4 = arguments.length, exp = new Array(_len4 > 1 ? _len4 - 1 : 0), _key4 = 1; _key4 < _len4; _key4++) { exp[_key4 - 1] = arguments[_key4]; } return exp.some(e => (e ?? undefined) === undefined) ? null : exp.reduce((output, e, i) => output + e + strings[i + 1], strings[0]); }; /** * Some Nil to Undefined: returns undefined when any expression nil. * * @param {string[]} strings - The string literals. * @param {...any} exp - The expressions. * @returns {string} The output string. */ exports.sn2n = sn2n; const sn2u = function (strings) { for (var _len5 = arguments.length, exp = new Array(_len5 > 1 ? _len5 - 1 : 0), _key5 = 1; _key5 < _len5; _key5++) { exp[_key5 - 1] = arguments[_key5]; } return exp.some(e => (e ?? undefined) === undefined) ? undefined : exp.reduce((output, e, i) => output + e + strings[i + 1], strings[0]); }; exports.sn2u = sn2u;