UNPKG

@rashedmakkouk/dev-utils

Version:
28 lines (27 loc) 866 B
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const isString_1 = __importDefault(require("lodash/isString")); /** * Extracts the first character from the first and last words in a string. * * Splits at: white space, comma, dot, pipe, underscore, dash. * * @returns Extracted characters as string. */ function initials(text = '') { if (!(0, isString_1.default)(text)) { return ''; } const parts = text.split(/[\s,.|_-]+/g).filter((part) => !!part); if (!parts.length) { return ''; } const first = parts[0].charAt(0); return parts.length === 1 ? first : `${first}${parts[parts.length - 1].charAt(0)}`; } exports.default = initials;