mirror of
https://github.com/toss/es-toolkit.git
synced 2024-11-24 03:32:58 +03:00
feat(padStart): Move padStart to our compat library
This commit is contained in:
parent
4bb457ad01
commit
17d8afa2a3
@ -163,7 +163,7 @@ function sidebar(): DefaultTheme.Sidebar {
|
||||
items: [
|
||||
{ text: 'isArguments (compat)', link: '/reference/compat/predicate/isArguments' },
|
||||
{ text: 'isArray (compat)', link: '/reference/compat/predicate/isArray' },
|
||||
{ text: 'isArrayLike (compat)', link: '/reference/predicate/isArrayLike' },
|
||||
{ text: 'isArrayLike (compat)', link: '/reference/compat/predicate/isArrayLike' },
|
||||
{ text: 'isBoolean', link: '/reference/predicate/isBoolean' },
|
||||
{ text: 'isEqual', link: '/reference/predicate/isEqual' },
|
||||
{ text: 'isFunction', link: '/reference/predicate/isFunction' },
|
||||
@ -175,7 +175,7 @@ function sidebar(): DefaultTheme.Sidebar {
|
||||
{ text: 'isNil', link: '/reference/predicate/isNil' },
|
||||
{ text: 'isNotNil', link: '/reference/predicate/isNotNil' },
|
||||
{ text: 'isNull', link: '/reference/predicate/isNull' },
|
||||
{ text: 'isObjectLike (compat)', link: '/reference/predicate/isObjectLike' },
|
||||
{ text: 'isObjectLike (compat)', link: '/reference/compat/predicate/isObjectLike' },
|
||||
{ text: 'isTypedArray', link: '/reference/predicate/isTypedArray' },
|
||||
{ text: 'isUndefined', link: '/reference/predicate/isUndefined' },
|
||||
],
|
||||
@ -199,7 +199,7 @@ function sidebar(): DefaultTheme.Sidebar {
|
||||
{ text: 'capitalize', link: '/reference/string/capitalize' },
|
||||
{ text: 'startsWith (compat)', link: '/reference/compat/string/startsWith' },
|
||||
{ text: 'endsWith (compat)', link: '/reference/compat/string/endsWith' },
|
||||
{ text: 'padStart', link: '/reference/string/padStart' },
|
||||
{ text: 'padStart (compat)', link: '/reference/compat/string/padStart' },
|
||||
],
|
||||
},
|
||||
],
|
||||
|
@ -172,9 +172,9 @@ function sidebar(): DefaultTheme.Sidebar {
|
||||
{
|
||||
text: '타입 가드',
|
||||
items: [
|
||||
{ text: 'isArguments (호환성)', link: '/ko/reference/predicate/isArguments' },
|
||||
{ text: 'isArguments (호환성)', link: '/ko/reference/compat/predicate/isArguments' },
|
||||
{ text: 'isArray (호환성)', link: '/ko/reference/compat/predicate/isArray' },
|
||||
{ text: 'isArrayLike (호환성)', link: '/ko/reference/predicate/isArrayLike' },
|
||||
{ text: 'isArrayLike (호환성)', link: '/ko/reference/compat/predicate/isArrayLike' },
|
||||
{ text: 'isBoolean', link: '/reference/predicate/isBoolean' },
|
||||
{ text: 'isEqual', link: '/ko/reference/predicate/isEqual' },
|
||||
{ text: 'isFunction', link: '/ko/reference/predicate/isFunction' },
|
||||
@ -186,7 +186,7 @@ function sidebar(): DefaultTheme.Sidebar {
|
||||
{ text: 'isNil', link: '/ko/reference/predicate/isNil' },
|
||||
{ text: 'isNotNil', link: '/ko/reference/predicate/isNotNil' },
|
||||
{ text: 'isNull', link: '/ko/reference/predicate/isNull' },
|
||||
{ text: 'isObjectLike (호환성)', link: '/ko/reference/predicate/isObjectLike' },
|
||||
{ text: 'isObjectLike (호환성)', link: '/ko/reference/compat/predicate/isObjectLike' },
|
||||
{ text: 'isTypedArray', link: '/ko/reference/predicate/isNull' },
|
||||
{
|
||||
text: 'isUndefined',
|
||||
@ -213,6 +213,7 @@ function sidebar(): DefaultTheme.Sidebar {
|
||||
{ text: 'capitalize', link: '/ko/reference/string/capitalize' },
|
||||
{ text: 'startsWith (호환성)', link: '/ko/reference/compat/string/startsWith' },
|
||||
{ text: 'endsWith (호환성)', link: '/ko/reference/compat/string/endsWith' },
|
||||
{ text: 'padStart (호환성)', link: '/ko/reference/compat/string/padStart' },
|
||||
],
|
||||
},
|
||||
],
|
||||
|
@ -156,7 +156,7 @@ function sidebar(): DefaultTheme.Sidebar {
|
||||
{
|
||||
text: '谓词',
|
||||
items: [
|
||||
{ text: 'isArguments (兼容性)', link: '/zh_hans/reference/predicate/isArguments' },
|
||||
{ text: 'isArguments (兼容性)', link: '/zh_hans/reference/compat/predicate/isArguments' },
|
||||
{ text: 'isArray (兼容性)', link: '/zh_hans/reference/compat/predicate/isArray' },
|
||||
{ text: 'isArrayLike (兼容性)', link: '/zh_hans/reference/predicate/isArrayLike' },
|
||||
{ text: 'isEqual', link: '/zh_hans/reference/predicate/isEqual' },
|
||||
@ -169,7 +169,7 @@ function sidebar(): DefaultTheme.Sidebar {
|
||||
{ text: 'isNil', link: '/zh_hans/reference/predicate/isNil' },
|
||||
{ text: 'isNotNil', link: '/zh_hans/reference/predicate/isNotNil' },
|
||||
{ text: 'isNull', link: '/zh_hans/reference/predicate/isNull' },
|
||||
{ text: 'isObjectLike (兼容性)', link: '/zh_hans/reference/predicate/isObjectLike' },
|
||||
{ text: 'isObjectLike (兼容性)', link: '/zh_hans/reference/compat/predicate/isObjectLike' },
|
||||
{ text: 'isTypedArray', link: '/zh_hans/reference/predicate/isTypedArray' },
|
||||
{ text: 'isUndefined', link: '/zh_hans/reference/predicate/isUndefined' },
|
||||
],
|
||||
@ -192,6 +192,7 @@ function sidebar(): DefaultTheme.Sidebar {
|
||||
{ text: 'capitalize', link: '/zh_hans/reference/string/capitalize' },
|
||||
{ text: 'startsWith (兼容性)', link: '/zh_hans/reference/compat/string/startsWith' },
|
||||
{ text: 'endsWith (兼容性)', link: '/zh_hans/reference/compat/string/endsWith' },
|
||||
{ text: 'padStart (兼容性)', link: '/zh_hans/reference/compat/string/padStart' },
|
||||
],
|
||||
},
|
||||
],
|
||||
|
29
docs/ko/reference/compat/string/padStart.md
Normal file
29
docs/ko/reference/compat/string/padStart.md
Normal file
@ -0,0 +1,29 @@
|
||||
# padStart
|
||||
|
||||
문자열을 주어진 길이가 될 때까지 앞에 글자를 추가해서 늘려요.
|
||||
|
||||
이미 문자열이 충분히 길거나, 앞에 추가할 글자가 빈 문자열이라면, 아무 동작도 하지 않아요.
|
||||
|
||||
## 인터페이스
|
||||
|
||||
```typescript
|
||||
function padStart(str: string, length = 0, chars = ' '): string;
|
||||
```
|
||||
|
||||
## 파라미터
|
||||
|
||||
- `str` (`string`): 길이를 늘릴 문자열.
|
||||
- `length` (`number`): 늘리고 싶은 길이. 기본값은 `0`.
|
||||
- `char` (`string`): 길이를 늘릴 때 추가할 글자. 기본값은 `' '`.
|
||||
|
||||
## 반환 값
|
||||
|
||||
주어진 길이까지 길어진, 앞에 글자가 추가된 문자열.
|
||||
|
||||
## 예시 값
|
||||
|
||||
```javascript
|
||||
padStart('hello', 10, 'a'); // 'aaaaahello'
|
||||
padStart('hello', 3, 'a'); // 'hello'
|
||||
padStart('hello', 5, ''); // 'hello'
|
||||
```
|
@ -1,20 +1,21 @@
|
||||
# padStart
|
||||
|
||||
Pads the start of a string with a given character until it reaches the specified length.
|
||||
|
||||
If the length is less than or equal to the original string's length, or if the padding character is an empty string,
|
||||
the original string is returned unchanged.
|
||||
|
||||
## Signature
|
||||
|
||||
```typescript
|
||||
function padStart<T extends string>(str: T, length = 0, chars = ' '): string;
|
||||
function padStart(str: string, length = 0, chars = ' '): string;
|
||||
```
|
||||
|
||||
## Parameters
|
||||
|
||||
- `str`: the string to pad
|
||||
- `length`: the length of the resulting string
|
||||
- `char`: the character to pad the string with
|
||||
- `str` (`string`): The string to pad.
|
||||
- `length` (`number`): The length of the resulting string. Defaults to `0`.
|
||||
- `char` (`string`): The character to pad the string with. Defaults to `' '`.
|
||||
|
||||
## Returns
|
||||
|
28
docs/zh_hans/reference/compat/string/padStart.md
Normal file
28
docs/zh_hans/reference/compat/string/padStart.md
Normal file
@ -0,0 +1,28 @@
|
||||
# padStart
|
||||
|
||||
在字符串的开头使用给定的字符进行填充,直到它达到指定的长度。
|
||||
|
||||
如果指定的长度小于或等于原字符串的长度,或者填充字符为空字符串,则返回原字符串不变。
|
||||
|
||||
## 签名
|
||||
|
||||
```typescript
|
||||
function padStart(str: string, length = 0, chars = ' '): string;
|
||||
```
|
||||
|
||||
## 参数
|
||||
|
||||
- `str` (`string`): 要填充的字符串。
|
||||
- `length` (`number`): 填充后的字符串长度。默认值为 `0`。
|
||||
- `char` (`string`): 用于填充字符串的字符。默认值为 `' '`。
|
||||
|
||||
## 返回值
|
||||
|
||||
返回一个新的字符串,该字符串用指定的字符填充,直到达到指定的长度。
|
||||
|
||||
## 示例
|
||||
|
||||
```javascript
|
||||
padStart('hello', 10, 'a'); // 'aaaaahello'
|
||||
padStart('hello', 3, 'a'); // 'hello'
|
||||
padStart('hello', 5, ''); // 'hello'
|
@ -55,6 +55,7 @@ export { matches } from './predicate/matches.ts';
|
||||
|
||||
export { startsWith } from './string/startsWith.ts';
|
||||
export { endsWith } from './string/endsWith.ts';
|
||||
export { padStart } from './string/padStart.ts';
|
||||
|
||||
export { max } from './math/max.ts';
|
||||
export { min } from './math/min.ts';
|
||||
|
@ -1,12 +1,12 @@
|
||||
/**
|
||||
* Pads the start of a string with a given character until it reaches the specified length.
|
||||
*
|
||||
* If the length is less than or equal to the original string's length, or if the padding character is an empty string,
|
||||
* the original string is returned unchanged.
|
||||
*
|
||||
* @template T - The type of the input string.
|
||||
* @param {T} str - The string to pad.
|
||||
* @param {number} [length=0] - The length of the resulting string once padded. Default is 0.
|
||||
* @param {string} [chars=' '] - The character(s) to use for padding. Default is a single space.
|
||||
* @param {string} str - The string to pad.
|
||||
* @param {number} [length] - The length of the resulting string once padded.
|
||||
* @param {string} [chars] - The character(s) to use for padding.
|
||||
* @returns {string} - The padded string, or the original string if padding is not required.
|
||||
*
|
||||
* @example
|
||||
@ -15,9 +15,6 @@
|
||||
* const result3 = padStart('abc', 3); // result will be 'abc'
|
||||
* const result4 = padStart('abc', 2); // result will be 'abc'
|
||||
*/
|
||||
export const padStart = <T extends string>(str: T, length = 0, chars = ' '): string => {
|
||||
if (Number.isSafeInteger(length) && length > str.length && chars.length > 0) {
|
||||
return str.padStart(length, chars);
|
||||
}
|
||||
return str;
|
||||
export function padStart(str: string, length = 0, chars = ' '): string {
|
||||
return str.padStart(length, chars);
|
||||
};
|
@ -4,4 +4,3 @@ export { kebabCase } from './kebabCase.ts';
|
||||
export { lowerCase } from './lowerCase.ts';
|
||||
export { startCase } from './startCase.ts';
|
||||
export { capitalize } from './capitalize.ts';
|
||||
export { padStart } from './padStart.ts';
|
||||
|
Loading…
Reference in New Issue
Block a user