feat(repeat): Add repeat (#459)

* bench: repeat

* test: repeat

* feat(repeat): Add repeat

* docs: repeat

* fix typo
This commit is contained in:
PiQuark6046 2024-09-03 23:35:38 +09:00 committed by GitHub
parent 53f5b698b9
commit e48c313d96
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 107 additions and 1 deletions

View File

@ -0,0 +1,13 @@
import { bench, describe } from 'vitest';
import { repeat as repeatToolkit } from 'es-toolkit/compat';
import { repeat as repeatLodash } from 'lodash';
describe('repeat', () => {
bench('es-toolkit/repeat', () => {
repeatToolkit('abc', 2);
});
bench('lodash/repeat', () => {
repeatLodash('abc', 2);
});
});

View File

@ -335,7 +335,7 @@ Even if a feature is marked "in review," it might already be under review to ens
| [padEnd](https://lodash.com/docs/4.17.15#padEnd) | ❌ |
| [padStart](https://lodash.com/docs/4.17.15#padStart) | ❌ |
| [parseInt](https://lodash.com/docs/4.17.15#parseInt) | ❌ |
| [repeat](https://lodash.com/docs/4.17.15#repeat) | |
| [repeat](https://lodash.com/docs/4.17.15#repeat) | |
| [replace](https://lodash.com/docs/4.17.15#replace) | ❌ |
| [snakeCase](https://lodash.com/docs/4.17.15#snakeCase) | 📝 |
| [split](https://lodash.com/docs/4.17.15#split) | ❌ |

View File

@ -0,0 +1,33 @@
# repeat
::: info
이 함수는 호환성을 위한 `es-toolkit/compat` 에서만 가져올 수 있어요. 대체할 수 있는 네이티브 JavaScript API가 있거나, 아직 충분히 최적화되지 않았기 때문이에요.
`es-toolkit/compat`에서 이 함수를 가져오면, [lodash와 완전히 똑같이 동작](../../../compatibility.md)해요.
:::
주어진 문자열을 주어진 횟수만큼 반복한 뒤에 리턴해요.
만약, 문자열이 없거나 횟수가 0인 경우에는 내용물이 없는 문자열을 리턴해요.
## 인터페이스
```typescript
function repeat(str: string, n: number): string
```
## 파라미터
- `str` (`string`): 반복할 문자열.
- `n` (`number`): 반복하시고 싶은 횟수.
## 반환 값
주어진 문자열이 n번째 반복된 문자열.
## 예시 값
```javascript
repeat('abc', 0); // ''
repeat('abc', 2); // 'abcabc'
```

View File

@ -0,0 +1,33 @@
# repeat
::: info
This function is only available in `es-toolkit/compat` for compatibility reasons. It either has alternative native JavaScript APIs or isnt fully optimized yet.
When imported from `es-toolkit/compat`, it behaves exactly like lodash and provides the same functionalities, as detailed [here](../../../compatibility.md).
:::
It returns the given string repeated a specified number of times.
If the string is empty or the count is 0, it returns an empty string.
## Signature
```typescript
function repeat(str: string, n: number): string
```
## Parameters
- `str` (`string`): The string to repeat.
- `n` (`number`): The number of times you want to repeat.
## Returns
The string repeated for the nth time.
## Examples
```javascript
repeat('abc', 0); // ''
repeat('abc', 2); // 'abcabc'
```

View File

@ -72,6 +72,7 @@ export { startsWith } from './string/startsWith.ts';
export { endsWith } from './string/endsWith.ts';
export { padStart } from './string/padStart.ts';
export { padEnd } from './string/padEnd.ts';
export { repeat } from './string/repeat.ts';
export { max } from './math/max.ts';
export { min } from './math/min.ts';

View File

@ -0,0 +1,12 @@
import { describe, it, expect } from 'vitest';
import { repeat } from './repeat';
describe('padStart', () => {
it('repeat abc 0', () => {
expect(repeat('abc', 0)).toBe('');
});
it('repeat abc 3', () => {
expect(repeat('abc', 3)).toBe('abcabcabc');
});
});

View File

@ -0,0 +1,14 @@
/**
* Repeats the given string n times.
*
* If n is less than 1, an empty string is returned, or if the string is an empty string,
* the original string is returned unchanged.
*
* @param {string} str - The string to repeat.
* @param n {number} - The number of times to repeat the string.
* @returns {string} - The repeated string, or an empty string if n is less than 1.
*/
export function repeat(str: string, n: number): string {
return str.repeat(n);
}