diff --git a/packages/strings/src/index.ts b/packages/strings/src/index.ts index 69db1bf3db..3e8c3d8ad1 100644 --- a/packages/strings/src/index.ts +++ b/packages/strings/src/index.ts @@ -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"; diff --git a/packages/strings/src/initials.ts b/packages/strings/src/initials.ts new file mode 100644 index 0000000000..a3a91cb97e --- /dev/null +++ b/packages/strings/src/initials.ts @@ -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; +};