From 17d8afa2a3abcd5d2b3e970db41aa2981122b9b6 Mon Sep 17 00:00:00 2001 From: raon0211 Date: Fri, 9 Aug 2024 10:28:11 +0900 Subject: [PATCH] feat(padStart): Move padStart to our compat library --- docs/.vitepress/en.mts | 6 ++-- docs/.vitepress/ko.mts | 7 +++-- docs/.vitepress/zh_hans.mts | 5 ++-- docs/ko/reference/compat/string/padStart.md | 29 +++++++++++++++++++ .../reference/{ => compat}/string/padStart.md | 9 +++--- .../reference/compat/string/padStart.md | 28 ++++++++++++++++++ src/compat/index.ts | 1 + src/{ => compat}/string/padStart.spec.ts | 0 src/{ => compat}/string/padStart.ts | 15 ++++------ src/string/index.ts | 1 - 10 files changed, 79 insertions(+), 22 deletions(-) create mode 100644 docs/ko/reference/compat/string/padStart.md rename docs/reference/{ => compat}/string/padStart.md (68%) create mode 100644 docs/zh_hans/reference/compat/string/padStart.md rename src/{ => compat}/string/padStart.spec.ts (100%) rename src/{ => compat}/string/padStart.ts (57%) diff --git a/docs/.vitepress/en.mts b/docs/.vitepress/en.mts index 16ffec60..daffde8d 100644 --- a/docs/.vitepress/en.mts +++ b/docs/.vitepress/en.mts @@ -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' }, ], }, ], diff --git a/docs/.vitepress/ko.mts b/docs/.vitepress/ko.mts index f09bb2a7..65332459 100644 --- a/docs/.vitepress/ko.mts +++ b/docs/.vitepress/ko.mts @@ -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' }, ], }, ], diff --git a/docs/.vitepress/zh_hans.mts b/docs/.vitepress/zh_hans.mts index 2c14ac7a..f6ab6661 100644 --- a/docs/.vitepress/zh_hans.mts +++ b/docs/.vitepress/zh_hans.mts @@ -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' }, ], }, ], diff --git a/docs/ko/reference/compat/string/padStart.md b/docs/ko/reference/compat/string/padStart.md new file mode 100644 index 00000000..cc4247a7 --- /dev/null +++ b/docs/ko/reference/compat/string/padStart.md @@ -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' +``` diff --git a/docs/reference/string/padStart.md b/docs/reference/compat/string/padStart.md similarity index 68% rename from docs/reference/string/padStart.md rename to docs/reference/compat/string/padStart.md index 079fd323..062e31a4 100644 --- a/docs/reference/string/padStart.md +++ b/docs/reference/compat/string/padStart.md @@ -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(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 diff --git a/docs/zh_hans/reference/compat/string/padStart.md b/docs/zh_hans/reference/compat/string/padStart.md new file mode 100644 index 00000000..8aa78a96 --- /dev/null +++ b/docs/zh_hans/reference/compat/string/padStart.md @@ -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' diff --git a/src/compat/index.ts b/src/compat/index.ts index a556bff8..7d155e3f 100644 --- a/src/compat/index.ts +++ b/src/compat/index.ts @@ -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'; diff --git a/src/string/padStart.spec.ts b/src/compat/string/padStart.spec.ts similarity index 100% rename from src/string/padStart.spec.ts rename to src/compat/string/padStart.spec.ts diff --git a/src/string/padStart.ts b/src/compat/string/padStart.ts similarity index 57% rename from src/string/padStart.ts rename to src/compat/string/padStart.ts index 48c555e6..2bf6c592 100644 --- a/src/string/padStart.ts +++ b/src/compat/string/padStart.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 = (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); }; diff --git a/src/string/index.ts b/src/string/index.ts index b27c7a21..fee6fe44 100644 --- a/src/string/index.ts +++ b/src/string/index.ts @@ -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';