Commit 158e2835 authored by Evgeny's avatar Evgeny

Use Excel Style DataFormatter to extend formatting values

parent a0db5cb6
import dataFormatter from 'excel-style-dataformatter';
// Default Excel value
const decimalPlaces = 2;
const locale = 'ru-RU';
......@@ -7,13 +10,15 @@ export default function format(v, type='', str=''){
return basicFormat(v);
}
// TODO: document possible type values somewhere(if not already)?
switch(type.toLowerCase()){
case 'number':
return numberFormat(v);
case 'currency':
return currencyFormat(v);
type = toStandard(type);
const result = dataFormatter.format(v.toString(), type, str);
if(result.value === ''){
throw new Error(`Unable to format value ${v} as type ${type} and format string (or option) ${str}`);
}
return result.value;
}
/**
......@@ -52,41 +57,6 @@ export function basicFormat(x){
return x.toLocaleString(); // for proper decimal separator
};
/**
* Basic number formatting, behaves like Excel number formatting with default
* settings (2 decimal places, no 1000 separator)
* @param {*} x - Input value
* @returns {String} String representation of input value
*/
export function numberFormat(x){
if(isNaN(x)){
throw new Error('Unable to format non-numeric and invalid values!');
} else {
return parseFloat(x).toLocaleString(
locale,
{
minimumFractionDigits: decimalPlaces,
maximumFractionDigits: decimalPlaces,
useGrouping: false
}
);
}
}
/**
* Basic currency formatting, behaves like Excel currency formatting with default
* settings (2 decimal places, russian ruble as a currency suffix)
* @param {*} x - Input value
* @returns {String} String representation of input value
*/
export function currencyFormat(x){
if(isNaN(x)){
throw new Error('Unable to format non-numeric and invalid values!');
} else {
return parseFloat(x).toLocaleString(
locale, {
style: 'currency', currency: 'RUR'
}
);
}
function toStandard(type){
return type[0].toUpperCase() + type.slice(1);
}
{
"name": "ed3-format-default",
"version": "0.0.2",
"version": "0.1.0",
"description": "Default format for ed3 library",
"main": "index.js",
"scripts": {
......@@ -13,7 +13,8 @@
"author": "missingdays",
"license": "ISC",
"dependencies": {
"d3": "^4.2.0"
"d3": "^4.2.0",
"excel-style-dataformatter": "^1.0.3"
},
"devDependencies": {
"babel-preset-es2015": "^6.13.2",
......
......@@ -12,23 +12,22 @@ describe('format', ()=>{
});
it('should do number formatting', function(){
expect(format(0, 'number')).to.be('0,00');
expect(format(1, 'number')).to.be('1,00');
expect(format(-1, 'number')).to.be('-1,00');
expect(format(1e10, 'number')).to.be('10000000000,00');
expect(format(1.1234, 'number')).to.be('1,12');
expect(format(1.1289, 'number')).to.be('1,13');
expect(() => format('123not-a-number', 'number')).to.throwError();
const str = '#,#0.00';
expect(format(0, 'number', str)).to.be('0,00');
expect(format(1, 'number', str)).to.be('1,00');
expect(format(-1, 'number', str)).to.be('-1,00');
expect(format(1e10, 'number', str)).to.be('10 000 000 000,00');
expect(format(1.1234, 'number', str)).to.be('1,12');
expect(format(1.1289, 'number', str)).to.be('1,13');
});
it('should do currency formatting', function(){
// Spaces should be non-breaking(ASCII 160/0xA0)
expect(format(0, 'currency')).to.be('0,00\xa0р.');
expect(format(1, 'currency')).to.be('1,00\xa0р.');
expect(format(-1, 'currency')).to.be('-1,00\xa0р.');
expect(format(1e10, 'currency')).to.be('10\xa0000\xa0000\xa0000,00\xa0р.');
expect(format(1.1234, 'currency')).to.be('1,12\xa0р.');
expect(format(1.1289, 'currency')).to.be('1,13\xa0р.');
expect(() => format('123not-a-number', 'currency')).to.throwError();
const str = '#,#0.00р.'
expect(format(0, 'Number', str)).to.be('0,00р.');
expect(format(1, 'Number', str)).to.be('1,00р.');
expect(format(-1, 'Number', str)).to.be('-1,00р.');
expect(format(1e10, 'Number', str)).to.be('10 000 000 000,00р.');
expect(format(1.1234, 'Number', str)).to.be('1,12р.');
expect(format(1.1289, 'Number', str)).to.be('1,13р.');
});
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment