Skip to content

Commit

Permalink
New functions
Browse files Browse the repository at this point in the history
  • Loading branch information
sanjeevkse committed Nov 22, 2023
1 parent 7728a16 commit 3933038
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ const { length } = require("light-string-utils");
- [`limit()`](#limit)
- [`occurance()`](#occurance)
- [`pad()`](#pad)
- [`repeat()`](#repeat)
- [`replaceBy()`](#replaceBy)
- [`sentenceCase()`](#sentenceCase)
- [`slugify()`](#slugify)
Expand Down Expand Up @@ -411,6 +412,30 @@ pad("Hello World", "both", "!!!"); // Returns "!!!Hello World!!!"
pad("Hello World", "both"); // Returns "Hello World"
```

## repeat

repeats a string by defined times.


### Parameters

| Name | Type | Default | Description |
| ---- | ---- | ------- | ----------- |
| item | {item} | _none_ | The input string/number to be repeated. |
| times | {number} | 1 | The number of times to repeat the string. |
| delimiter | {string} | "" | The delimiter to be used between the repeated strings. |
### Returns

{string} - The repeated string.

### Examples

```js
repeat("san"); // Returns "san"
repeat("san", 4); // Returns "sansansansan"
repeat("test ", 2); // Returns "test test"
```

## replaceBy

Replaces all the occurrences of a string with another string.
Expand Down
23 changes: 23 additions & 0 deletions src/repeat/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
@name repeat
@description repeats a string by defined times.
@param {item} item - _none_ - The input string/number to be repeated.
@param {number} times - 1 - The number of times to repeat the string.
@param {string} delimiter - "" - The delimiter to be used between the repeated strings.
@returns {string} - The repeated string.
@throws {Error} - Throws an error if the input is not a string.
@example repeat("san"); // Returns "san"
@example repeat("san", 4); // Returns "sansansansan"
@example repeat("test ", 2); // Returns "test test"
*/

function repeat(item, times = 1, delimiter = "") {
if (!item) {
throw new Error("Invalid string served");
}

return new Array(times).fill(item).join(delimiter);
}

module.exports = repeat;
14 changes: 14 additions & 0 deletions src/repeat/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const repeat = require("./index");

describe("repeat()", () => {
test("Test that the function throws an error if an invalid input is provided", () => {
expect(() => repeat(null)).toThrow(Error);
expect(() => repeat(undefined)).toThrow(Error);
});

test("Test that the function correctly repeats a single word", () => {
expect(repeat("test")).toBe("test");
expect(repeat("testing", 2)).toBe("testingtesting");
expect(repeat("testing", 2, " ")).toBe("testing testing");
});
});

0 comments on commit 3933038

Please sign in to comment.