Skip to content

Instantly share code, notes, and snippets.

@ramirezsandin
Last active August 11, 2022 06:36
Show Gist options
  • Save ramirezsandin/024ef7492959d0d6ec78b9096f77526f to your computer and use it in GitHub Desktop.
Save ramirezsandin/024ef7492959d0d6ec78b9096f77526f to your computer and use it in GitHub Desktop.
Extract the number of initials from a given string. Ideal for names.
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