@restorecommerce/handlebars-helperized
Version:
Opinionated handlebars based templating engine for rendering e-mail like content
54 lines (44 loc) • 1.37 kB
text/typescript
const numbro = require('numbro');
const allLanguages = require('numbro/dist/languages.min');
Object.values(allLanguages).forEach((data) => {
numbro.registerLanguage(data);
});
let numbroHandlebarsExtension = (hbs: any, opts: any) => {
const locale = opts.locale.replace('_', '-');
// For numeric values without decimals
hbs.registerHelper('nfn', (value: any, hash: any) => {
numbro.setLanguage(locale);
return numbro(value).format({
thousandSeparated: true,
mantissa: 0
});
});
// For currency based numeric values
hbs.registerHelper('nfc', (value: any, hash: any) => {
const lhash = hash.hash;
numbro.setLanguage(locale);
// Don't use formatCurrency as it does not allow control over the currency
// symbol in confunction with locales
let formatted = numbro(value).format({
mantissa: 2,
thousandSeparated: true
});
const cs = lhash.cs;
if (cs === undefined) return formatted;
if (lhash.csPos === 'postfix') {
formatted += cs;
} else {
formatted = cs + formatted;
}
return formatted;
});
// For byte based numeric values
hbs.registerHelper('nfb', (value: any, hash: any) => {
numbro.setLanguage(locale);
return numbro(value).format({
output: 'byte',
base: 'binary'
});
});
};
module.exports = numbroHandlebarsExtension;