Skip to content

Commit

Permalink
feat(strings): add initials()
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Sep 9, 2021
1 parent a7e1969 commit 5b8476f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/strings/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export * from "./float";
export * from "./format";
export * from "./groups";
export * from "./hollerith";
export * from "./initials";
export * from "./int";
export * from "./interpolate";
export * from "./join";
Expand Down
28 changes: 28 additions & 0 deletions packages/strings/src/initials.ts
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;
};

0 comments on commit 5b8476f

Please sign in to comment.