diff --git a/packages/utils/src/string/string.utils.test.ts b/packages/utils/src/string/string.utils.test.ts index ead94a1..ad93dfe 100644 --- a/packages/utils/src/string/string.utils.test.ts +++ b/packages/utils/src/string/string.utils.test.ts @@ -71,16 +71,16 @@ describe('StringUtils', () => { describe('Capitalization', () => { it('should capitalize first letter only', () => { - expect(StringUtils.of('hELLO world').capitalizeFirst().value()).toBe('Hello world'); + expect(StringUtils.of('hELLO world').sentenceCase().value()).toBe('Hello world'); }); it('should capitalize each word', () => { - expect(StringUtils.of('hello world').capitalizeEachWord().value()).toBe('Hello World'); + expect(StringUtils.of('hello world').capitalizeCase().value()).toBe('Hello World'); }); - it('should handle double spaces in capitalizeEachWord', () => { + it('should handle double spaces in capitalizeCase', () => { // Test regex logic for splitting words - expect(StringUtils.of('hello world').capitalizeEachWord().value()).toBe('Hello World'); + expect(StringUtils.of('hello world').capitalizeCase().value()).toBe('Hello World'); }); }); diff --git a/packages/utils/src/string/string.utils.ts b/packages/utils/src/string/string.utils.ts index 277861d..cfb8211 100644 --- a/packages/utils/src/string/string.utils.ts +++ b/packages/utils/src/string/string.utils.ts @@ -14,7 +14,7 @@ * // Basic Chaining * StringUtils.of(" hello world ") * .trim() - * .capitalizeEachWord() + * .capitalizeCase() * .value(); // "Hello World" * ------------------------------------------------------------ */ @@ -38,8 +38,8 @@ interface IStringUtils { // --- Manipulations (Chainable) --- upperCase(): IStringUtils; lowerCase(): IStringUtils; - capitalizeFirst(): IStringUtils; - capitalizeEachWord(): IStringUtils; + capitalizeCase(): IStringUtils; + sentenceCase(): IStringUtils; limit(maxLength: number, suffix?: string): IStringUtils; trim(): IStringUtils; slugify(): IStringUtils; @@ -125,28 +125,15 @@ export class StringUtils implements IStringUtils { return new StringUtils(this._value.toLowerCase()); } - /** - * Capitalizes only the first letter of the entire string. - * Remainder is forced to lowercase (Sentence case). - * - * @example - * StringUtils.of("HELLO world").capitalizeFirst().value() // "Hello world" - */ - capitalizeFirst(): StringUtils { - if (!this._value) return this; - const lower = this._value.toLowerCase(); - return new StringUtils(lower.charAt(0).toUpperCase() + lower.slice(1)); - } - /** * Capitalizes the first letter of every word (Title Case). * Automatically handles multiple spaces. * * @example - * StringUtils.of("hello world").capitalizeEachWord().value() // "Hello World" - * StringUtils.of("hello world").capitalizeEachWord().value() // "Hello World" + * StringUtils.of("hello world").capitalizeCase().value() // "Hello World" + * StringUtils.of("hello world").capitalizeCase().value() // "Hello World" */ - capitalizeEachWord(): StringUtils { + capitalizeCase(): StringUtils { if (!this._value) return this; const transformed = this._value .toLowerCase() @@ -156,6 +143,19 @@ export class StringUtils implements IStringUtils { return new StringUtils(transformed); } + /** + * Capitalizes only the first letter of the entire string. + * Remainder is forced to lowercase (Sentence case). + * + * @example + * StringUtils.of("HELLO world").sentenceCase().value() // "Hello world" + */ + sentenceCase(): StringUtils { + if (!this._value) return this; + const lower = this._value.toLowerCase(); + return new StringUtils(lower.charAt(0).toUpperCase() + lower.slice(1)); + } + /** * Truncates string to a specific length and adds a suffix. *