mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-22 00:57:26 +00:00
Create internal methods with regexp test
- Make it more easily readable and add unit tests
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user