mirror of
https://github.com/toss/es-toolkit.git
synced 2024-12-26 01:12:56 +03:00
feat(toLower): Implement toLower
This commit is contained in:
parent
b411e9ea1d
commit
fb236c21bf
36
docs/ja/reference/compat/string/toLower.md
Normal file
36
docs/ja/reference/compat/string/toLower.md
Normal file
@ -0,0 +1,36 @@
|
||||
# toLower
|
||||
|
||||
::: info
|
||||
この関数は互換性のために `es-toolkit/compat` からのみインポートできます。代替可能なネイティブ JavaScript API があるか、まだ十分に最適化されていないためです。
|
||||
|
||||
`es-toolkit/compat` からこの関数をインポートすると、[lodash と完全に同じように動作](../../../compatibility.md)します。
|
||||
:::
|
||||
|
||||
与えられた値を文字列に変換し、小文字に変換します。与えられた入力は最初に文字列に変換されます。
|
||||
|
||||
## インターフェース
|
||||
|
||||
```typescript
|
||||
function toLower(value?: unknown): string;
|
||||
```
|
||||
|
||||
### パラメータ
|
||||
|
||||
- `value` (`unknown`): 変換する値。
|
||||
|
||||
### 戻り値
|
||||
|
||||
(`string`): 小文字に変換された文字列を返します。
|
||||
|
||||
## 例
|
||||
|
||||
```typescript
|
||||
toLower('--FOO-BAR--');
|
||||
// => '--foo-bar--'
|
||||
|
||||
toLower(null);
|
||||
// => ''
|
||||
|
||||
toLower([1, 2, 3]);
|
||||
// => '1,2,3'
|
||||
```
|
36
docs/ko/reference/compat/string/toLower.md
Normal file
36
docs/ko/reference/compat/string/toLower.md
Normal file
@ -0,0 +1,36 @@
|
||||
# toLower
|
||||
|
||||
::: info
|
||||
이 함수는 호환성을 위한 `es-toolkit/compat` 에서만 가져올 수 있어요. 대체할 수 있는 네이티브 JavaScript API가 있거나, 아직 충분히 최적화되지 않았기 때문이에요.
|
||||
|
||||
`es-toolkit/compat`에서 이 함수를 가져오면, [lodash와 완전히 똑같이 동작](../../../compatibility.md)해요.
|
||||
:::
|
||||
|
||||
주어진 값을 문자열로 변환하고 소문자로 변환해요. 주어진 인자는 먼저 문자열로 변환돼서 처리돼요.
|
||||
|
||||
## 인터페이스
|
||||
|
||||
```typescript
|
||||
function toLower(value?: unknown): string;
|
||||
```
|
||||
|
||||
### 파라미터
|
||||
|
||||
- `value` (`unknown`): 변환할 값.
|
||||
|
||||
### 반환 값
|
||||
|
||||
(`string`): 소문자로 변환된 문자열.
|
||||
|
||||
## 예시
|
||||
|
||||
```typescript
|
||||
toLower('--FOO-BAR--');
|
||||
// => '--foo-bar--'
|
||||
|
||||
toLower(null);
|
||||
// => ''
|
||||
|
||||
toLower([1, 2, 3]);
|
||||
// => '1,2,3'
|
||||
```
|
36
docs/reference/compat/string/toLower.md
Normal file
36
docs/reference/compat/string/toLower.md
Normal file
@ -0,0 +1,36 @@
|
||||
# toLower
|
||||
|
||||
::: info
|
||||
This function is only available in `es-toolkit/compat` for compatibility reasons. It either has alternative native JavaScript APIs or isn’t fully optimized yet.
|
||||
|
||||
When imported from `es-toolkit/compat`, it behaves exactly like lodash and provides the same functionalities, as detailed [here](../../../compatibility.md).
|
||||
:::
|
||||
|
||||
Converts the given value to a string and transforms it to lower case. The function can handle various input types by first converting them to strings.
|
||||
|
||||
## Signature
|
||||
|
||||
```typescript
|
||||
function toLower(value?: unknown): string;
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- `value` (`unknown`): The value to convert.
|
||||
|
||||
### Returns
|
||||
|
||||
(`string`): Returns the lower cased string.
|
||||
|
||||
## Examples
|
||||
|
||||
```typescript
|
||||
toLower('--FOO-BAR--');
|
||||
// => '--foo-bar--'
|
||||
|
||||
toLower(null);
|
||||
// => ''
|
||||
|
||||
toLower([1, 2, 3]);
|
||||
// => '1,2,3'
|
||||
```
|
@ -6,7 +6,7 @@ This function is only available in `es-toolkit/compat` for compatibility reasons
|
||||
When imported from `es-toolkit/compat`, it behaves exactly like lodash and provides the same functionalities, as detailed [here](../../../compatibility.md).
|
||||
:::
|
||||
|
||||
Converts the given value to a string and transforms it to uppercase. The function can handle various input types by first converting them to strings.
|
||||
Converts the given value to a string and transforms it to upper case. The function can handle various input types by first converting them to strings.
|
||||
|
||||
## Signature
|
||||
|
||||
@ -16,11 +16,11 @@ function toUpper(value?: unknown): string;
|
||||
|
||||
## Parameters
|
||||
|
||||
- `value`(`unknown`) : The value to convert to uppercase. If omitted, returns an empty string.
|
||||
- `value`(`unknown`) : The value to convert to upper case. If omitted, returns an empty string.
|
||||
|
||||
## Returns
|
||||
|
||||
`string` : The uppercase version of the input value converted to a string.
|
||||
`string` : The upper case version of the input value converted to a string.
|
||||
|
||||
## Examples
|
||||
|
||||
|
36
docs/zh_hans/reference/compat/string/toLower.md
Normal file
36
docs/zh_hans/reference/compat/string/toLower.md
Normal file
@ -0,0 +1,36 @@
|
||||
# toLower
|
||||
|
||||
::: info
|
||||
出于兼容性原因,此函数仅在 `es-toolkit/compat` 中提供。它可能具有替代的原生 JavaScript API,或者尚未完全优化。
|
||||
|
||||
从 `es-toolkit/compat` 导入时,它的行为与 lodash 完全一致,并提供相同的功能,详情请见 [这里](../../../compatibility.md)。
|
||||
:::
|
||||
|
||||
将给定的值转换为字符串并转换为小写。给定的输入首先会被转换为字符串。
|
||||
|
||||
## 签名
|
||||
|
||||
```typescript
|
||||
function toLower(value?: unknown): string;
|
||||
```
|
||||
|
||||
### 参数
|
||||
|
||||
- `value` (`unknown`): 要转换的值。
|
||||
|
||||
### 返回值
|
||||
|
||||
(`string`): 返回小写的字符串.
|
||||
|
||||
## 示例
|
||||
|
||||
```typescript
|
||||
toLower('--FOO-BAR--');
|
||||
// => '--foo-bar--'
|
||||
|
||||
toLower(null);
|
||||
// => ''
|
||||
|
||||
toLower([1, 2, 3]);
|
||||
// => '1,2,3'
|
||||
```
|
@ -176,6 +176,7 @@ export { snakeCase } from './string/snakeCase.ts';
|
||||
export { startCase } from './string/startCase.ts';
|
||||
export { startsWith } from './string/startsWith.ts';
|
||||
export { template, templateSettings } from './string/template.ts';
|
||||
export { toLower } from './string/toLower.ts';
|
||||
export { toUpper } from './string/toUpper.ts';
|
||||
export { trim } from './string/trim.ts';
|
||||
export { trimEnd } from './string/trimEnd.ts';
|
||||
|
116
src/compat/string/toLower.spec.ts
Normal file
116
src/compat/string/toLower.spec.ts
Normal file
@ -0,0 +1,116 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { toLower } from './toLower';
|
||||
|
||||
describe('toLower', () => {
|
||||
it('should convert whole string to lower case', () => {
|
||||
expect(toLower('--Foo-Bar--')).toEqual('--foo-bar--');
|
||||
expect(toLower('fooBar')).toEqual('foobar');
|
||||
expect(toLower('__FOO_BAR__')).toEqual('__foo_bar__');
|
||||
});
|
||||
|
||||
const strings = ['foo bar', 'Foo bar', 'foo Bar', 'Foo Bar', 'FOO BAR', 'fooBar', '--foo-bar--', '__foo_bar__'];
|
||||
|
||||
it('should convert string to upper case while preserving special characters', () => {
|
||||
const actual = strings.map(string => toLower(string));
|
||||
const expected = ['foo bar', 'foo bar', 'foo bar', 'foo bar', 'foo bar', 'foobar', '--foo-bar--', '__foo_bar__'];
|
||||
expect(actual).toEqual(expected);
|
||||
});
|
||||
|
||||
it('should handle double-converting strings', () => {
|
||||
const actual = strings.map(string => toLower(toLower(string)));
|
||||
const expected = strings.map(string => toLower(string));
|
||||
expect(actual).toEqual(expected);
|
||||
});
|
||||
|
||||
it('should preserve contractions with apostrophes', () => {
|
||||
const postfixes = ['d', 'll', 'm', 're', 's', 't', 've'];
|
||||
["'", '\u2019'].forEach(apos => {
|
||||
const actual = postfixes.map(postfix => toLower(`a b${apos}${postfix} c`));
|
||||
const expected = postfixes.map(postfix => `A B${apos}${postfix.toLowerCase()} C`);
|
||||
expect(actual).toEqual(expected);
|
||||
});
|
||||
});
|
||||
|
||||
it('should preserve spaces and special characters', () => {
|
||||
expect(toLower('hello world')).toBe('HELLO WORLD');
|
||||
expect(toLower('!@#$hello%^&*')).toBe('!@#$HELLO%^&*');
|
||||
expect(toLower('tabs\tand\nnewlines')).toBe('TABS\tAND\nNEWLINES');
|
||||
});
|
||||
|
||||
it('should handle unicode characters', () => {
|
||||
expect(toLower('café')).toBe('CAFÉ');
|
||||
expect(toLower('über')).toBe('ÜBER');
|
||||
expect(toLower('señor')).toBe('SEÑOR');
|
||||
});
|
||||
|
||||
it('should preserve Latin mathematical operators', () => {
|
||||
expect(toLower('\xd7')).toBe('\xd7');
|
||||
expect(toLower('\xf7')).toBe('\xf7');
|
||||
});
|
||||
|
||||
it('should handle null and undefined', () => {
|
||||
expect(toLower(null)).toBe('');
|
||||
expect(toLower(undefined)).toBe('');
|
||||
});
|
||||
|
||||
it('should handle numbers including special cases', () => {
|
||||
expect(toLower(123)).toBe('123');
|
||||
expect(toLower(-0)).toBe('-0');
|
||||
expect(toLower(0)).toBe('0');
|
||||
expect(toLower(Infinity)).toBe('INFINITY');
|
||||
expect(toLower(NaN)).toBe('NAN');
|
||||
});
|
||||
|
||||
it('should handle arrays', () => {
|
||||
expect(toLower([1, 2, 3])).toBe('1,2,3');
|
||||
expect(toLower(['a', 'b', 'c'])).toBe('A,B,C');
|
||||
expect(toLower([1, 'b', -0])).toBe('1,B,-0');
|
||||
expect(toLower([])).toBe('');
|
||||
});
|
||||
|
||||
it('should handle nested arrays', () => {
|
||||
expect(toLower([1, [2, 3], 4])).toBe('1,2,3,4');
|
||||
expect(toLower([[['a']]])).toBe('A');
|
||||
});
|
||||
|
||||
it('should handle symbols', () => {
|
||||
const sym1 = Symbol('test');
|
||||
const sym2 = Symbol('');
|
||||
expect(toLower(sym1)).toBe('SYMBOL(TEST)');
|
||||
expect(toLower(sym2)).toBe('SYMBOL()');
|
||||
expect(toLower([Symbol('a'), Symbol('b')])).toBe('SYMBOL(A),SYMBOL(B)');
|
||||
});
|
||||
|
||||
it('should handle objects', () => {
|
||||
const obj = { toString: () => 'custom' };
|
||||
expect(toLower(obj)).toBe('CUSTOM');
|
||||
expect(toLower({})).toBe('[OBJECT OBJECT]');
|
||||
});
|
||||
|
||||
it('should handle mixed types in arrays', () => {
|
||||
const sym = Symbol('test');
|
||||
expect(toLower([1, 'b', sym, null, undefined])).toBe('1,B,SYMBOL(TEST),,');
|
||||
});
|
||||
|
||||
it('should maintain proper TypeScript types', () => {
|
||||
const result1: string = toLower('test');
|
||||
const result2: string = toLower(123);
|
||||
const result3: string = toLower(null);
|
||||
const result4: string = toLower(undefined);
|
||||
|
||||
expect(typeof result1).toBe('string');
|
||||
expect(typeof result2).toBe('string');
|
||||
expect(typeof result3).toBe('string');
|
||||
expect(typeof result4).toBe('string');
|
||||
});
|
||||
|
||||
it('should handle empty strings', () => {
|
||||
expect(toLower('')).toBe('');
|
||||
});
|
||||
|
||||
it('should handle whitespace strings', () => {
|
||||
expect(toLower(' ')).toBe(' ');
|
||||
expect(toLower('\t')).toBe('\t');
|
||||
expect(toLower('\n')).toBe('\n');
|
||||
});
|
||||
});
|
22
src/compat/string/toLower.ts
Normal file
22
src/compat/string/toLower.ts
Normal file
@ -0,0 +1,22 @@
|
||||
import { toString } from '../util/toString.ts';
|
||||
|
||||
/**
|
||||
* Converts the given value to a string and transforms it to lower case.
|
||||
* The function can handle various input types by first converting them to strings.
|
||||
*
|
||||
* @param {unknown} [value=''] The value to convert.
|
||||
* @returns {string} Returns the lower cased string.
|
||||
* @example
|
||||
*
|
||||
* toLower('--FOO-BAR--');
|
||||
* // => '--foo-bar--'
|
||||
*
|
||||
* toLower(null);
|
||||
* // => ''
|
||||
*
|
||||
* toLower([1, 2, 3]);
|
||||
* // => '1,2,3'
|
||||
*/
|
||||
export function toLower(value?: unknown): string {
|
||||
return toString(value).toLowerCase();
|
||||
}
|
Loading…
Reference in New Issue
Block a user