Skip to content

Commit

Permalink
fix: added replaceAll method for older node versions #23
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-shuvo committed Mar 25, 2022
1 parent 7c0273a commit 8eef7bf
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
19 changes: 16 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,19 @@ class CurrencyConverter {
return this
}

replaceAll(text, queryString, replaceString) {
let text_ = ""
for (let i = 0; i < text.length; i++) {
if (text[i] === queryString){
text_ += replaceString
}
else{
text_ += text[i]
}
}
return text_
}

rates(){
if(this.currencyFrom === this.currencyTo)
return new Promise((resolve, _) => {resolve(this.currencyAmount) })
Expand All @@ -235,13 +248,13 @@ class CurrencyConverter {
.then((rates) => {
if(this.isDecimalComma){
if(rates.includes("."))
rates = rates.replaceAll(".", "")
rates = this.replaceAll(rates, ".", "")
if(rates.includes(","))
rates = rates.replaceAll(",", ".")
rates = this.replaceAll(rates, ",", ".")
}
else{
if(rates.includes(","))
rates = rates.replaceAll(",", "")
rates = this.replaceAll(rates, ",", "")
}

return parseFloat(rates)
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,20 @@ describe("currencyConverter", () => {
})
})

describe("replaceAll", () => {
it("should replace all the , with empty space", () => {
assert.equal(currencyConverter.replaceAll("123,456,789.50", ",", ""), "123456789.50")
})

it("should replace all the , with empty .", () => {
assert.equal(currencyConverter.replaceAll("123456789,50", ",", "."), "123456789.50")
})

it("should replace all the . with empty space", () => {
assert.equal(currencyConverter.replaceAll("123.456.789", ".", ""), "123456789")
})
})

describe("rates", () => {
it("should not return undefined values", () => {
currencyConverter.from("USD").to("JPY")
Expand Down

0 comments on commit 8eef7bf

Please sign in to comment.