UNPKG

@synotech/utils

Version:

a collection of utilities for internal use

33 lines (32 loc) 890 B
/** * This method returns initials from a given name string * @module initials * @param {string} alt - a name string to get initials from * @return {string} {String} a string of initials * @example * * initials('Zeal Murapa') // returns ZM * */ export const initials = (alt: any): string => { let init; alt = alt && alt.trim(); alt = alt.replace(/-/g, ''); alt = alt.replace(/,/g, ''); try { if (alt && alt.split(' ').length >= 2) { init = alt.split(' '); init = init.map((str: any) => Array.from(str)[0]); init = init + ''; init = init && init.replace(/,/g, ''); // @ts-ignore } else if (alt && Array.from(alt)[0].length === 1) { init = `${Array.from(alt)[0]}${Array.from(alt)[1]}`; } init = typeof init === 'string' && init?.toUpperCase(); init = typeof init === 'string' && init.substr(0, 2); } catch (error) { // silent } return init; };