Skip to content

Commit

Permalink
Update getInitials function
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrkulpinski committed Sep 13, 2023
1 parent e96474b commit 5902b4d
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@curiousleaf/utils",
"description": "A lightweight set of utilities",
"version": "1.0.2",
"version": "1.0.3",
"license": "MIT",
"module": "./dist/index.js",
"type": "module",
Expand Down
40 changes: 37 additions & 3 deletions src/helpers/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,43 @@ describe("isTruthy", () => {
})

describe("getInitials", () => {
it("gets the initials from a string", () => {
expect(getInitials("John Doe")).toEqual("JD")
expect(getInitials("jane doe")).toEqual("JD")
it("should return empty string if value is undefined", () => {
expect(getInitials(undefined)).toEqual("")
})

it("should return empty string if value is null", () => {
expect(getInitials(null)).toEqual("")
})

it("should return empty string if value is an empty string", () => {
expect(getInitials("")).toEqual("")
})

it("should return the initials of a single name", () => {
expect(getInitials("John")).toEqual("J")
})

it("should return the initials of two names", () => {
expect(getInitials("John Doe")).toEqual("JD")
})

it("should return the initials of three names", () => {
expect(getInitials("John Adam Doe")).toEqual("JAD")
})

it("should return the initials of four names", () => {
expect(getInitials("John Adam Doe Smith")).toEqual("JADS")
})

it("should return the initials of two names with limit of 1", () => {
expect(getInitials("John Doe", 1)).toEqual("J")
})

it("should return the initials of three names with limit of 2", () => {
expect(getInitials("John Adam Doe", 2)).toEqual("JA")
})

it("should return the initials of four names with limit of 3", () => {
expect(getInitials("John Adam Doe Smith", 3)).toEqual("JAD")
})
})
12 changes: 9 additions & 3 deletions src/helpers/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,20 @@ export function isTruthy<T>(value?: T | undefined | null | false): value is T {
/**
* Get the initials from a string
* @param value A string to get the initials from
* @param limit The maximum number of initials to return
* @returns The initials from the string
*/
export const getInitials = (value?: string | null) => {
export const getInitials = (value?: string | null, limit = 0) => {
if (!value) {
return ""
}

const names = value.trim().split(" ").filter(isTruthy)
const values = value.trim().split(" ").filter(isTruthy)
const initials = values.map((name) => name.charAt(0).toUpperCase()).join("")

return names.map((name) => name.charAt(0).toUpperCase()).join("")
if (limit > 0) {
return initials.slice(0, limit)
}

return initials
}

0 comments on commit 5902b4d

Please sign in to comment.