Skip to content

Commit

Permalink
Doc updated
Browse files Browse the repository at this point in the history
  • Loading branch information
Waqas-Jani committed Jan 25, 2024
1 parent 5af4ace commit caea646
Show file tree
Hide file tree
Showing 5 changed files with 241 additions and 4 deletions.
8 changes: 4 additions & 4 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ or relevant libraries. Common operations include concatenation, searching, repla
| ✔✔ `JStr.random` | Generates a random string. | [Documentation](docs/Random.md) |
| ✔✔ `JStr.remove` | Removes a specified substring from the string. | [Documentation](docs/Remove.md) |
| ✔✔ `JStr.repeat` | Repeats the string a specified number of times. | [Documentation](docs/Repeat.md) |
|`JStr.replace` | Replaces occurrences of a specified substring with another substring. | [Documentation](docs/Replace.md) |
|`JStr.replaceArray` | Replaces occurrences of specified substrings with corresponding replacements. | [Documentation](docs/ReplaceArray.md) |
| `JStr.replace` | Replaces occurrences of a specified substring with another substring. | [Documentation](docs/Replace.md) |
| `JStr.replaceArray` | Replaces occurrences of specified substrings with corresponding replacements. | [Documentation](docs/ReplaceArray.md) |
|`JStr.replaceFirst` | Replaces the first occurrence of a specified substring with another substring. | [Documentation](docs/ReplaceFirst.md) |
|`JStr.replaceLast` | Replaces the last occurrence of a specified substring with another substring. | [Documentation](docs/ReplaceLast.md) |
| ✔✔ `JStr.rtrim` | Removes whitespace from the end of the string. | [Documentation](docs/Rtrim.md) |
Expand Down Expand Up @@ -201,8 +201,8 @@ clarity and conciseness in your code.
|`prepend` | Prepends a string or an array of strings to the beginning of the current string. | [Documentation](docs/Prepend.md) |
| ✔✔ `remove` | Removes a specified substring from the string. | [Documentation](docs/Remove.md) |
| ✔✔ `repeat` | Repeats the string a specified number of times. | [Documentation](docs/Repeat.md) |
|`replace` | Replaces occurrences of a specified substring with another substring. | [Documentation](docs/Replace.md) |
|`replaceArray` | Replaces occurrences of specified substrings with corresponding replacements. | [Documentation](docs/ReplaceArray.md) |
| `replace` | Replaces occurrences of a specified substring with another substring. | [Documentation](docs/Replace.md) |
| `replaceArray` | Replaces occurrences of specified substrings with corresponding replacements. | [Documentation](docs/ReplaceArray.md) |
|`replaceFirst` | Replaces the first occurrence of a specified substring with another substring. | [Documentation](docs/ReplaceFirst.md) |
|`replaceLast` | Replaces the last occurrence of a specified substring with another substring. | [Documentation](docs/ReplaceLast.md) |
|`replaceMatches` | Replaces occurrences of a specified pattern with a callback result. | [Documentation](docs/ReplaceMatches.md) |
Expand Down
39 changes: 39 additions & 0 deletions docs/Replace.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Replace

### Example#1

```javascript
import JStr from "@akcybex/jstr";

const search = "world";
const replaceValue = "planet";
const subject = "Hello, world!";
const result = JStr.replace(search, replaceValue, subject);
console.log(result); // Outputs: 'Hello, planet!'
```

### Example#2

```javascript
import JStr from "@akcybex/jstr";

const search = ["o", "l"];
const replaceValue = ["O", "L"];
const subject = "Hello, world!";
const result = JStr.replace(search, replaceValue, subject);

console.log(result); // Outputs: 'HeLLO, wOrLd!'
```

### Example#3

```javascript
import JStr from "@akcybex/jstr";

const search = "o";
const replaceValue = "O";
const subject = "Hello, world!";
const result = JStr.of(subject).replace(search, replaceValue).toString();

console.log(result); // Outputs: 'HellO, wOrld!'
```
39 changes: 39 additions & 0 deletions docs/ReplaceArray.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# ReplaceArray

### Example#1

```javascript
import JStr from "@akcybex/jstr";

const search = "apple";
const replaceValue = ["banana"];
const subject = "I like apple and orange";
const result = JStr.of(subject).replaceArray(search, replaceValue).toString();
console.log(result); // Outputs: 'I like banana and orange'
```

### Example#2

```javascript
import JStr from "@akcybex/jstr";

const search = "$";
const replaceValue = ["Dollar", "Euro"];
const subject = "Pay $10 with €5";
const result = JStr.replaceArray(search, replaceValue, subject);

console.log(result); // Outputs: 'Pay Dollar10 with €5'
```

### Example#3

```javascript
import JStr from "@akcybex/jstr";

const search = "o";
const replaceValue = ["x"];
const subject = "hello world";
const result = JStr.replaceArray(search, replaceValue, subject);

console.log(result); // Outputs: 'hellx world'
```
35 changes: 35 additions & 0 deletions tests/replace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,41 @@ describe('JStr static replace method', () => {

expect(JStr.replace(search, replaceValue, subject)).toBe("HeLLO, wOrLd!");
});

test('should handle array input for search and replace', () => {
const search = ['apple', 'orange'];
const replaceValue = ['banana', 'grape'];
const subject = 'I like apple and orange';
const result = JStr.replace(search, replaceValue, subject);
expect(result).toBe('I like banana and grape');
});

});

describe('JStr dynamic replace method', () => {

test('JStr.replace() - should replace a single occurrence of search with replace', () => {
const search = 'world';
const replaceValue = 'planet';
const subject = 'Hello, world!';
expect(JStr.of(subject).replace(search, replaceValue).toString()).toBe("Hello, planet!");
});

test('JStr.replace() - should replace multiple occurrences of search with replace', () => {
const search = 'o';
const replaceValue = 'O';
const subject = 'Hello, world!';
expect(JStr.of(subject).replace(search, replaceValue).toString()).toBe("HellO, wOrld!");
});

test('JStr.replace() - should replace using arrays for search and replace', () => {
const search = ['o', 'l'];
const replaceValue = ['O', 'L'];
const subject = 'Hello, world!';


expect(JStr.of(subject).replace(search, replaceValue).toString()).toBe("HeLLO, wOrLd!");
});


});
Expand Down
124 changes: 124 additions & 0 deletions tests/replaceArray.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import JStr from '../src/main.ts';

/**
* The replaceArray method replaces a given value in the string sequentially using an array
*/

describe('JStr replaceArray static method', () => {
test('should replace a single string with an array of replacements', () => {
const search = 'o';
const replaceValue = ['x', 'y', 'z'];
const subject = 'Hello, world!';
const result = JStr.replaceArray(search, replaceValue, subject);
expect(result).toBe('Hellx, wyrld!');
});

test('should handle a single replacement in the array', () => {
const search = 'apple';
const replaceValue = ['banana'];
const subject = 'I like apple and orange';
const result = JStr.replaceArray(search, replaceValue, subject);
expect(result).toBe('I like banana and orange');
});

test('should handle an empty replacement array', () => {
const search = 'o';
const replaceValue: any[] = [];
const subject = 'hello world';
const result = JStr.replaceArray(search, replaceValue, subject);
expect(result).toBe('hello world');
});

test('should not replace if search string is not found', () => {
const search = 'missing';
const replaceValue = ['replacement'];
const subject = 'This is a test';
const result = JStr.replaceArray(search, replaceValue, subject);
expect(result).toBe('This is a test');
});

test('should handle special characters in search string', () => {
const search = '$';
const replaceValue = ['Dollar', 'Euro'];
const subject = 'Pay $10 with €5';
const result = JStr.replaceArray(search, replaceValue, subject);
expect(result).toBe('Pay Dollar10 with €5');
});

test('should handle an array of replacements longer than the number of occurrences', () => {
const search = 'o';
const replaceValue = ['x', 'y', 'z'];
const subject = 'hello world';
const result = JStr.replaceArray(search, replaceValue, subject);
expect(result).toBe('hellx wyrld');
});

test('should handle an array of replacements shorter than the number of occurrences', () => {
const search = 'o';
const replaceValue = ['x'];
const subject = 'hello world';
const result = JStr.replaceArray(search, replaceValue, subject);
expect(result).toBe('hellx world');
});
});

describe('JStr replaceArray dynamic method', () => {
test('should replace a single string with an array of replacements', () => {
const search = 'o';
const replaceValue = ['x', 'y', 'z'];
const subject = 'Hello, world!';
const result = JStr.of(subject).replaceArray(search, replaceValue).toString();
expect(result).toBe('Hellx, wyrld!');
});

test('should handle a single replacement in the array', () => {
const search = 'apple';
const replaceValue = ['banana'];
const subject = 'I like apple and orange';
const result = JStr.of(subject).replaceArray(search, replaceValue).toString();
expect(result).toBe('I like banana and orange');
});

test('should handle an empty replacement array', () => {
const search = 'o';
const replaceValue: any[] = [];
const subject = 'hello world';
const result = JStr.of(subject).replaceArray(search, replaceValue).toString();
expect(result).toBe('hello world');
});

test('should not replace if search string is not found', () => {
const search = 'missing';
const replaceValue = ['replacement'];
const subject = 'This is a test';
const result = JStr.of(subject).replaceArray(search, replaceValue).toString();
expect(result).toBe('This is a test');
});

test('should handle special characters in search string', () => {
const search = '$';
const replaceValue = ['Dollar', 'Euro'];
const subject = 'Pay $10 with €5';
const result = JStr.of(subject).replaceArray(search, replaceValue).toString();
expect(result).toBe('Pay Dollar10 with €5');
});

test('should handle an array of replacements longer than the number of occurrences', () => {
const search = 'o';
const replaceValue = ['x', 'y', 'z'];
const subject = 'hello world';
const result = JStr.of(subject).replaceArray(search, replaceValue).toString();
expect(result).toBe('hellx wyrld');
});

test('should handle an array of replacements shorter than the number of occurrences', () => {
const search = 'o';
const replaceValue = ['x'];
const subject = 'hello world';
const result = JStr.of(subject).replaceArray(search, replaceValue).toString();
expect(result).toBe('hellx world');
});
});



0 comments on commit caea646

Please sign in to comment.