From db8f8a2675323a3249057d1f482f9ee07cbac58c Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Bellet Date: Fri, 11 Jun 2021 10:40:02 +0200 Subject: [PATCH] Create internal methods with regexp test - Make it more easily readable and add unit tests --- .../admin/utils/services/price_parser.js.coffee | 15 +++++++++++---- .../utils/services/price_parser_spec.js.coffee | 17 +++++++++++++++++ 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/app/assets/javascripts/admin/utils/services/price_parser.js.coffee b/app/assets/javascripts/admin/utils/services/price_parser.js.coffee index 492fbe48a0..ce7cd3b874 100644 --- a/app/assets/javascripts/admin/utils/services/price_parser.js.coffee +++ b/app/assets/javascripts/admin/utils/services/price_parser.js.coffee @@ -7,12 +7,10 @@ angular.module("admin.utils").factory "PriceParser", -> thousands_separator = I18n.toCurrency(1000, {precision: 1, unit: ''}).substring(1,2) # Replace comma used as a decimal separator and remplace by "." - if (price.match(/^[0-9]*(,{1})[0-9]{1,2}$/g)) - price = price.replace(",", ".") + price = this.replaceCommaByFinalPoint(price) # Remove configured thousands separator if it is actually a thousands separator - if (new RegExp("^([0-9]*(" + thousands_separator + "{1})[0-9]{3}[0-9\.,]*)*$", "g").test(price)) - price = price.replaceAll(thousands_separator, '') + price = this.removeThousandsSeparator(price, thousands_separator) if (decimal_separator == ",") price = price.replace(",", ".") @@ -22,3 +20,12 @@ angular.module("admin.utils").factory "PriceParser", -> return null if isNaN(price) return price + + replaceCommaByFinalPoint : (price) => + if price.match(/^[0-9]*(,{1})[0-9]{1,2}$/g) then price.replace(",", ".") else price + + removeThousandsSeparator : (price, thousands_separator) => + if (new RegExp("^([0-9]*(" + thousands_separator + "{1})[0-9]{3}[0-9\.,]*)*$", "g").test(price)) + price.replaceAll(thousands_separator, '') + else + price diff --git a/spec/javascripts/unit/admin/utils/services/price_parser_spec.js.coffee b/spec/javascripts/unit/admin/utils/services/price_parser_spec.js.coffee index 3d3dffd1ae..fb97815741 100644 --- a/spec/javascripts/unit/admin/utils/services/price_parser_spec.js.coffee +++ b/spec/javascripts/unit/admin/utils/services/price_parser_spec.js.coffee @@ -6,6 +6,23 @@ describe "PriceParser service", -> inject (PriceParser) -> priceParser = PriceParser + describe "test internal method with Regexp", -> + describe "test replaceCommaByFinalPoint() method", -> + it "handle the default case (with two numbers after comma)", -> + expect(priceParser.replaceCommaByFinalPoint("1,00")).toEqual "1.00" + it "doesn't confuse with thousands separator", -> + expect(priceParser.replaceCommaByFinalPoint("1,000")).toEqual "1,000" + it "handle also when there is only one number after the decimal separator", -> + expect(priceParser.replaceCommaByFinalPoint("1,0")).toEqual "1.0" + describe "test removeThousandsSeparator() method", -> + it "handle the default case", -> + expect(priceParser.removeThousandsSeparator("1,000", ",")).toEqual "1000" + expect(priceParser.removeThousandsSeparator("1,000,000", ",")).toEqual "1000000" + it "handle the case with decimal separator", -> + expect(priceParser.removeThousandsSeparator("1,000,000.00", ",")).toEqual "1000000.00" + it "handle the case when it is actually a decimal separator (and not a thousands one)", -> + expect(priceParser.removeThousandsSeparator("1,00", ",")).toEqual "1,00" + describe "with point as decimal separator and comma as thousands separator for I18n service", -> beforeEach ->