Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions src/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -541,3 +541,47 @@ lib.objectFromPath = function(path, value) {

return obj;
};

/**
* Converts value to string separated by the provided separators.
*
* @example
* lib.numSeparate(2016, '.,');
* // returns '2016'
*
* @example
* lib.numSeparate(1234.56, '|,')
* // returns '1,234|56'
*
* @param {string|number} value the value to be converted
* @param {string} separators string of decimal, then thousands separators
*
* @return {string} the value that has been separated
*/
lib.numSeparate = function(value, separators) {

if(typeof separators !== 'string' || separators.length === 0) {
throw new Error('Separator string required for formatting!');
}

if(typeof value === 'number') {
value = String(value);
}

var thousandsRe = /(\d+)(\d{3})/,
decimalSep = separators.charAt(0),
thouSep = separators.charAt(1);

var x = value.split('.'),
x1 = x[0],
x2 = x.length > 1 ? decimalSep + x[1] : '';

// Years are ignored for thousands separators
if(thouSep && (x.length > 1 || x1.length>4)) {
while(thousandsRe.test(x1)) {
x1 = x1.replace(thousandsRe, '$1' + thouSep + '$2');
}
}

return x1 + x2;
};
23 changes: 1 addition & 22 deletions src/plots/cartesian/axes.js
Original file line number Diff line number Diff line change
Expand Up @@ -1105,7 +1105,7 @@ function numFormat(v, ax, fmtoverride, hover) {
if(dp) v = v.substr(0, dp + tickRound).replace(/\.?0+$/, '');
}
// insert appropriate decimal point and thousands separator
v = numSeparate(v, ax._gd._fullLayout.separators);
v = Lib.numSeparate(v, ax._gd._fullLayout.separators);
}

// add exponent
Expand Down Expand Up @@ -1141,27 +1141,6 @@ function numFormat(v, ax, fmtoverride, hover) {
return v;
}

// add arbitrary decimal point and thousands separator
var findThousands = /(\d+)(\d{3})/;
function numSeparate(nStr, separators) {
// separators - first char is decimal point,
// next char is thousands separator if there is one

var dp = separators.charAt(0),
thou = separators.charAt(1),
x = nStr.split('.'),
x1 = x[0],
x2 = x.length > 1 ? dp + x[1] : '';
// even if there is a thousands separator, don't use it on
// 4-digit integers (like years)
if(thou && (x.length > 1 || x1.length>4)) {
while(findThousands.test(x1)) {
x1 = x1.replace(findThousands, '$1' + thou + '$2');
}
}
return x1 + x2;
}


axes.subplotMatch = /^x([0-9]*)y([0-9]*)$/;

Expand Down
31 changes: 31 additions & 0 deletions test/jasmine/tests/lib_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -930,4 +930,35 @@ describe('Test lib.js:', function() {

});
});

describe('numSeparate', function() {

it('should work on numbers and strings', function() {
expect(Lib.numSeparate(12345.67, '.,')).toBe('12,345.67');
expect(Lib.numSeparate('12345.67', '.,')).toBe('12,345.67');
});

it('should ignore years', function() {
expect(Lib.numSeparate(2016, '.,')).toBe('2016');
});

it('should work for multiple thousands', function() {
expect(Lib.numSeparate(1000000000, '.,')).toBe('1,000,000,000');
});

it('should work when there\'s only one separator', function() {
expect(Lib.numSeparate(12.34, '|')).toBe('12|34');
expect(Lib.numSeparate(1234.56, '|')).toBe('1234|56');
});

it('should throw an error when no separator is provided', function() {
expect(function() {
Lib.numSeparate(1234);
}).toThrowError('Separator string required for formatting!');

expect(function() {
Lib.numSeparate(1234, '');
}).toThrowError('Separator string required for formatting!');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nicely done.

});
});
});