Created
July 24, 2014 12:07
-
-
Save stravid/887e46b95e97c14762d5 to your computer and use it in GitHub Desktop.
Testing Handlebars Helper with Ember CLI
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
import numberToCurrency from '../../helpers/number-to-currency'; | |
module('number-to-currency Handlebars Helper'); | |
test('convert invalid input to zero', function() { | |
equal('€ 0,00', numberToCurrency._rawFunction(null)); | |
equal('€ 0,00', numberToCurrency._rawFunction(undefined)); | |
}); | |
test('handle single digit input', function() { | |
equal('€ 0,00', numberToCurrency._rawFunction(0)); | |
equal('€ 0,09', numberToCurrency._rawFunction(9)); | |
}); | |
test('handle two digit input', function() { | |
equal('€ 0,99', numberToCurrency._rawFunction(99)); | |
}); | |
test('handle multi digit input', function() { | |
equal('€ 1,99', numberToCurrency._rawFunction(199)); | |
equal('€ 100,99', numberToCurrency._rawFunction(10099)); | |
equal('€ 1.000,99', numberToCurrency._rawFunction(100099)); | |
equal('€ 1.000.000,99', numberToCurrency._rawFunction(100000099)); | |
}); |
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
import Ember from 'ember'; | |
export default Ember.Handlebars.makeBoundHelper(function(value) { | |
if (value === null) { | |
value = 0; | |
} | |
if (isNaN(value)) { | |
value = 0; | |
} | |
value = value.toString(); | |
while (value.length < 3) { | |
value = '0' + value; | |
} | |
var fractionalPart = value.slice(-2); | |
var integerPart = value.slice(0, -2).replace(/\B(?=(\d{3})+(?!\d))/g, '.'); | |
return '€ ' + integerPart + ',' + fractionalPart; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment