Move unlocalize_currency filter to a PriceParser service

- It's no longer a filter but more a service: it's therefore more logic.
This commit is contained in:
Jean-Baptiste Bellet
2021-06-11 10:01:19 +02:00
parent 6cf0c73453
commit 178c0a441b
6 changed files with 69 additions and 70 deletions

View File

@@ -1,10 +1,10 @@
describe 'convert string to number with configurated currency', ->
filter = null
describe "PriceParser service", ->
priceParser = null
beforeEach ->
module 'ofn.admin'
inject ($filter) ->
filter = $filter('unlocalizeCurrency')
module('admin.utils')
inject (PriceParser) ->
priceParser = PriceParser
describe "with point as decimal separator and comma as thousands separator for I18n service", ->
@@ -16,34 +16,34 @@ describe 'convert string to number with configurated currency', ->
return "1,000"
it "handle point as decimal separator", ->
expect(filter("1.00")).toEqual 1.0
expect(priceParser.parse("1.00")).toEqual 1.0
it "handle point as decimal separator", ->
expect(filter("1.000")).toEqual 1.0
expect(priceParser.parse("1.000")).toEqual 1.0
it "also handle comma as decimal separator", ->
expect(filter("1,0")).toEqual 1.0
expect(priceParser.parse("1,0")).toEqual 1.0
it "also handle comma as decimal separator", ->
expect(filter("1,00")).toEqual 1.0
expect(priceParser.parse("1,00")).toEqual 1.0
it "also handle comma as decimal separator", ->
expect(filter("11,00")).toEqual 11.0
expect(priceParser.parse("11,00")).toEqual 11.0
it "handle comma as decimal separator but not confusing with thousands separator", ->
expect(filter("11,000")).toEqual 11000
expect(priceParser.parse("11,000")).toEqual 11000
it "handle point as decimal separator and comma as thousands separator", ->
expect(filter("1,000,000.00")).toEqual 1000000
expect(priceParser.parse("1,000,000.00")).toEqual 1000000
it "handle integer number", ->
expect(filter("10")).toEqual 10
expect(priceParser.parse("10")).toEqual 10
it "handle integer number with comma as thousands separator", ->
expect(filter("1,000")).toEqual 1000
expect(priceParser.parse("1,000")).toEqual 1000
it "handle integer number with no thousands separator", ->
expect(filter("1000")).toEqual 1000
expect(priceParser.parse("1000")).toEqual 1000
describe "with comma as decimal separator and final point as thousands separator for I18n service", ->
@@ -55,34 +55,34 @@ describe 'convert string to number with configurated currency', ->
return "1.000"
it "handle comma as decimal separator", ->
expect(filter("1,00")).toEqual 1.0
expect(priceParser.parse("1,00")).toEqual 1.0
it "handle comma as decimal separator with one digit after the comma", ->
expect(filter("11,0")).toEqual 11.0
expect(priceParser.parse("11,0")).toEqual 11.0
it "handle comma as decimal separator with two digit after the comma", ->
expect(filter("11,00")).toEqual 11.0
expect(priceParser.parse("11,00")).toEqual 11.0
it "handle comma as decimal separator with three digit after the comma", ->
expect(filter("11,000")).toEqual 11.0
expect(priceParser.parse("11,000")).toEqual 11.0
it "also handle point as decimal separator", ->
expect(filter("1.00")).toEqual 1.0
expect(priceParser.parse("1.00")).toEqual 1.0
it "also handle point as decimal separator with integer part with two digits", ->
expect(filter("11.00")).toEqual 11.0
expect(priceParser.parse("11.00")).toEqual 11.0
it "handle point as decimal separator and final point as thousands separator", ->
expect(filter("1.000.000,00")).toEqual 1000000
expect(priceParser.parse("1.000.000,00")).toEqual 1000000
it "handle integer number", ->
expect(filter("10")).toEqual 10
expect(priceParser.parse("10")).toEqual 10
it "handle integer number with final point as thousands separator", ->
expect(filter("1.000")).toEqual 1000
expect(priceParser.parse("1.000")).toEqual 1000
it "handle integer number with no thousands separator", ->
expect(filter("1000")).toEqual 1000
expect(priceParser.parse("1000")).toEqual 1000
describe "with comma as decimal separator and space as thousands separator for I18n service", ->
@@ -94,41 +94,42 @@ describe 'convert string to number with configurated currency', ->
return "1 000"
it "handle comma as decimal separator", ->
expect(filter("1,00")).toEqual 1.0
expect(priceParser.parse("1,00")).toEqual 1.0
it "handle comma as decimal separator with one digit after the comma", ->
expect(filter("11,0")).toEqual 11.0
expect(priceParser.parse("11,0")).toEqual 11.0
it "handle comma as decimal separator with two digit after the comma", ->
expect(filter("11,00")).toEqual 11.0
expect(priceParser.parse("11,00")).toEqual 11.0
it "handle comma as decimal separator with three digit after the comma", ->
expect(filter("11,000")).toEqual 11.0
expect(priceParser.parse("11,000")).toEqual 11.0
it "also handle final point as decimal separator", ->
expect(filter("1.00")).toEqual 1.0
expect(priceParser.parse("1.00")).toEqual 1.0
it "also handle final point as decimal separator with integer part with two digits", ->
expect(filter("11.00")).toEqual 11.0
expect(priceParser.parse("11.00")).toEqual 11.0
it "handle point as decimal separator and space as thousands separator", ->
expect(filter("1 000 000,00")).toEqual 1000000
expect(priceParser.parse("1 000 000,00")).toEqual 1000000
it "handle integer number", ->
expect(filter("10")).toEqual 10
expect(priceParser.parse("10")).toEqual 10
it "handle integer number with space as thousands separator", ->
expect(filter("1 000")).toEqual 1000
expect(priceParser.parse("1 000")).toEqual 1000
it "handle integer number with no thousands separator", ->
expect(filter("1000")).toEqual 1000
expect(priceParser.parse("1000")).toEqual 1000
describe "handle null/undefined case", ->
it "null case", ->
expect(filter(null)).toEqual null
expect(priceParser.parse(null)).toEqual null
it "undefined case ", ->
expect(filter(undefined)).toEqual null
expect(priceParser.parse(undefined)).toEqual null
it "wtf case", ->
expect(filter("wtf")).toEqual null
expect(priceParser.parse("wtf")).toEqual null