Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: string repeat function #397

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions docs/string/repeat.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
title: repeat
description: Convert a string to n times.
group: string
---

## Basic usage

Given a string return the string with the given number of times.

```ts
import { repeat } from 'radash'

repeat('hello', 3) // => 'hellohellohello'
repeat('hello', 0) // => ''
repeat('hello', -1) // => ''
```
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
"devDependencies": {
"@rollup/plugin-typescript": "^10.0.1",
"@types/chai": "^4.3.3",
"@types/jest": "^28.1.1",
"@types/jest": "^29.5.12",
"@types/mocha": "^10.0.6",
"chai": "^4.3.6",
"chiller": "^1.0.0-rc.30",
"esbuild": "^0.16.3",
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export {
capitalize,
dash,
pascal,
repeat,
snake,
template,
title,
Expand Down
23 changes: 23 additions & 0 deletions src/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,29 @@ export const pascal = (str: string): string => {
return parts.map(str => str.charAt(0).toUpperCase() + str.slice(1)).join('')
}

/**
* repeats the given string n times
* @param str the string to repeat
* @param num the number of times to repeat the string
* @returns the repeated string
*
* ```typescript
* repeat('a', 3) // 'aaa'
* repeat('a', 0) // ''
* repeat('a', -1) // ''
* ```
*/
export function repeat(str: string, num: number): string {
let result = ''
if (!str || num < 1 || num > Number.MAX_SAFE_INTEGER)
return result

for (let i = 0; i < num; i++)
result += str

return result
}

/**
* Formats the given string in title case fashion
*
Expand Down
12 changes: 12 additions & 0 deletions src/tests/string.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,18 @@ describe('string module', () => {
})
})

describe('repeat function', () => {
test('return empty string in repeat', () => {
assert.equal(_.repeat('a', 0), '')
assert.equal(_.repeat('a', -1), '')
})

test('return a string specifying the number of repetitions', () => {
assert.equal(_.repeat('a', 3), 'aaa')
assert.equal(_.repeat('a', 1), 'a')
})
})

describe('title function', () => {
test('returns input formatted in title case', () => {
assert.equal(_.title('hello world'), 'Hello World')
Expand Down
129 changes: 121 additions & 8 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,13 @@
dependencies:
jest-get-type "^28.0.2"

"@jest/expect-utils@^29.7.0":
version "29.7.0"
resolved "https://registry.yarnpkg.com/@jest/expect-utils/-/expect-utils-29.7.0.tgz#023efe5d26a8a70f21677d0a1afc0f0a44e3a1c6"
integrity sha512-GlsNBWiFQFCVi9QVSx7f5AgMeLxe9YCCs5PuP2O2LdjDAA8Jh9eX7lA1Jq/xdXw3Wb3hyvlFNfZIfcRetSzYcA==
dependencies:
jest-get-type "^29.6.3"

"@jest/expect@^28.1.3":
version "28.1.3"
resolved "https://registry.npmjs.org/@jest/expect/-/expect-28.1.3.tgz"
Expand Down Expand Up @@ -547,6 +554,13 @@
dependencies:
"@sinclair/typebox" "^0.24.1"

"@jest/schemas@^29.6.3":
version "29.6.3"
resolved "https://registry.yarnpkg.com/@jest/schemas/-/schemas-29.6.3.tgz#430b5ce8a4e0044a7e3819663305a7b3091c8e03"
integrity sha512-mo5j5X+jIZmJQveBKeS/clAueipV7KgiX1vMgCxam1RNYiqE1w62n0/tJJnHtjW8ZHcQco5gY85jA3mi0L+nSA==
dependencies:
"@sinclair/typebox" "^0.27.8"

"@jest/source-map@^28.1.2":
version "28.1.2"
resolved "https://registry.npmjs.org/@jest/source-map/-/source-map-28.1.2.tgz"
Expand Down Expand Up @@ -609,6 +623,18 @@
"@types/yargs" "^17.0.8"
chalk "^4.0.0"

"@jest/types@^29.6.3":
version "29.6.3"
resolved "https://registry.yarnpkg.com/@jest/types/-/types-29.6.3.tgz#1131f8cf634e7e84c5e77bab12f052af585fba59"
integrity sha512-u3UPsIilWKOM3F9CXtrG8LEJmNxwoCQC/XVj4IKYXvvpx7QIi/Kg1LI5uDmDpKlac62NUtX7eLjRh+jVZcLOzw==
dependencies:
"@jest/schemas" "^29.6.3"
"@types/istanbul-lib-coverage" "^2.0.0"
"@types/istanbul-reports" "^3.0.0"
"@types/node" "*"
"@types/yargs" "^17.0.8"
chalk "^4.0.0"

"@jridgewell/gen-mapping@^0.1.0":
version "0.1.1"
resolved "https://registry.npmjs.org/@jridgewell/gen-mapping/-/gen-mapping-0.1.1.tgz"
Expand Down Expand Up @@ -671,6 +697,11 @@
resolved "https://registry.npmjs.org/@sinclair/typebox/-/typebox-0.24.50.tgz"
integrity sha512-k8ETQOOQDg5FtK7y9KJWpsGLik+QlPmIi8zzl/dGUgshV2QitprkFlCR/AemjWOTyKn9UwSSGRTzLVotvgCjYQ==

"@sinclair/typebox@^0.27.8":
version "0.27.8"
resolved "https://registry.yarnpkg.com/@sinclair/typebox/-/typebox-0.27.8.tgz#6667fac16c436b5434a387a34dedb013198f6e6e"
integrity sha512-+Fj43pSMwJs4KRrH/938Uf+uAELIgVBmQzg/q1YG10djyfA3TnrU8N8XzqCh/okZdszqBQTZf96idMfE5lnwTA==

"@sinonjs/commons@^1.7.0":
version "1.8.3"
resolved "https://registry.npmjs.org/@sinonjs/commons/-/commons-1.8.3.tgz"
Expand Down Expand Up @@ -754,13 +785,18 @@
dependencies:
"@types/istanbul-lib-report" "*"

"@types/jest@^28.1.1":
version "28.1.8"
resolved "https://registry.npmjs.org/@types/jest/-/jest-28.1.8.tgz"
integrity sha512-8TJkV++s7B6XqnDrzR1m/TT0A0h948Pnl/097veySPN67VRAgQ4gZ7n2KfJo2rVq6njQjdxU3GCCyDvAeuHoiw==
"@types/jest@^29.5.12":
version "29.5.12"
resolved "https://registry.yarnpkg.com/@types/jest/-/jest-29.5.12.tgz#7f7dc6eb4cf246d2474ed78744b05d06ce025544"
integrity sha512-eDC8bTvT/QhYdxJAulQikueigY5AsdBRH2yDKW3yveW7svY3+DzN84/2NUgkw10RTiJbWqZrTtoGVdYlvFJdLw==
dependencies:
expect "^28.0.0"
pretty-format "^28.0.0"
expect "^29.0.0"
pretty-format "^29.0.0"

"@types/mocha@^10.0.6":
version "10.0.6"
resolved "https://registry.yarnpkg.com/@types/mocha/-/mocha-10.0.6.tgz#818551d39113081048bdddbef96701b4e8bb9d1b"
integrity sha512-dJvrYWxP/UcXm36Qn36fxhUKu8A/xMRXVT2cliFF1Z7UA9liG5Psj3ezNSZw+5puH2czDXRLcXQxf8JbJt0ejg==

"@types/node@*":
version "18.11.5"
Expand Down Expand Up @@ -1184,6 +1220,11 @@ diff-sequences@^28.1.1:
resolved "https://registry.npmjs.org/diff-sequences/-/diff-sequences-28.1.1.tgz"
integrity sha512-FU0iFaH/E23a+a718l8Qa/19bF9p06kgE0KipMOMadwa3SjnaElKzPaUC0vnibs6/B/9ni97s61mcejk8W1fQw==

diff-sequences@^29.6.3:
version "29.6.3"
resolved "https://registry.yarnpkg.com/diff-sequences/-/diff-sequences-29.6.3.tgz#4deaf894d11407c51efc8418012f9e70b84ea921"
integrity sha512-EjePK1srD3P08o2j4f0ExnylqRs5B9tJjcp9t1krH2qRi8CCdsYfwe9JgSLurFBWwq4uOlipzfk5fHNvwFKr8Q==

diff@^4.0.1:
version "4.0.2"
resolved "https://registry.npmjs.org/diff/-/diff-4.0.2.tgz"
Expand Down Expand Up @@ -1289,7 +1330,7 @@ exit@^0.1.2:
resolved "https://registry.npmjs.org/exit/-/exit-0.1.2.tgz"
integrity sha512-Zk/eNKV2zbjpKzrsQ+n1G6poVbErQxJ0LBOJXaKZ1EViLzH+hrLu9cdXI4zw9dBQJslwBEpbQ2P1oS7nDxs6jQ==

expect@^28.0.0, expect@^28.1.3:
expect@^28.1.3:
version "28.1.3"
resolved "https://registry.npmjs.org/expect/-/expect-28.1.3.tgz"
integrity sha512-eEh0xn8HlsuOBxFgIss+2mX85VAS4Qy3OSkjV7rlBWljtA4oWH37glVGyOZSZvErDT/yBywZdPGwCXuTvSG85g==
Expand All @@ -1300,6 +1341,17 @@ expect@^28.0.0, expect@^28.1.3:
jest-message-util "^28.1.3"
jest-util "^28.1.3"

expect@^29.0.0:
version "29.7.0"
resolved "https://registry.yarnpkg.com/expect/-/expect-29.7.0.tgz#578874590dcb3214514084c08115d8aee61e11bc"
integrity sha512-2Zks0hf1VLFYI1kbh0I5jP3KHHyCHpkfyHBzsSXRFgl/Bg9mWYfMW8oD+PdMPlEwy5HNsR9JutYy6pMeOh61nw==
dependencies:
"@jest/expect-utils" "^29.7.0"
jest-get-type "^29.6.3"
jest-matcher-utils "^29.7.0"
jest-message-util "^29.7.0"
jest-util "^29.7.0"

[email protected], fast-json-stable-stringify@^2.0.0:
version "2.1.0"
resolved "https://registry.npmjs.org/fast-json-stable-stringify/-/fast-json-stable-stringify-2.1.0.tgz"
Expand Down Expand Up @@ -1656,6 +1708,16 @@ jest-diff@^28.1.3:
jest-get-type "^28.0.2"
pretty-format "^28.1.3"

jest-diff@^29.7.0:
version "29.7.0"
resolved "https://registry.yarnpkg.com/jest-diff/-/jest-diff-29.7.0.tgz#017934a66ebb7ecf6f205e84699be10afd70458a"
integrity sha512-LMIgiIrhigmPrs03JHpxUh2yISK3vLFPkAodPeo0+BuF7wA2FoQbkEg1u8gBYBThncu7e1oEDUfIXVuTqLRUjw==
dependencies:
chalk "^4.0.0"
diff-sequences "^29.6.3"
jest-get-type "^29.6.3"
pretty-format "^29.7.0"

jest-docblock@^28.1.1:
version "28.1.1"
resolved "https://registry.npmjs.org/jest-docblock/-/jest-docblock-28.1.1.tgz"
Expand Down Expand Up @@ -1691,6 +1753,11 @@ jest-get-type@^28.0.2:
resolved "https://registry.npmjs.org/jest-get-type/-/jest-get-type-28.0.2.tgz"
integrity sha512-ioj2w9/DxSYHfOm5lJKCdcAmPJzQXmbM/Url3rhlghrPvT3tt+7a/+oXc9azkKmLvoiXjtV83bEWqi+vs5nlPA==

jest-get-type@^29.6.3:
version "29.6.3"
resolved "https://registry.yarnpkg.com/jest-get-type/-/jest-get-type-29.6.3.tgz#36f499fdcea197c1045a127319c0481723908fd1"
integrity sha512-zrteXnqYxfQh7l5FHyL38jL39di8H8rHoecLH3JNxH3BwOrBsNeabdap5e0I23lD4HHI8W5VFBZqG4Eaq5LNcw==

jest-haste-map@^28.1.3:
version "28.1.3"
resolved "https://registry.npmjs.org/jest-haste-map/-/jest-haste-map-28.1.3.tgz"
Expand Down Expand Up @@ -1728,6 +1795,16 @@ jest-matcher-utils@^28.1.3:
jest-get-type "^28.0.2"
pretty-format "^28.1.3"

jest-matcher-utils@^29.7.0:
version "29.7.0"
resolved "https://registry.yarnpkg.com/jest-matcher-utils/-/jest-matcher-utils-29.7.0.tgz#ae8fec79ff249fd592ce80e3ee474e83a6c44f12"
integrity sha512-sBkD+Xi9DtcChsI3L3u0+N0opgPYnCRPtGcQYrgXmR+hmt/fYfWAL0xRXYU8eWOdfuLgBe0YCW3AFtnRLagq/g==
dependencies:
chalk "^4.0.0"
jest-diff "^29.7.0"
jest-get-type "^29.6.3"
pretty-format "^29.7.0"

jest-message-util@^28.1.3:
version "28.1.3"
resolved "https://registry.npmjs.org/jest-message-util/-/jest-message-util-28.1.3.tgz"
Expand All @@ -1743,6 +1820,21 @@ jest-message-util@^28.1.3:
slash "^3.0.0"
stack-utils "^2.0.3"

jest-message-util@^29.7.0:
version "29.7.0"
resolved "https://registry.yarnpkg.com/jest-message-util/-/jest-message-util-29.7.0.tgz#8bc392e204e95dfe7564abbe72a404e28e51f7f3"
integrity sha512-GBEV4GRADeP+qtB2+6u61stea8mGcOT4mCtrYISZwfu9/ISHFJ/5zOMXYbpBE9RsS5+Gb63DW4FgmnKJ79Kf6w==
dependencies:
"@babel/code-frame" "^7.12.13"
"@jest/types" "^29.6.3"
"@types/stack-utils" "^2.0.0"
chalk "^4.0.0"
graceful-fs "^4.2.9"
micromatch "^4.0.4"
pretty-format "^29.7.0"
slash "^3.0.0"
stack-utils "^2.0.3"

jest-mock@^28.1.3:
version "28.1.3"
resolved "https://registry.npmjs.org/jest-mock/-/jest-mock-28.1.3.tgz"
Expand Down Expand Up @@ -1880,6 +1972,18 @@ jest-util@^28.0.0, jest-util@^28.1.3:
graceful-fs "^4.2.9"
picomatch "^2.2.3"

jest-util@^29.7.0:
version "29.7.0"
resolved "https://registry.yarnpkg.com/jest-util/-/jest-util-29.7.0.tgz#23c2b62bfb22be82b44de98055802ff3710fc0bc"
integrity sha512-z6EbKajIpqGKU56y5KBUgy1dt1ihhQJgWzUlZHArA/+X2ad7Cb5iF+AK1EWVL/Bo7Rz9uurpqw6SiBCefUbCGA==
dependencies:
"@jest/types" "^29.6.3"
"@types/node" "*"
chalk "^4.0.0"
ci-info "^3.2.0"
graceful-fs "^4.2.9"
picomatch "^2.2.3"

jest-validate@^28.1.3:
version "28.1.3"
resolved "https://registry.npmjs.org/jest-validate/-/jest-validate-28.1.3.tgz"
Expand Down Expand Up @@ -2222,7 +2326,7 @@ prettier@^2.7.1:
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.8.1.tgz#4e1fd11c34e2421bc1da9aea9bd8127cd0a35efc"
integrity sha512-lqGoSJBQNJidqCHE80vqZJHWHRFoNYsSpP9AjFhlhi9ODCJA541svILes/+/1GM3VaL/abZi7cpFzOpdR9UPKg==

pretty-format@^28.0.0, pretty-format@^28.1.3:
pretty-format@^28.1.3:
version "28.1.3"
resolved "https://registry.npmjs.org/pretty-format/-/pretty-format-28.1.3.tgz"
integrity sha512-8gFb/To0OmxHR9+ZTb14Df2vNxdGCX8g1xWGUTqUw5TiZvcQf5sHKObd5UcPyLLyowNwDAMTF3XWOG1B6mxl1Q==
Expand All @@ -2232,6 +2336,15 @@ pretty-format@^28.0.0, pretty-format@^28.1.3:
ansi-styles "^5.0.0"
react-is "^18.0.0"

pretty-format@^29.0.0, pretty-format@^29.7.0:
version "29.7.0"
resolved "https://registry.yarnpkg.com/pretty-format/-/pretty-format-29.7.0.tgz#ca42c758310f365bfa71a0bda0a807160b776812"
integrity sha512-Pdlw/oPxN+aXdmM9R00JVC9WVFoCLTKJvDVLgmJ+qAffBMxsV85l/Lu7sNx4zSzPyoL2euImuEwHhOXdEgNFZQ==
dependencies:
"@jest/schemas" "^29.6.3"
ansi-styles "^5.0.0"
react-is "^18.0.0"

prompts@^2.0.1:
version "2.4.2"
resolved "https://registry.npmjs.org/prompts/-/prompts-2.4.2.tgz"
Expand Down
Loading