-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5af4ace
commit caea646
Showing
5 changed files
with
241 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!' | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); | ||
|
||
|
||
|