diff --git a/benchmarks/performance/lowerFirst.bench.ts b/benchmarks/performance/lowerFirst.bench.ts new file mode 100644 index 00000000..5c276fb1 --- /dev/null +++ b/benchmarks/performance/lowerFirst.bench.ts @@ -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); + }); + }); +}); diff --git a/docs/reference/string/lowerFirst.md b/docs/reference/string/lowerFirst.md new file mode 100644 index 00000000..a880abac --- /dev/null +++ b/docs/reference/string/lowerFirst.md @@ -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' +``` \ No newline at end of file diff --git a/src/string/index.ts b/src/string/index.ts index 20770d06..2201a24c 100644 --- a/src/string/index.ts +++ b/src/string/index.ts @@ -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'; diff --git a/src/string/lowerFirst.spec.ts b/src/string/lowerFirst.spec.ts new file mode 100644 index 00000000..9fb71e82 --- /dev/null +++ b/src/string/lowerFirst.spec.ts @@ -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'); + }); +}); diff --git a/src/string/lowerFirst.ts b/src/string/lowerFirst.ts new file mode 100644 index 00000000..951b8baf --- /dev/null +++ b/src/string/lowerFirst.ts @@ -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); +};