Skip to content

Commit

Permalink
feat(compat): implement upperFirst/lowerFirst
Browse files Browse the repository at this point in the history
  • Loading branch information
D-Sketon committed Dec 7, 2024
1 parent 995be54 commit 2ecc93b
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 0 deletions.
11 changes: 11 additions & 0 deletions benchmarks/performance/lowerFirst.bench.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { bench, describe } from 'vitest';
import { lowerFirst as lowerFirstToolkit_ } from 'es-toolkit';
import { lowerFirst as lowerFirstCompatToolkit_ } from 'es-toolkit/compat';
import { lowerFirst as lowerFirstLodash_ } from 'lodash';

const lowerFirstToolkit = lowerFirstToolkit_;
const lowerFirstCompatToolkit = lowerFirstCompatToolkit_;
const lowerFirstLodash = lowerFirstLodash_;

describe('lowerFirst', () => {
Expand All @@ -12,6 +14,11 @@ describe('lowerFirst', () => {
lowerFirstToolkit(str);
});

bench('es-toolkit/compat/lowerFirst', () => {
const str = 'camelCase';
lowerFirstCompatToolkit(str);
});

bench('lodash/lowerFirst', () => {
const str = 'camelCase';
lowerFirstLodash(str);
Expand All @@ -24,6 +31,10 @@ describe('lowerFirst', () => {
lowerFirstToolkit(LONG_STR);
});

bench('es-toolkit/compat/lowerFirst', () => {
lowerFirstCompatToolkit(LONG_STR);
});

bench('lodash/lowerFirst', () => {
lowerFirstLodash(LONG_STR);
});
Expand Down
11 changes: 11 additions & 0 deletions benchmarks/performance/upperFirst.bench.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { bench, describe } from 'vitest';
import { upperFirst as upperFirstToolkit_ } from 'es-toolkit';
import { upperFirst as upperFirstCompatToolkit_ } from 'es-toolkit/compat';
import { upperFirst as upperFirstLodash_ } from 'lodash';

const upperFirstToolkit = upperFirstToolkit_;
const upperFirstCompatToolkit = upperFirstCompatToolkit_;
const upperFirstLodash = upperFirstLodash_;

describe('upperFirst', () => {
Expand All @@ -12,6 +14,11 @@ describe('upperFirst', () => {
upperFirstToolkit(str);
});

bench('es-toolkit/compat/upperFirst', () => {
const str = 'camelCase';
upperFirstCompatToolkit(str);
});

bench('lodash/upperFirst', () => {
const str = 'camelCase';
upperFirstLodash(str);
Expand All @@ -24,6 +31,10 @@ describe('upperFirst', () => {
upperFirstToolkit(LONG_STR);
});

bench('es-toolkit/compat/upperFirst', () => {
upperFirstCompatToolkit(LONG_STR);
});

bench('lodash/upperFirst', () => {
upperFirstLodash(LONG_STR);
});
Expand Down
2 changes: 2 additions & 0 deletions src/compat/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ export { endsWith } from './string/endsWith.ts';
export { escape } from './string/escape.ts';
export { kebabCase } from './string/kebabCase.ts';
export { lowerCase } from './string/lowerCase.ts';
export { lowerFirst } from './string/lowerFirst.ts';
export { pad } from './string/pad.ts';
export { padEnd } from './string/padEnd.ts';
export { padStart } from './string/padStart.ts';
Expand All @@ -181,6 +182,7 @@ export { trim } from './string/trim.ts';
export { trimEnd } from './string/trimEnd.ts';
export { trimStart } from './string/trimStart.ts';
export { upperCase } from './string/upperCase.ts';
export { upperFirst } from './string/upperFirst.ts';
export { words } from './string/words.ts';

export { constant } from './util/constant.ts';
Expand Down
22 changes: 22 additions & 0 deletions src/compat/string/lowerFirst.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { describe, expect, it } from 'vitest';
import { lowerFirst } from './lowerFirst';
import { map } from '../array/map';
import { stubString } from '../util/stubString';

describe('lowerFirst', () => {
it('should lowercase only the first character', () => {
expect(lowerFirst('fred')).toBe('fred');
expect(lowerFirst('Fred')).toBe('fred');
expect(lowerFirst('FRED')).toBe('fRED');
});

it('should return an empty string for empty values', () => {
// eslint-disable-next-line no-sparse-arrays
const values = [, null, undefined, ''];
const expected = map(values, stubString);

const actual = map(values, (value, index) => (index ? lowerFirst(value as any) : lowerFirst()));

expect(actual).toEqual(expected);
});
});
17 changes: 17 additions & 0 deletions src/compat/string/lowerFirst.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { lowerFirst as lowerFirstToolkit } from '../../string/lowerFirst.ts';
import { toString } from '../util/toString.ts';

/**
* 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 function lowerFirst(str?: string): string {
return lowerFirstToolkit(toString(str));
}
22 changes: 22 additions & 0 deletions src/compat/string/upperFirst.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { describe, expect, it } from 'vitest';
import { upperFirst } from './upperFirst';
import { map } from '../array/map';
import { stubString } from '../util/stubString';

describe('upperFirst', () => {
it('should uppercase only the first character', () => {
expect(upperFirst('fred')).toBe('Fred');
expect(upperFirst('Fred')).toBe('Fred');
expect(upperFirst('FRED')).toBe('FRED');
});

it('should return an empty string for empty values', () => {
// eslint-disable-next-line no-sparse-arrays
const values = [, null, undefined, ''];
const expected = map(values, stubString);

const actual = map(values, (value, index) => (index ? upperFirst(value as any) : upperFirst()));

expect(actual).toEqual(expected);
});
});
17 changes: 17 additions & 0 deletions src/compat/string/upperFirst.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { upperFirst as upperFirstToolkit } from '../../string/upperFirst.ts';
import { toString } from '../util/toString.ts';

/**
* Converts the first character of string to upper case.
*
* @param {string} str - The string that is to be changed
* @returns {string} - The converted string.
*
* @example
* const convertedStr1 = upperFirst('fred') // returns 'Fred'
* const convertedStr2 = upperFirst('Fred') // returns 'Fred'
* const convertedStr3 = upperFirst('FRED') // returns 'FRED'
*/
export function upperFirst(str?: string): string {
return upperFirstToolkit(toString(str));
}

0 comments on commit 2ecc93b

Please sign in to comment.