fix(startCase): convert the non-first char to lowercase (#591)
Some checks failed
CI / codecov (push) Has been cancelled
Release / release (push) Has been cancelled

* fix: convert the non-first char to lowercase

* test: more test case

* docs: update examples
This commit is contained in:
hyesung oh 2024-09-24 10:39:37 +09:00 committed by GitHub
parent c734f730b4
commit 1e04c48d3d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 24 additions and 22 deletions

View File

@ -25,8 +25,8 @@ import { startCase } from 'es-toolkit/string';
startCase('--foo-bar--'); // 'Foo Bar' を返します
startCase('fooBar'); // 'Foo Bar' を返します
startCase('__FOO_BAR__'); // 'FOO BAR' を返します
startCase('XMLHttpRequest'); // 'XML Http Request' を返します
startCase('__FOO_BAR__'); // 'Foo Bar' を返します
startCase('XMLHttpRequest'); // 'Xml Http Request' を返します
startCase('_abc_123_def'); // 'Abc 123 Def' を返します
startCase('__abc__123__def__'); // 'Abc 123 Def' を返します
startCase('_-_-_-_'); // '' を返します

View File

@ -25,8 +25,8 @@ import { startCase } from 'es-toolkit/string';
startCase('--foo-bar--'); // returns 'Foo Bar'
startCase('fooBar'); // returns 'Foo Bar'
startCase('__FOO_BAR__'); // returns 'FOO BAR'
startCase('XMLHttpRequest'); // returns 'XML Http Request'
startCase('__FOO_BAR__'); // returns 'Foo Bar'
startCase('XMLHttpRequest'); // returns 'Xml Http Request'
startCase('_abc_123_def'); // returns 'Abc 123 Def'
startCase('__abc__123__def__'); // returns 'Abc 123 Def'
startCase('_-_-_-_'); // returns ''

View File

@ -25,8 +25,8 @@ import { startCase } from 'es-toolkit/string';
startCase('--foo-bar--'); // returns 'Foo Bar'
startCase('fooBar'); // returns 'Foo Bar'
startCase('__FOO_BAR__'); // returns 'FOO BAR'
startCase('XMLHttpRequest'); // returns 'XML Http Request'
startCase('__FOO_BAR__'); // returns 'Foo Bar'
startCase('XMLHttpRequest'); // returns 'Xml Http Request'
startCase('_abc_123_def'); // returns 'Abc 123 Def'
startCase('__abc__123__def__'); // returns 'Abc 123 Def'
startCase('_-_-_-_'); // returns ''

View File

@ -25,8 +25,8 @@ import { startCase } from 'es-toolkit/string';
startCase('--foo-bar--'); // returns 'Foo Bar'
startCase('fooBar'); // returns 'Foo Bar'
startCase('__FOO_BAR__'); // returns 'FOO BAR'
startCase('XMLHttpRequest'); // returns 'XML Http Request'
startCase('__FOO_BAR__'); // returns 'Foo Bar'
startCase('XMLHttpRequest'); // returns 'Xml Http Request'
startCase('_abc_123_def'); // returns 'Abc 123 Def'
startCase('__abc__123__def__'); // returns 'Abc 123 Def'
startCase('_-_-_-_'); // returns ''

View File

@ -5,20 +5,20 @@ describe('startCase', function () {
it('should capitalize each word', function () {
expect(startCase('--foo-bar--')).toBe('Foo Bar');
expect(startCase('fooBar')).toBe('Foo Bar');
expect(startCase('__FOO_BAR__')).toBe('FOO BAR');
expect(startCase('__FOO_BAR__')).toBe('Foo Bar');
});
it('should handle compound words', function () {
expect(startCase('createElement')).toBe('Create Element');
expect(startCase('iPhone')).toBe('I Phone');
expect(startCase('XMLHttpRequest')).toBe('XML Http Request');
expect(startCase('XMLHttpRequest')).toBe('Xml Http Request');
});
it('should handle various delimiters', function () {
expect(startCase('_abc_123_def')).toBe('Abc 123 Def');
expect(startCase('__abc__123__def__')).toBe('Abc 123 Def');
expect(startCase('ABC-DEF')).toBe('ABC DEF');
expect(startCase('ABC DEF')).toBe('ABC DEF');
expect(startCase('ABC-DEF')).toBe('Abc Def');
expect(startCase('ABC DEF')).toBe('Abc Def');
});
it('should handle empty strings', function () {
@ -30,16 +30,16 @@ describe('startCase', function () {
});
it('should work with numbers', function () {
expect(startCase('12abc 12ABC')).toBe('12 Abc 12 ABC');
expect(startCase('12abc 12ABC')).toBe('12 Abc 12 Abc');
});
it('should handle consecutive uppercase letters', function () {
expect(startCase('ABC')).toBe('ABC');
expect(startCase('ABCdef')).toBe('AB Cdef');
expect(startCase('ABC')).toBe('Abc');
expect(startCase('ABCdef')).toBe('Ab Cdef');
});
it('should handle combinations of numbers and letters', function () {
expect(startCase('123ABC')).toBe('123 ABC');
expect(startCase('123ABC')).toBe('123 Abc');
expect(startCase('a1B2c3')).toBe('A 1 B 2 C 3');
});
@ -58,4 +58,9 @@ describe('startCase', function () {
expect(startCase(' foo bar ')).toBe('Foo Bar');
expect(startCase('\tfoo\nbar')).toBe('Foo Bar');
});
it('should convert the non-first characters to lowercase', function () {
expect(startCase('FOO BAR')).toBe('Foo Bar');
expect(startCase('FOO BAR BAZ')).toBe('Foo Bar Baz');
});
});

View File

@ -9,7 +9,7 @@ import { getWords } from './_internal/getWords.ts';
*
* @example
* const result1 = startCase('hello world'); // result will be 'Hello World'
* const result2 = startCase('HELLO WORLD'); // result will be 'HELLO WORLD'
* const result2 = startCase('HELLO WORLD'); // result will be 'Hello World'
* const result3 = startCase('hello-world'); // result will be 'Hello World'
* const result4 = startCase('hello_world'); // result will be 'Hello World'
*/
@ -21,11 +21,8 @@ export function startCase(str: string): string {
if (result) {
result += ' ';
}
if (word === word.toUpperCase()) {
result += word;
} else {
result += word[0].toUpperCase() + word.slice(1).toLowerCase();
}
result += word[0].toUpperCase() + word.slice(1).toLowerCase();
}
return result;
}