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', () => {
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');
});
});
+19 -19
View File
@@ -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.
*