wix-style-react
Version:
wix-style-react
31 lines (30 loc) • 1.05 kB
JavaScript
;
exports.__esModule = true;
exports.nameToInitials = nameToInitials;
/**
* Convert a space delimited full name to capitalized initials.
* Returned initials would not exceed 3 or 2 letters, according to provided `limit`.
* @param limit number If set to 3, then if name has more than 3 parts,
* then the 1st, 2nd and last parts would be used. If set to 2, then first and last parts are used.
*/
function nameToInitials(name) {
var limit = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 2;
if (!name) {
return '';
}
if (limit < 1 || limit > 3) {
limit = 2;
}
var initials = name.split(' ').map(s => s[0]);
if (limit === 1 && initials.length > 1) {
initials = [initials[0]];
}
if (limit === 2 && initials.length > 2) {
initials = [initials[0], initials[initials.length - 1]];
}
if (limit === 3 && initials.length > 3) {
initials = [initials[0], initials[1], initials[initials.length - 1]];
}
return initials.join('').toUpperCase();
}
//# sourceMappingURL=utils.js.map