-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2332 from dusk-network/feature-2303
web-wallet: fix rounding errors in migration amount input
- Loading branch information
Showing
10 changed files
with
86 additions
and
41 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
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
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,8 @@ | ||
/** | ||
* @returns {string} | ||
*/ | ||
const getDecimalSeparator = () => { | ||
return (0.1).toLocaleString().slice(1, 2); | ||
}; | ||
|
||
export default getDecimalSeparator; |
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
export { default as createNumberFormatter } from "./createNumberFormatter"; | ||
export { default as getDecimalSeparator } from "./getDecimalSeparator"; |
28 changes: 28 additions & 0 deletions
28
web-wallet/src/lib/dusk/string/__tests__/cleanNumberString.spec.js
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,28 @@ | ||
import { describe, expect, it } from "vitest"; | ||
import { cleanNumberString } from "../"; | ||
|
||
describe("calculateAdaptiveCharCount", () => { | ||
it("should return a valid string represantation of a number when the decimal separator is `,`", () => { | ||
expect(cleanNumberString("12153,566,68468,,,351", ",")).toBe( | ||
"12153,56668468351" | ||
); | ||
}); | ||
|
||
it("should return a valid string represantation of a number when the decimal separator is `.`", () => { | ||
expect(cleanNumberString("100.00..549..6.548", ".")).toBe("100.005496548"); | ||
}); | ||
|
||
it("should return an empty string if an empty string is passed", () => { | ||
expect(cleanNumberString("", ".")).toBe(""); | ||
}); | ||
|
||
it("should return an empty string if a string with non valid characters is passed", () => { | ||
expect(cleanNumberString("asdsasd/*-,/?!@#$%^&*()_=+", ".")).toBe(""); | ||
}); | ||
|
||
it("should return a valid string represantation of a number if a string containing non valid characters is passed", () => { | ||
expect( | ||
cleanNumberString("1321651.0518asds592asd/*-,/?!@#$%^&*()_=+", ".") | ||
).toBe("1321651.0518592"); | ||
}); | ||
}); |
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,16 @@ | ||
/** | ||
* Cleans the input string | ||
* Returns a valid number in string form with the correct decimal separator according to the locale | ||
* | ||
* @param {string} amount | ||
* @param {string} separator | ||
* @returns {string} | ||
*/ | ||
const cleanNumberString = (amount, separator) => { | ||
const regex = new RegExp(`[^\\d${separator}]+`, "g"); // Remove any character that are not digits or the decimal separator | ||
const regex2 = new RegExp(`(?<=\\${separator}.*)\\${separator}`, "g"); // Remove all but the first decimal separator | ||
|
||
return amount.replace(regex, "").replace(regex2, ""); | ||
}; | ||
|
||
export default cleanNumberString; |
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