feat(lowerFirst): add lowerFirst function (#403)

* feat(lowerFirst): Add lowerFirst function

* Add lowerFirst benchmark

* Add lowerFirst english docs

---------

Co-authored-by: Sojin Park <raon0211@toss.im>
This commit is contained in:
mattiacoll 2024-08-26 01:12:57 +12:00 committed by GitHub
parent 3844e7a3c3
commit 02643bcbe3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 93 additions and 0 deletions

View File

@ -0,0 +1,28 @@
import { bench, describe } from 'vitest';
import { lowerFirst as lowerFirstToolkit } from 'es-toolkit';
import { lowerFirst as lowerFirstLodash } from 'lodash';
describe('lowerFirst', () => {
describe('short string', () => {
bench('es-toolkit/lowerFirst', () => {
const str = 'camelCase';
lowerFirstToolkit(str);
});
bench('lodash/lowerFirst', () => {
const str = 'camelCase';
lowerFirstLodash(str);
});
});
describe('long string', () => {
const LONG_STR = 'camelCaseLongString'.repeat(1000);
bench('es-toolkit/lowerFirst', () => {
lowerFirstToolkit(LONG_STR);
});
bench('lodash/lowerFirst', () => {
lowerFirstLodash(LONG_STR);
});
});
});

View File

@ -0,0 +1,26 @@
# lowerFirst
Converts the first character of string to lower case.
## Signature
```typescript
function lowerFirst(str: string): string;
```
### Parameters
- `str` (`string`): The string that is to be changed.
### Returns
(`string`) The string converted.
## Examples
```typescript
import { lowerFirst } from 'es-toolkit/string';
upperCase('Fred') // returns 'fred'
upperCase('FRED') // returns 'fRED'
```

View File

@ -5,3 +5,4 @@ export { lowerCase } from './lowerCase.ts';
export { startCase } from './startCase.ts';
export { capitalize } from './capitalize.ts';
export { pascalCase } from './pascalCase.ts';
export { lowerFirst } from './lowerFirst.ts';

View File

@ -0,0 +1,24 @@
import { describe, it, expect } from 'vitest';
import { lowerFirst } from './lowerFirst';
describe('lowerFirst', () => {
it('should change first character of upper case to lower case', () => {
expect(lowerFirst('Fred')).toEqual('fred');
});
it('should keep first character of lower case unaltered', () => {
expect(lowerFirst('fred')).toEqual('fred');
});
it('should keep all other characters unaltered', () => {
expect(lowerFirst('FRED')).toEqual('fRED');
});
it('should work with an empty string', () => {
expect(lowerFirst('')).toEqual('');
});
it('should keep white space and not alter string', () => {
expect(lowerFirst(' fred')).toEqual(' fred');
});
});

14
src/string/lowerFirst.ts Normal file
View File

@ -0,0 +1,14 @@
/**
* Converts the first character of string to lower case.
*
* @param {string} str - The string that is to be changed
* @returns {string} - The converted string.
*
* @example
* const convertedStr1 = lowerCase('fred') // returns 'fred'
* const convertedStr2 = lowerCase('Fred') // returns 'Fred'
* const convertedStr3 = lowerCase('FRED') // returns 'FRED'
*/
export const lowerFirst = (str: string): string => {
return str.substring(0, 1).toLowerCase() + str.substring(1);
};