-
-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a7e1969
commit 5b8476f
Showing
2 changed files
with
29 additions
and
0 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/** | ||
* Takes an array of string parts and returns a new string of each part's | ||
* initial character. The `mode` arg can be used to customize result casing | ||
* (default: uppercase). If `mode` is null, the original casing will be kept. | ||
* | ||
* @example | ||
* ```ts | ||
* initials(["alicia", "bella", "carerra"]); | ||
* // "ABC" | ||
* | ||
* initials("shader-ast-GLSL".split("-")) | ||
* // "SAG" | ||
* | ||
* initials("Ludwig van Beethoven".split(" "), null) | ||
* // "LvB" | ||
* ``` | ||
* | ||
* @param parts | ||
* @param mode | ||
*/ | ||
export const initials = (parts: string[], mode: "u" | "l" | null = "u") => { | ||
const res = parts.map((x) => x[0]).join(""); | ||
return mode === "u" | ||
? res.toUpperCase() | ||
: mode === "l" | ||
? res.toLowerCase() | ||
: res; | ||
}; |