mirror of
https://github.com/toss/es-toolkit.git
synced 2024-11-28 03:34:26 +03:00
fix(startCase): convert the non-first char to lowercase (#591)
* fix: convert the non-first char to lowercase * test: more test case * docs: update examples
This commit is contained in:
parent
c734f730b4
commit
1e04c48d3d
@ -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('_-_-_-_'); // '' を返します
|
||||
|
@ -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 ''
|
||||
|
@ -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 ''
|
||||
|
@ -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 ''
|
||||
|
@ -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');
|
||||
});
|
||||
});
|
||||
|
@ -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;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user