Created
April 23, 2016 20:25
-
-
Save wyster/b1804a1bf544af8bdbdacfc30e021b7a to your computer and use it in GitHub Desktop.
This file contains hidden or 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
function priceFormatter(priceText) { | |
"use strict"; | |
priceText = priceText.replace('.', '').trim(); | |
priceText = priceText.replace(',', '.').trim(); | |
const re = /(\D*)(.*)/; | |
const result = re.exec(priceText); | |
var price = parseFloat(parseFloat(result[2]).toFixed(2)); | |
return price; | |
} | |
const test = require('tape'); | |
test('price formatting test', function (t) { | |
t.plan(2); | |
t.equal(parseFloat('1357.14'), priceFormatter('1.357,14')); | |
t.equal(parseFloat('109.99'), priceFormatter('109,99')); | |
}); |
This file contains hidden or 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
TAP version 13 | |
# price formatting test | |
ok 1 should be equal | |
ok 2 should be equal | |
1..2 | |
# tests 2 | |
# pass 2 | |
# ok |
This file contains hidden or 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
function priceFormatter(priceText) { | |
"use strict"; | |
priceText = priceText.replace(',', '.').trim(); | |
const re = /(\D*)(.*)/; | |
const result = re.exec(priceText); | |
var price = parseFloat(parseFloat(result[2]).toFixed(2)); | |
return price; | |
} | |
const test = require('tape'); | |
test('price formatting test', function (t) { | |
t.plan(2); | |
t.equal(parseFloat('1357.14'), priceFormatter('1.357,14')); | |
t.equal(parseFloat('109.99'), priceFormatter('109,99')); | |
}); |
This file contains hidden or 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
TAP version 13 | |
# price formatting test | |
not ok 1 should be equal | |
--- | |
operator: equal | |
expected: 1.36 | |
actual: 1357.14 | |
at: Test.<anonymous> (/home/ilya/web/amazon-wish-list2/src/priceFormatter.js:20:7) | |
... | |
ok 2 should be equal | |
1..2 | |
# tests 2 | |
# pass 1 | |
# fail 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment