Skip to content

Instantly share code, notes, and snippets.

@stravid
Created July 24, 2014 12:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stravid/887e46b95e97c14762d5 to your computer and use it in GitHub Desktop.
Save stravid/887e46b95e97c14762d5 to your computer and use it in GitHub Desktop.
Testing Handlebars Helper with Ember CLI
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));
});
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