mirror of
https://github.com/toss/es-toolkit.git
synced 2024-11-24 11:45:26 +03:00
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:
parent
3844e7a3c3
commit
02643bcbe3
28
benchmarks/performance/lowerFirst.bench.ts
Normal file
28
benchmarks/performance/lowerFirst.bench.ts
Normal 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);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
26
docs/reference/string/lowerFirst.md
Normal file
26
docs/reference/string/lowerFirst.md
Normal 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'
|
||||||
|
```
|
@ -5,3 +5,4 @@ export { lowerCase } from './lowerCase.ts';
|
|||||||
export { startCase } from './startCase.ts';
|
export { startCase } from './startCase.ts';
|
||||||
export { capitalize } from './capitalize.ts';
|
export { capitalize } from './capitalize.ts';
|
||||||
export { pascalCase } from './pascalCase.ts';
|
export { pascalCase } from './pascalCase.ts';
|
||||||
|
export { lowerFirst } from './lowerFirst.ts';
|
||||||
|
24
src/string/lowerFirst.spec.ts
Normal file
24
src/string/lowerFirst.spec.ts
Normal 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
14
src/string/lowerFirst.ts
Normal 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);
|
||||||
|
};
|
Loading…
Reference in New Issue
Block a user