Skip to content

Commit c011555

Browse files
committed
Lib: Refactor numSeparate formatter out of axes.js into Lib
1 parent 64e2052 commit c011555

File tree

3 files changed

+76
-22
lines changed

3 files changed

+76
-22
lines changed

src/lib/index.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,3 +541,47 @@ lib.objectFromPath = function(path, value) {
541541

542542
return obj;
543543
};
544+
545+
/**
546+
* Converts value to string separated by the provided separators.
547+
*
548+
* @example
549+
* lib.numSeparate(2016, '.,');
550+
* // returns '2016'
551+
*
552+
* @example
553+
* lib.numSeparate(1234.56, '|,')
554+
* // returns '1,234|56'
555+
*
556+
* @param {string|number} value the value to be converted
557+
* @param {string} separators string of decimal, then thousands separators
558+
*
559+
* @return {string} the value that has been separated
560+
*/
561+
lib.numSeparate = function(value, separators) {
562+
563+
if(typeof separators !== 'string' || separators.length === 0) {
564+
throw new Error('Separator string required for formatting!');
565+
}
566+
567+
if(typeof value === 'number') {
568+
value = String(value);
569+
}
570+
571+
var thousandsRe = /(\d+)(\d{3})/,
572+
decimalSep = separators.charAt(0),
573+
thouSep = separators.charAt(1);
574+
575+
var x = value.split('.'),
576+
x1 = x[0],
577+
x2 = x.length > 1 ? decimalSep + x[1] : '';
578+
579+
// Years are ignored for thousands separators
580+
if(thouSep && (x.length > 1 || x1.length>4)) {
581+
while(thousandsRe.test(x1)) {
582+
x1 = x1.replace(thousandsRe, '$1' + thouSep + '$2');
583+
}
584+
}
585+
586+
return x1 + x2;
587+
};

src/plots/cartesian/axes.js

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,7 +1105,7 @@ function numFormat(v, ax, fmtoverride, hover) {
11051105
if(dp) v = v.substr(0, dp + tickRound).replace(/\.?0+$/, '');
11061106
}
11071107
// insert appropriate decimal point and thousands separator
1108-
v = numSeparate(v, ax._gd._fullLayout.separators);
1108+
v = Lib.numSeparate(v, ax._gd._fullLayout.separators);
11091109
}
11101110

11111111
// add exponent
@@ -1141,27 +1141,6 @@ function numFormat(v, ax, fmtoverride, hover) {
11411141
return v;
11421142
}
11431143

1144-
// add arbitrary decimal point and thousands separator
1145-
var findThousands = /(\d+)(\d{3})/;
1146-
function numSeparate(nStr, separators) {
1147-
// separators - first char is decimal point,
1148-
// next char is thousands separator if there is one
1149-
1150-
var dp = separators.charAt(0),
1151-
thou = separators.charAt(1),
1152-
x = nStr.split('.'),
1153-
x1 = x[0],
1154-
x2 = x.length > 1 ? dp + x[1] : '';
1155-
// even if there is a thousands separator, don't use it on
1156-
// 4-digit integers (like years)
1157-
if(thou && (x.length > 1 || x1.length>4)) {
1158-
while(findThousands.test(x1)) {
1159-
x1 = x1.replace(findThousands, '$1' + thou + '$2');
1160-
}
1161-
}
1162-
return x1 + x2;
1163-
}
1164-
11651144

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

test/jasmine/tests/lib_test.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -930,4 +930,35 @@ describe('Test lib.js:', function() {
930930

931931
});
932932
});
933+
934+
describe('numSeparate', function() {
935+
936+
it('should work on numbers and strings', function() {
937+
expect(Lib.numSeparate(12345.67, '.,')).toBe('12,345.67');
938+
expect(Lib.numSeparate('12345.67', '.,')).toBe('12,345.67');
939+
});
940+
941+
it('should ignore years', function() {
942+
expect(Lib.numSeparate(2016, '.,')).toBe('2016');
943+
});
944+
945+
it('should work for multiple thousands', function() {
946+
expect(Lib.numSeparate(1000000000, '.,')).toBe('1,000,000,000');
947+
});
948+
949+
it('should work when there\'s only one separator', function() {
950+
expect(Lib.numSeparate(12.34, '|')).toBe('12|34');
951+
expect(Lib.numSeparate(1234.56, '|')).toBe('1234|56');
952+
});
953+
954+
it('should throw an error when no separator is provided', function() {
955+
expect(function() {
956+
Lib.numSeparate(1234);
957+
}).toThrowError('Separator string required for formatting!');
958+
959+
expect(function() {
960+
Lib.numSeparate(1234, '');
961+
}).toThrowError('Separator string required for formatting!');
962+
});
963+
});
933964
});

0 commit comments

Comments
 (0)