diff --git a/CHANGELOG.md b/CHANGELOG.md index dde3275..e153241 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added + +- `uncapitalize` string utils + ## [1.3.1] - 2025-06-06 ### Fixed diff --git a/src/lib/string.spec.ts b/src/lib/string.spec.ts index eae00af..3aebb83 100644 --- a/src/lib/string.spec.ts +++ b/src/lib/string.spec.ts @@ -1,4 +1,4 @@ -import { isNullOrEmpty, isNullOrWhitespace, capitalize } from "./string"; +import { isNullOrEmpty, isNullOrWhitespace, capitalize, uncapitalize } from "./string"; describe("string tests", () => { test.each([ @@ -72,4 +72,16 @@ describe("string tests", () => { ])("capitalize", (value, expected) => { expect(capitalize(value)).toBe(expected); }); + + test.each([ + [null as unknown as string, null], + [undefined as unknown as string, undefined], + ["", ""], + ["A", "a"], + ["Hello", "hello"], + ["Hello world", "hello world"], + ["hello world", "hello world"], + ])("uncapitalize", (value, expected) => { + expect(uncapitalize(value)).toBe(expected); + }); }); diff --git a/src/lib/string.ts b/src/lib/string.ts index c47392d..0acba9c 100644 --- a/src/lib/string.ts +++ b/src/lib/string.ts @@ -32,3 +32,16 @@ export function capitalize(value?: string): string | undefined { return value.charAt(0).toUpperCase() + value.slice(1); } + +/** + * Uncapitalize the string + * @param value The string to uncapitalize + * @returns The uncapitalized string + */ +export function uncapitalize(value?: string): string | undefined { + if (!value || isNullOrWhitespace(value)) { + return value; + } + + return value.charAt(0).toLowerCase() + value.slice(1); +}