feat: rename capitalization methods for clarity and consistency in StringUtils

This commit is contained in:
Firman Ramdhani
2026-02-02 09:51:35 +07:00
parent 562d32a506
commit a4107e81be
2 changed files with 23 additions and 23 deletions
@@ -71,16 +71,16 @@ describe('StringUtils', () => {
describe('Capitalization', () => { describe('Capitalization', () => {
it('should capitalize first letter only', () => { 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', () => { 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 // 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');
}); });
}); });
+19 -19
View File
@@ -14,7 +14,7 @@
* // Basic Chaining * // Basic Chaining
* StringUtils.of(" hello world ") * StringUtils.of(" hello world ")
* .trim() * .trim()
* .capitalizeEachWord() * .capitalizeCase()
* .value(); // "Hello World" * .value(); // "Hello World"
* ------------------------------------------------------------ * ------------------------------------------------------------
*/ */
@@ -38,8 +38,8 @@ interface IStringUtils {
// --- Manipulations (Chainable) --- // --- Manipulations (Chainable) ---
upperCase(): IStringUtils; upperCase(): IStringUtils;
lowerCase(): IStringUtils; lowerCase(): IStringUtils;
capitalizeFirst(): IStringUtils; capitalizeCase(): IStringUtils;
capitalizeEachWord(): IStringUtils; sentenceCase(): IStringUtils;
limit(maxLength: number, suffix?: string): IStringUtils; limit(maxLength: number, suffix?: string): IStringUtils;
trim(): IStringUtils; trim(): IStringUtils;
slugify(): IStringUtils; slugify(): IStringUtils;
@@ -125,28 +125,15 @@ export class StringUtils implements IStringUtils {
return new StringUtils(this._value.toLowerCase()); 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). * Capitalizes the first letter of every word (Title Case).
* Automatically handles multiple spaces. * Automatically handles multiple spaces.
* *
* @example * @example
* StringUtils.of("hello world").capitalizeEachWord().value() // "Hello World" * StringUtils.of("hello world").capitalizeCase().value() // "Hello World"
* StringUtils.of("hello world").capitalizeEachWord().value() // "Hello World" * StringUtils.of("hello world").capitalizeCase().value() // "Hello World"
*/ */
capitalizeEachWord(): StringUtils { capitalizeCase(): StringUtils {
if (!this._value) return this; if (!this._value) return this;
const transformed = this._value const transformed = this._value
.toLowerCase() .toLowerCase()
@@ -156,6 +143,19 @@ export class StringUtils implements IStringUtils {
return new StringUtils(transformed); 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. * Truncates string to a specific length and adds a suffix.
* *