Last active
August 11, 2022 06:36
-
-
Save ramirezsandin/024ef7492959d0d6ec78b9096f77526f to your computer and use it in GitHub Desktop.
Extract the number of initials from a given string. Ideal for names.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function extractInitials (name: string, count = 2) { | |
const re = new RegExp(/\b\w/g) | |
const initials = name.match(re) | |
return initials !== null | |
? initials.reduce((prev, curr, index) => | |
index < count ? prev + curr : prev | |
) | |
: null | |
} | |
export function parseEmptyStringToUndefined(arg?: unknown) { | |
return typeof arg === "string" && arg === "" ? undefined : arg; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment